4 - Using Conditional Blocks or Tokens
The following is an example of a template file
with conditional blocks and simple conditional token :
file 'model-condition.htm' :
<html>
<head>
<title>PclTemplate - Template Sample File - <!--(page_name)--></title>
</head>
<body>
Hello <!--(user_name)--> !
<br>
<!--(if:web_site)-->
His web site is : <!--(web_site)-->
<!--(endif)-->
<br>
<!--(if:email_block)-->
His email is : <a href="mailto:<!--(email)-->"><!--(email)--></a>
(alternative is <!--(alternate)-->)
<!--(endif)-->
<br>
<!--(ifnot:not_here)-->
Nothing to add !
<!--(endifnot)-->
</body>
</html>
Notes :
- Inside a conditional block a token can be used several time.
- A conditional block allow you to have several values or other
template blocks inside the block.
- A simple conditional token is in fact a conditional block
with only a simple token inside with same name.
The script to generate the page from the template
will be :
file 'sample-condition.php' :
require_once('pcltemplate.class.php');
// ----- Create the template object
$v_template = new PclTemplate();
// ----- Parse the template file
$v_template->parseFile('model-condition.htm');
// ----- Prepare data
$v_att = array();
// ----- Set the values of the simple tokens
$v_att['page_name'] = 'First Generated Page';
$v_att['user_name'] = 'Vincent Blavet';
// ----- Set the values of the simple token condition
$v_att['web_site'] = 'www.phpconcept.net';
// ----- Set the values of the bloc condition
$v_att['email_block']['email'] = 'vincent@phpconcept.net';
$v_att['email_block']['alternate'] = 'support@phpconcept.net';
// ----- Generate result in a string
$v_result = $v_template->generate($v_att, 'string');
// ----- Display result
echo $v_result;
The result will be :
|
Hello Vincent Blavet !
His web site is : www.phpconcept.net
His email is : vincent@phpconcept.net
(alternative is support@phpconcept.net)
|
5 - Including files or templates
The following is an example of a template file
which also the inclusion of an other template :
file 'model-inclusion.htm' :
<html>
<head>
<title>PclTemplate - Template Sample File - <!--(page_name)--></title>
</head>
<body>
Hello <!--(first_name)--> <!--(last_name)--> !
<br>
<!--(include:footer)-->
</body>
</html>
file 'model-footer.htm' :
<br>
<hr>
<!--(token:copyright)--> - <!--(token:author)-->
<br>
The script to generate the page from the template
will be :
file 'sample-inclusion.php' :
require_once('pcltemplate.class.php');
// ----- Create the template object
$v_template = new PclTemplate();
// ----- Parse the template file
if ($v_template->parseFile('model-inclusion.htm') != PCL_TEMPLATE_ERR_NO_ERROR) {
echo "Error parsing file :<br>";
echo nl2br($v_template->errorInfo());
exit;
}
// ----- Prepare data
$v_att = array();
// ----- Set the values of the simple tokens
$v_att['page_name'] = 'Sample Inclusion';
$v_att['last_name'] = 'Blavet';
$v_att['first_name'] = 'Vincent';
// ----- Include token
// The token is not a single value but an array with :
// - The filename to include ('filename').
// - The values of the tokens for the included file ('values').
$v_att['footer']['filename'] = 'model-footer.htm';
$v_att['footer']['values']['copyright'] = 'Copyright PhpConcept';
$v_att['footer']['values']['author'] = 'vblavet';
// ----- Generate result in a string
$v_result = $v_template->generate($v_att, 'string');
// ----- Display result
echo $v_result;
The result will be :
|
Hello Vincent Blavet !
Copyright PhpConcept - vblavet
|