Actions

J1.5

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

From Joomla! Documentation

m (1 revision(s))
Line 1: Line 1:
===== Retrieving parameter data in a template file =====
+
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.
 
+
To retrieve a Parameter in your template file use the function call
+
  
 +
To retrieve the value of a parameter in your template code use the function call:
 
<source lang="php">
 
<source lang="php">
<?php  
+
<?php $myParam = $this->params->get( 'parameterName' ); ?>
$myParam = $this->params->get( 'parameterName' );  
+
?>
+
 
</source>
 
</source>
  
For example, retrieve the two Parameters we set on the page [[Defining a parameter in templateDetails.xml|Defining a parameter in templateDetails.xml]]
+
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 the ''templateColor'' Parameter we have set the option values blue, red, green and black, this values refers to a CSS file. So we set a link to the color CSS file.
+
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.
 
<source lang="php">
 
<source lang="php">
 
<?php
 
<?php
$tplColor = $this->params->get( 'templateColor' );
+
$tplColour = $this->params->get( 'templateColour' );
$this->addStyleSheet( $this->baseurl  .'/templates/'. $this->template .'/css/'. $tplColor .'.css' );
+
$this->addStyleSheet( $this->baseurl  . '/templates/'
 +
                    . $this->template . '/css/' . $tplColour .'.css' );
 
?>
 
?>
 
</source>
 
</source>
  
For the ''authorCopyright'' Parameter we have set the option values 0 and 1 to hide or show a copyright notice for i.e. on the end of the template.
+
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:
 
+
 
<source lang="php">
 
<source lang="php">
 
<?php if ($this->params->get( 'authorCopyright' )) : ?>   
 
<?php if ($this->params->get( 'authorCopyright' )) : ?>   
 
     <div class="copyright">
 
     <div class="copyright">
         Your code...
+
         Copyright &copy; 2008 Fat Pigeon Templates
 
     </div>
 
     </div>
 
<?php endif; ?>
 
<?php endif; ?>
 
</source>
 
</source>
 
<noinclude>[[Category:Intermediate]][[Category:Templates]][[Category:Topics]]</noinclude>
 
<noinclude>[[Category:Intermediate]][[Category:Templates]][[Category:Topics]]</noinclude>

Revision as of 14:36, 16 August 2008

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 see 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; ?>