Joomla 2.5 Templates – JFactory::getDocument()

In our continued effort to review the PHP code in the Joomla 2.5 Beez2 template, we come across the following code:

// get params $color              = $this->params->get('templatecolor'); $logo               = $this->params->get('logo'); $navposition        = $this->params->get('navposition'); $app                = JFactory::getApplication(); $doc        = JFactory::getDocument();

Our specific focus in this article will be $doc = JFactory::getDocument().

 

What is getDocument?

According to official documentation, getDocument returns the global document object as a JDocument object type. Our next step is to find out what is contained within this global document object.

 

What is included within JDocument?

Because we want to find the contents of JDocument, which is put into the $doc variable, we will adjust our template’s index.php to print out this data.

Updated Code Screenshot of the contents of $doc
// get params $color              = $this->params->get('templatecolor'); $logo               = $this->params->get('logo'); $navposition        = $this->params->get('navposition'); $app                = JFactory::getApplication(); $doc        = JFactory::getDocument(); $templateparams     = $app->getTemplate(true)->params; echo "<pre>"; print_r($doc); echo "</pre>"; die();
jdocumenthtml

Contents of $doc
JDocumentHTML Object (     [_links] => Array         (             [/joomla25/index.php?format=feed&type=rss] => Array                 (                     [relation] => alternate                     [relType] => rel                     [attribs] => Array                         (                             [type] => application/rss+xml                             [title] => RSS 2.0                         )                  )              [/joomla25/index.php?format=feed&type=atom] => Array                 (                     [relation] => alternate                     [relType] => rel                     [attribs] => Array                         (                             [type] => application/atom+xml                             [title] => Atom 1.0                         )                  )          )      [_custom] => Array         (         )      [template] => beez_20     [baseurl] => /joomla25     [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                 )          )      [_file] => /home/inmoti6/bradm.inmotiontesting.com/joomla25/templates/beez_20/index.php     [_template:protected] =>      [_template_tags:protected] => Array         (         )      [_caching:protected] =>      [title] => My New Page Title     [description] => This is my custom Meta Description     [link] =>      [base] => http://bradm.inmotiontesting.com/joomla25/     [language] => en-gb     [direction] => ltr     [_generator] => Joomla! - Open Source Content Management     [_mdate] =>      [_tab] =>        [_lineEnd] =>       [_charset] => utf-8     [_mime] => text/html     [_namespace] =>      [_profile] =>      [_scripts] => Array         (             [/joomla25/media/system/js/mootools-core.js] => Array                 (                     [mime] => text/javascript                     [defer] =>                      [async] =>                  )              [/joomla25/media/system/js/core.js] => Array                 (                     [mime] => text/javascript                     [defer] =>                      [async] =>                  )              [/joomla25/media/system/js/caption.js] => Array                 (                     [mime] => text/javascript                     [defer] =>                      [async] =>                  )              [http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js] => Array                 (                     [mime] => text/javascript                     [defer] =>                      [async] =>                  )              [http://bradm.inmotiontesting.com/joomla25/modules/mod_unite_nivoslider/tmpl/js/jquery.nivo.slider.pack.js] => Array                 (                     [mime] => text/javascript                     [defer] =>                      [async] =>                  )              [/joomla25/media/system/js/mootools-more.js] => Array                 (                     [mime] => text/javascript                     [defer] =>                      [async] =>                  )          )      [_script] => Array         (             [text/javascript] => window.addEvent('load', function() {                 new JCaption('img.caption');             });         )      [_styleSheets] => Array         (             [http://bradm.inmotiontesting.com/joomla25/modules/mod_unite_nivoslider/tmpl/css/nivo-slider.css] => Array                 (                     [mime] => text/css                     
=> [attribs] => Array ( ) ) [http://bradm.inmotiontesting.com/joomla25/modules/mod_unite_nivoslider/tmpl/themes/default/default.css] => Array ( [mime] => text/css
=> [attribs] => Array ( ) ) ) [_style] => Array ( ) [_metaTags] => Array ( [http-equiv] => Array ( [content-type] => text/html ) [standard] => Array ( [keywords] => joomla 2.5 testing, metadata options, meta keywords, learn joomla, joomla 2.5 tutorial [rights] => [robots] => index, follow ) ) [_engine] => [_type] => html [_errors:protected] => Array ( ) )

As you can see in the data above, the $doc variable has information about:

  1. RSS feeds within the page
  2. Site information, such as the title and description
  3. References to .js javascript and .css CSS files being loaded
  4. … and more

 

How is $doc / JDocument used within the template?

The only reference to $doc in this particular template is in the following code:

$doc->addScript($this->baseurl.'/templates/'.$this->template.'/javascript/md_stylechanger.js', 'text/javascript', true);

Based upon addScript, it appears we are telling Joomla to load another javascript file into the page, /javascript/md_stylechanger.js.

Leave a Reply