TracNav menu
The SimpleCondition template
In nearly every application, there will be parts of the HTML frontend, that should only be displayed, if a certain information is available. One example could be a box, that displays information about the currently logged in user. It makes no sense, to display this box, if no user is logged in.
patTemplate helps you achieving this, by providing the simpleCondition template.
Take a look at the following template:
<patTemplate:tmpl name="page">
Here is your main content.
<patTemplate:tmpl name="user-info">
<div id="userInfo">
Logged in as {USER_NAME} ({USER_ID}).
</div>
</patTemplate:tmpl>
</patTemplate:tmpl>
To add user information to this page, you need to use the addVars() method, like the following code snippet shows:
$tmpl = new patTemplate();
$tmpl->readTemplatesFromInput('my-template.tmpl');
if (userIsLoggedIn()) {
$user = array(
'ID' => getUserId(),
'NAME' => getUserName()
);
$tmpl->addVars('user-info', $user, 'USER_');
}
The problem is, that if there is no user logged in, that the HTML code for the user info box will still be displayed. This can easily be changed, by adding two attributes to the <patTemplate:tmpl/> tag:
<patTemplate:tmpl name="page">
Here is your main content.
<patTemplate:tmpl name="user-info" type="simpleCondition" requiredVars="USER_ID">
<div id="userInfo">
Logged in as {USER_NAME} ({USER_ID}).
</div>
</patTemplate:tmpl>
</patTemplate:tmpl>
By setting the type attribute to simpleCondition you are emulating an if-condition. The attribute requiredVars allows you to specify a variable that must be assigned a non-empty value in order for the template to be displayed. By setting this attribute to USER_ID you can force this template to be hidden, if no user-id has been passed.
Specifying more than one variable
It is also possible to specify more than one variable. In this case you have to set the attribute value to a comma-separated list of variable names. If any of the variables has a non-empty value, the template will be displayed:
<patTemplate:tmpl name="page">
Here is your main content.
<patTemplate:tmpl name="user-info" type="simpleCondition" requiredVars="USER_ID,USER_NAME">
<div id="userInfo">
Logged in as {USER_NAME} ({USER_ID}).
</div>
</patTemplate:tmpl>
</patTemplate:tmpl>
In this example, the user-info box will be displayed, if either the username or the userid has been set.
Using global variables
If you want to display the userid and username in different places in your template, you might want to add it globally:
$tmpl = new patTemplate();
$tmpl->readTemplatesFromInput('my-template.tmpl');
if (userIsLoggedIn()) {
$user = array(
'ID' => getUserId(),
'NAME' => getUserName()
);
$tmpl->addGlobalVars($user, 'USER_');
}
By ddefault, the simpleCondition template will check, whether the variable has been asigned locally. This can be changed, by setting the the useGlobals attribute to true:
<patTemplate:tmpl name="page">
Here is your main content.
<patTemplate:tmpl name="user-info" type="simpleCondition" requiredVars="USER_ID" useGlobals="true">
<div id="userInfo">
Logged in as {USER_NAME} ({USER_ID}).
</div>
</patTemplate:tmpl>
</patTemplate:tmpl>
Specifying a required value
It is not only possible to check, whether a variable has a non-empty value, but also to check, whether you assigned a specific value to a variable. Take a look at the following template:
<patTemplate:tmpl name="page">
Here is your main content.
<patTemplate:tmpl name="admin-options">
<div id="userInfo">
Admin-Options:<br/>
- Delete page<br/>
- Edit page<br/>
</div>
</patTemplate:tmpl>
</patTemplate:tmpl>
The template admin-options should only be displayed, if the currently logged in user is an admin. So we change the PHP code, to pass this information to the template globally:
$tmpl = new patTemplate();
$tmpl->readTemplatesFromInput('my-template.tmpl');
if (userIsLoggedIn()) {
$user = array(
'ID' => getUserId(),
'NAME' => getUserName(),
'TYPE' => getUserType()
);
$tmpl->addGlobalVars($user, 'USER_');
}
The getUserType() function should return either admin or user depending on the status of the currently logged in user. So it is not sufficient to only check, whether the USER_TYPE variable is set at all, but you need to check, whether it is set to admin. This can be easily accomplished:
<patTemplate:tmpl name="page">
Here is your main content.
<patTemplate:tmpl name="admin-options" type="simpleCondition" requiredVars="USER_TYPE=admin">
<div id="userInfo">
Admin-Options:<br/>
- Delete page<br/>
- Edit page<br/>
</div>
</patTemplate:tmpl>
</patTemplate:tmpl>
This feature can be combined with the previously discussed feature that allowed you to specify more than one required variable.
