J1.5

Difference between revisions of "Retrieving parameter data in a template file"

From Joomla! Documentation

m (archiving articles with category removal and addition)
 
(2 intermediate revisions by 2 users not shown)
Line 1: Line 1:
Having defined a parameter in the ''templateDetails.xml'' file and saved a value for it in the ''params.ini'' file, it remains to see how to retrieve the current value of the parameter so it can be used in the template code.
+
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:
 
To retrieve the value of a parameter in your template code use the function call:
Line 25: Line 25:
 
<?php endif; ?>
 
<?php endif; ?>
 
</source>
 
</source>
<noinclude>[[Category:Intermediate]][[Category:Templates]][[Category:Topics]]</noinclude>
+
<noinclude></noinclude>
 +
[[Category:Archived version Joomla! 1.5|{{PAGENAME}}]]

Latest revision as of 08:40, 30 April 2013

The "J1.5" namespace is an archived namespace. This page contains information for a Joomla! version which is no longer supported. It exists only as a historical reference, it will not be improved and its content may be incomplete and/or contain broken links.

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.

<?php
$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
     </div>
<?php endif; ?>