Joomla 2.5 Templates – JFactory::getApplication()

Joomla 2.5 has reached its end of life as for 12/31/2014. Please be advised this may be a security risk to your website. You can view more information about the end of life here.

As we dig deeper into the PHP code that makes up the Joomla 2.5 Beez2 template, we come to the following line of code:

$app                = JFactory::getApplication();

We have two items here that we need to review, JFactory and getApplication.

What is JFactory used for?

You can use JFactory to get access to several core Joomla objects. When you use JFactory, your variable will be an object and generally contains your Joomla 2.5 site settings.

According to official documentation, there are many other objects that you can call.

What data is returned with getApplication() ?

Using the following code, we can look at the contents of the $app object, which is assigned its values by JFactory::getApplication().

$app                = JFactory::getApplication();
echo “<pre>”; print_r($app); echo “</pre>”; die();

As you can see in the table below, there is quite a bit of information put into the $app variable by JFactory::getApplication().

Actual Data in $app variableScreenshot of our testing code
JSite Object
(
    [template:private] => stdClass Object
        (
            [id] => 4
            [home] => 1
            [template] => beez_20
            [params] => JRegistry Object
                (
                    [data:protected] => stdClass Object
                        (
                            [wrapperSmall] => 53
                            [wrapperLarge] => 72
                            [logo] => images/joomla_black.gif
                            [sitetitle] => Joomla!
                            [sitedescription] => Open Source Content Management
                            [navposition] => left
                            [templatecolor] => personal
                        )

                )

        )

    [_language_filter:private] => 
    [_detect_browser:private] => 
    [clientId:protected] => 
    [_clientId:protected] => 0
    [messageQueue:protected] => Array
        (
        )

    [_messageQueue:protected] => Array
        (
        )

    [name:protected] => 
    [_name:protected] => site
    [scope] => 
    [requestTime] => 2012-07-12 15:36
    [startTime] => 1342107374.8458
    [input] => JInput Object
        (
            [options:protected] => Array
                (
                )

            [filter:protected] => JFilterInput Object
                (
                    [tagsArray] => Array
                        (
                        )

                    [attrArray] => Array
                        (
                        )

                    [tagsMethod] => 0
                    [attrMethod] => 0
                    [xssAuto] => 1
                    [tagBlacklist] => Array
                        (
                            [0] => applet
                            [1] => body
                            [2] => bgsound
                            [3] => base
                            [4] => basefont
                            [5] => embed
                            [6] => frame
                            [7] => frameset
                            [8] => head
                            [9] => html
                            [10] => id
                            [11] => iframe
                            [12] => ilayer
                            [13] => layer
                            [14] => link
                            [15] => meta
                            [16] => name
                            [17] => object
                            [18] => script
                            [19] => style
                            [20] => title
                            [21] => xml
                        )

                    [attrBlacklist] => Array
                        (
                            [0] => action
                            [1] => background
                            [2] => codebase
                            [3] => dynsrc
                            [4] => lowsrc
                        )

                    [_errors:protected] => Array
                        (
                        )

                )

            [data:protected] => Array
                (
                    [SESSf61997c271b5d7e7cd0e3d4d2cc7fed5] => oQ8nNIkJD-4hEdP-MWN7foEq6jN2SC6Ww19I9Q26zv8
                    [jpanesliders_menu-sliders-543] => 0
                    [jpanesliders_plugin-sliders-433] => 0
                    [jpanesliders_permissions-sliderscom_users] => 0
                    [jpanesliders_sliders] => 1
                    [jpanesliders_plugin-sliders-10067] => 0
                    [jpanesliders_content-sliders-] => 0
                    [jpanesliders_permissions-sliders-] => 0
                    [jpanesliders_menu-sliders-435] => 0
                    [jpanesliders_content-sliders-79] => 0
                    [jpanesliders_permissions-sliders-79] => 0
                    [jpanesliders_permissions-sliderscom_content] => 0
                    [jpanesliders_menu-sliders-] => 0
                    [fb8d050fa6edfad9f07b1fb40c8266d0] => 400ef7499e634eb4e7c9cb0d763b22e1
                    [037affe80d78b62a827b593db7df4c3b] => 91e8e99991ce9f146a26eab8c67f551e
                    [jpanesliders_module-sliders] => 0
                    [jpanesliders_panel-sliders] => 0
                    [jpanesliders_position-icon] => 0
                    [__utma] => 256822360.1859812311.1342025884.1342039063.1342100374.5
                    [__utmc] => 256822360
                    [__utmz] => 256822360.1342025884.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)
                    [jpanesliders_template-sliders-4] => 0
                    [option] => com_content
                    [view] => featured
                    [Itemid] => 435
                )

            [inputs:protected] => Array
                (
                    [request] => JInput Object
                        (
                            [options:protected] => Array
                                (
                                )

                            [filter:protected] => JFilterInput Object
                                (
                                    [tagsArray] => Array
                                        (
                                        )

                                    [attrArray] => Array
                                        (
                                        )

                                    [tagsMethod] => 0
                                    [attrMethod] => 0
                                    [xssAuto] => 1
                                    [tagBlacklist] => Array
                                        (
                                            [0] => applet
                                            [1] => body
                                            [2] => bgsound
                                            [3] => base
                                            [4] => basefont
                                            [5] => embed
                                            [6] => frame
                                            [7] => frameset
                                            [8] => head
                                            [9] => html
                                            [10] => id
                                            [11] => iframe
                                            [12] => ilayer
                                            [13] => layer
                                            [14] => link
                                            [15] => meta
                                            [16] => name
                                            [17] => object
                                            [18] => script
                                            [19] => style
                                            [20] => title
                                            [21] => xml
                                        )

                                    [attrBlacklist] => Array
                                        (
                                            [0] => action
                                            [1] => background
                                            [2] => codebase
                                            [3] => dynsrc
                                            [4] => lowsrc
                                        )

                                    [_errors:protected] => Array
                                        (
                                        )

                                )

                            [data:protected] => Array
                                (
                                    [SESSf61997c271b5d7e7cd0e3d4d2cc7fed5] => oQ8nNIkJD-4hEdP-MWN7foEq6jN2SC6Ww19I9Q26zv8
                                    [jpanesliders_menu-sliders-543] => 0
                                    [jpanesliders_plugin-sliders-433] => 0
                                    [jpanesliders_permissions-sliderscom_users] => 0
                                    [jpanesliders_sliders] => 1
                                    [jpanesliders_plugin-sliders-10067] => 0
                                    [jpanesliders_content-sliders-] => 0
                                    [jpanesliders_permissions-sliders-] => 0
                                    [jpanesliders_menu-sliders-435] => 0
                                    [jpanesliders_content-sliders-79] => 0
                                    [jpanesliders_permissions-sliders-79] => 0
                                    [jpanesliders_permissions-sliderscom_content] => 0
                                    [jpanesliders_menu-sliders-] => 0
                                    [fb8d050fa6edfad9f07b1fb40c8266d0] => 400ef7499e634eb4e7c9cb0d763b22e1
                                    [037affe80d78b62a827b593db7df4c3b] => 91e8e99991ce9f146a26eab8c67f551e
                                    [jpanesliders_module-sliders] => 0
                                    [jpanesliders_panel-sliders] => 0
                                    [jpanesliders_position-icon] => 0
                                    [__utma] => 256822360.1859812311.1342025884.1342039063.1342100374.5
                                    [__utmc] => 256822360
                                    [__utmz] => 256822360.1342025884.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)
                                    [jpanesliders_template-sliders-4] => 0
                                    [option] => com_content
                                    [view] => featured
                                    [Itemid] => 435
                                )

                            [inputs:protected] => Array
                                (
                                )

                        )

                )

        )

    [_errors:protected] => Array
        (
        )

)
data-in-app-variable

How is the $app variable used in the template?

We now know that the $app variable contains a lot of information, but how is this information used within our template? To find out, we’ll look at an example where $app is used:

$templateparams     = $app->getTemplate(true)->params;

The above appears to be the only use of the $app variable within the Beez2 index.php file. In this case, we are using $app with getTemplate params to apparently get the parameters of our template (which we’ve apparently already done within the template?).

We’ll see what values are stored in $templateparams by using the following code:

$templateparams     = $app->getTemplate(true)->params;
echo “<pre>”; print_r($templateparams); echo “</pre>”; die();

The results of that line of code confirm that the $templateparams variable contains the values of our template’s parameters:

JRegistry Object
(
    [data:protected] => stdClass Object
        (
            [wrapperSmall] => 53
            [wrapperLarge] => 72
            [logo] => images/joomla_black.gif
            [sitetitle] => Joomla!
            [sitedescription] => Open Source Content Management
            [navposition] => left
            [templatecolor] => personal
        )

)

Again, we have already accessed these same values using the following code (which appeared earlier in the template):

// get params
$color              = $this->params->get('templatecolor');
$logo               = $this->params->get('logo');
$navposition        = $this->params->get('navposition');

In the end, the $templateparams variable is ultimately used in the following context:

$templateparams->get(‘sitetitle’)

$templateparams->get(‘sitedescription’)

Why is the JFactory::getApplication() used in this template?

Above we showed you how JFactory::getApplication is used in the template, now we will show you WHY it was used:

  1. JFactory::getApplication was used to create the $app variable:$app = JFactory::getApplication();
  2. The $app variable was then used to create the $templateparams variable:$templateparams = $app->getTemplate(true)->params;
  3. Finally, $templateparams is used to get the following values:$templateparams->get(‘sitetitle’)

    and…

    $templateparams->get(‘sitedescription’)

    So, in the end it appears that getApplication is used to get the site’s title and the site’s description:

    Test code to see the value of sitetitle and sitedescriptionWhat are the values of sitetitle and sitedescription?
    $app                = JFactory::getApplication();
    $doc        = JFactory::getDocument();
    $templateparams     = $app->getTemplate(true)->params;
    echo "                  templateparams->get('sitetitle') = " . $templateparams->get('sitetitle') . "
                    templateparams->get('sitedescription') = " . $templateparams->get('sitedescription') . "
            
     "; die();
    values-taken-from-templateparams

    Where are these values used within the template?Where are these values defined?
    sitetitle-and-sitedescription-in-the-template

    Where are these values defined? That’s a really good question! After further research, they do not appear to be set anywhere within the Global Configuration settings, anywhere within the configuration.php file, nor do they appear to be anywhere within the site’s language files. They are actually set in the template’s configuration (which means they’re stored in the database), as seen in the screenshot below:

    sitetitle-and-sitedescription-in-template-config

0 thoughts on “Joomla 2.5 Templates – JFactory::getApplication()

Was this article helpful? Join the conversation!

Server Madness Sale
Score Big with Savings up to 99% Off

X