Retrieving parameter data in a template file

From Joomla! Documentation

Revision as of 08:51, 6 May 2010 by Srinivasmurty (Talk | contribs)

Replacement filing cabinet.png
This Namespace has been archived - Please Do Not Edit or Create Pages in this namespace. Pages contain information for a Joomla! version which is no longer supported. It exists only as a historical reference, will not be improved and its content may be incomplete.

Having defined a parameter in the templateDetails.xml file and saved a value for it in the params.ini file, it remains to be seen how to retrieve the current value of the parameter so it can be used in the template code.

To retrieve the value of a parameter in your template code use the function call:

<?php $myParam = $this->params->get( 'parameterName' ); ?>

Note that the $this object in the template is always the current instance of the JDocument object. Also note that $this->params is an object of type JParameter.

For example, suppose your template has a parameter called templateColour which takes various string values which determine the colour scheme to be used. The colour schemes themselves are defined in CSS files which include the colour name as part of their file names. The following code retrieves the parameter then adds the appropriate stylesheet to render the page in the required colour scheme.

$tplColour = $this->params->get( 'templateColour' );
$this->addStyleSheet( $this->baseurl  . '/templates/'
                    . $this->template . '/css/' . $tplColour .'.css' );

For another example, suppose your template has a parameter called authorCopyright which takes the value 0 to hide a copyright notice, or 1 to show it. The following code retrieves the parameter then outputs the copyright notice within a conditional PHP statement:

<?php if ($this->params->get( 'authorCopyright' )) : ?>   
     <div class="copyright">
         Copyright &copy; 2008 Fat Pigeon Templates
<?php endif; ?>