You are here: WikiStart/Docs/Variables/DotSyntax


Accessing variables from other templates

There are two situations, in which you may want to access the variables from any other template:

  1. Display the value of the variable
  2. Use the value as a condition for a condition template

In both situations, you can use the dot-syntax to solve the problem.

Importing variables from other templates

To import one variable from a different template into the current template, you must use the <patTemplate:var/> tag with the copyFrom attribute:

PHP-Code:

$tmpl = new patTemplate();
$tmpl->readTemplatesFromInput('myPage.tmpl');
$tmpl->addVar('header', 'TITLE', 'Page title');
$tmpl->displayParsedTemplate();

Template:

<patTemplate:tmpl name="page">
  <patTemplate:tmpl name="header">
  <head>
    <title>{TITLE}</title>
  </head>
  </patTemplate:tmpl>

  <patTemplate:tmpl name="footer">  
    <div>
      <small><patTemplate:var name="copiedVar" copyFrom="header.TITLE"/></small>
    </div>
  </patTemplate:tmpl>
</patTemplate:tmpl>

In the above example, we are importing the variable TITLE from the template header into the template footer and name the imported value copiedVar. We could also apply a variable modifier to the copied variable. Since revision [437] it is also possible to use __parent.VARNAME to fetch any variable from the parent template without specifying the name of the template.

Using the dot-syntax in conditions

When creating a condition template you may use the dot-syntax in the conditionVar attribute:

<patTemplate:tmpl name="page">
  <patTemplate:tmpl name="header">
  <head>
    <title>{TITLE}</title>
  </head>
  </patTemplate:tmpl>

  <patTemplate:tmpl name="footer" type="condition" conditionVar="header.TITLE">
    <patTemplate:sub condition="Homepage">
      Display on the homepage.
    </patTemplate:sub>
    <patTemplate:sub condition="__default">
      Display on all other pages.
    </patTemplate:sub>
  </patTemplate:tmpl>
</patTemplate:tmpl>

The output of the footer template will now depend on the value you assign to the variable TITLE of the header template.