All Examples are included in the Beilpuz archive.
This example shows, how to load a template and include another.
Main file
<?php include_once('../bp/Beilpuz.php'); $template = new BpTemplate('static main document.html'); echo $template->render(); ?>
Template
<html> <bp:include template="static title.html"/> <body>Im the main document. My title was declared by template 'static title.html'. </body></html>
Included template
<title>Im a static title</title>
It is possible to render the inner template of a block in a loop by using the foreach signature.
Main file
<?php include('Profiler.php'); Profiler::start(md5(__FILE__)); include('../bp/Beilpuz.php'); Beilpuz::enableTemporary(); $template = new BpTemplate('arrays and objects.html'); $template->assign('animals', Array( 'livestock' =>Array('cow','pig','chicken'), 'pets' =>Array('cat','hamster','tortoise'))); $template->assign('entry', new Entry); $render = ''; for ($s = 0; $s < 10; ++$s) { $render .= $template->render(); } class Entry { public function getNow() { return time(); } public $nr = 3; public $array = Array('Eins','Zwei','Drei','Vier'); } Profiler::end(); echo $render; ?>
Template
<html><body> All Animals:<br> <ul> <bp:foreach in="animals" as="animal" index="type" > <li><bp:$type/></li> <ul> <bp:foreach in="animal" as="name" index="number" direct="true"> <li><bp:$number/>. <bp:$name/></li> </bp:foreach> </ul> </bp:foreach> </ul> Object: <ul> <li><bp:$entry.getNow()/></li> <li><bp:$entry.nr/></li> <li><bp:$entry.array[3]/></li> </ul> </body></html>
You can share assigned values between templates with the include signature.
Main file
<?php include('../bp/Beilpuz.php'); $template = new BpTemplate('shared values.html'); $template->assign('tellmesomething','Nachts ist es kälter als draußen'); echo $template->render(); ?>
This file includes the title template and shares its values.
Template
<html> <bp:include template="shared title.html" shared_values="true"/> <body> Im a shared value, defined from my title template: <bp:$tellmesomething/> </body></html>
Template
<title><bp:$tellmesomething/></title>
You can specify custom attributes as you like.
Main file
<?php include('../bp/Beilpuz.php'); Beilpuz::addSignatureClassPath('signatures'); $template = new BpTemplate('signature attributes.html'); echo $template->render(); ?>
Template
<html><body> A div from signature with attributes: <bp:draw_div width="300" height="200" color="green" border="true"/> </body></html>
Signatures can contain further template code.
Main file
<?php include('Profiler.php'); Profiler::start(md5(__FILE__)); include('../bp/Beilpuz.php'); Beilpuz::addSignatureClassPath('signatures'); $template = new BpTemplate('signature content.html'); $render = ''; for ($s = 0; $s < 10; ++$s) { $render .= $template->render(); } Profiler::end(); echo $render; ?>
Template
<html><body> A signature with content: <bp:draw_div width="500" height="300" color="red" border="false"> <bp:draw_div width="400" height="260" color="yellow" border="false"> Im content: <bp:draw_div width="10" height="10" color="green" border="true"/> <bp:draw_div width="10" height="10" color="green" border="true"/> <bp:draw_div width="10" height="10" color="green" border="true"/> <bp:draw_div width="10" height="10" color="green" border="true"/> End </bp:draw_div> <bp:draw_div width="400" height="20" color="blue" border="false"> hello world </bp:draw_div> </bp:draw_div> </body></html>
Cache files of templates can be created if they contain cacheable signatures.
Main file
<?php include('Profiler.php'); Profiler::start(md5(__FILE__)); include('../bp/Beilpuz.php'); Beilpuz::addSignatureClassPath('signatures'); Beilpuz::enableCachingWithKeys(array('foo'=>'bar')); Beilpuz::allowCacheKey('foo'); $template = new BpTemplate('cached template.html'); echo $template->render(); echo '<br>'; Profiler::end(); ?>
Template
<html><bp:include template="static title.html" cacheable="true"/> <body> Im a cached template, created at: <bp:now cacheable="true"/><br> You have to change my master template or delete me for recreation.<br><br> Im a non-cached signature: <bp:now/> </body></html>
Very simple conditions with the value and is atributes of signature if, elseif and else.
Main file
<?php include('Profiler.php'); Profiler::start(md5(__FILE__)); include('../bp/Beilpuz.php'); Beilpuz::enableCachingWithKeys(array('foo'=>'bar')); Beilpuz::allowCacheKey('foo'); $template = new BpTemplate('conditions.html'); $template->assign('headline','Hello'); $template->assign('subject','Kitty'); echo $template->render().'<br>'; echo Profiler::end(); ?>
Template
<bp:if value="headline"> <h1><bp:$headline/></h1> <bp:if value="subject" is="World" cacheable="true"> You should not see me. </bp:if> <bp:elseif value="subject" is="Kitty"> <font color="#FFAACC"><bp:$subject/></font> </bp:elseif> <bp:else> Me too. </bp:else> </bp:if>
If you dont like the tag-delimiters, you can choose you own.
Main file
<?php include('../../bp/Beilpuz.php'); include('../../bp/BpSignatureHandler.php'); class Bp_delimiter extends BpSignatureHandler { public static function render($sig,$tpl) { $sig->tpl->assign('delimiters', Beilpuz::$SIG_BEGIN . Beilpuz::$SIG_END . ' / ' . Beilpuz::$SIG_CLOSE.Beilpuz::$SIG_END); return $sig->tpl->render(); } } Beilpuz::$templates = '../templates'; Beilpuz::$compiled = '../compiled'; Beilpuz::setDelimiters('{', '}'); $template = new BpTemplate('delimiters.html'); echo $template->render(); ?>
Template
<html><head><title>Custom delimiters</title></head>
<body>
{bp:delimiter}
My delimiters are now: {bp:$delimiters}
{/bp:delimiter}<br>
</body></html>Writing custom signatures is easy.
Main file
<?php include('../bp/Beilpuz.php'); Beilpuz::addSignatureClassPath('signatures'); $template = new BpTemplate('own signatures.html'); echo $template->render(); ?>
Template
<html><body> I am a custom signature:<br> <hr><bp:hello name="Welt" /> <hr><bp:hello name="Dame" direct="true"/> </body></html>
"Hello"-Signature
<?php class Bp_hello extends BpSignatureHandler { public static $directAllowed = true; public static function direct(BpSignature $sig, BpTemplate $template) { return Beilpuz::PHP_BEGIN . ' echo \'direct: Hallo liebe ' . $sig->name . '\'' . Beilpuz::PHP_END; } public static function render(BpSignature $sig, BpTemplate $template) { return 'Hallo liebe ' . $sig->name . '<br>'; } } ?>
You can decide during runtime if you want to create a cache file of this template.
Main file
<?php include('Profiler.php'); Profiler::start(md5(__FILE__)); include('../bp/Beilpuz.php'); Beilpuz::addSignatureClassPath('signatures'); $start_time = microtime(true); Beilpuz::enableCachingWithKeys(array('number'=>$_GET['number'])); $template = new BpTemplate('cache range.html'); for ($s = 0; $s < 1; ++$s) { echo $template->render(); } Profiler::end(); ?>
Template
<html> <title><bp:print_title cacheable="true" /></title> <body> <h3><bp:value name="number" cacheable="true"/></h3> <bp:check_range cacheable="true" > <b>Im in the allowed range of cacheable templates.</b><br> I was created at: <bp:now/> </bp:check_range> <hr> Caching is only allowed for numbers between 20 and 40.<br> Please enter a number below, to see if its in the range. <form name="form" method="get"> Enter: <input type="text" name="number" value="<bp:value name="number" cacheable="true"/>" size="5"/> Submit: <input type="submit"/> </form> </body> </html>
Signature
<?php class Bp_check_range extends BpSignatureHandler { public static function render(BpSignature $sig, BpTemplate $template) { $number = $template->findValue('number'); if ($number >= 20 && $number <= 40) { Beilpuz::allowCacheKey('number'); return $sig->tpl->render(); } } } ?>
You can write signatures that collect their inline templates for putting them out at another position.
Main file
<?php include('../bp/Beilpuz.php'); Beilpuz::addSignatureClassPath('signatures'); $template = new BpTemplate('collectors.html'); echo $template->render(); ?>
Template
<html>
<bp:collector>
<style>
body {color:red;}
</style>
</bp:collector>
<head><title>Collectors</title>
<bp:collector_output/>
</head>
<body>
<bp:collector>
<script type="text/javascript">
function hello() {
alert('Hello World');
}
</script></bp:collector>
<bp:collector>
<style>
body {background-color:silver}
</style>
</bp:collector>
This example demonstrates collectors.
</body>
<script type="text/javascript">hello();</script>
</html>Collecting signature
<?php class Bp_collector extends BpSignatureHandler { public static $directOnly = true; protected static $collected = array(); public static function direct(BpSignature $sig, BpTemplate $template) { if ($sig->tpl!==null) self::$collected[$template->name][] = $sig->tpl; } } ?>
Output signature
<?php include_once('Bp_collector.php'); class Bp_collector_output extends Bp_collector { public static $directAllowed = false; public static $directOnly = false; public static function render(BpSignature $sig, BpTemplate $template) { $render = ''; $tpls=array(); if (isset($sig->collected)) { $tpls = $sig->collected; //echo '<pre>'.print_r($tpls,true).'</pre>'; } else if (isset(self::$collected[$template->name])) { $sig->collected = self::$collected[$template->name]; $tpls = self::$collected[$template->name]; $template->update = true; } foreach ($tpls as $tpl) { $render .= $tpl->render(); } return $render; } } ?>
You can create extended conditions with the expr attribute of signature if.
Main file
<?php include('Profiler.php'); Profiler::start(md5(__FILE__)); include('../bp/Beilpuz.php'); $template = new BpTemplate('extended conditions.html'); $template->assign('border',5); $template->assign('number',8); $render = ''; for ($s = 0; $s < 10; ++$s) { $render .= $template->render(); } Profiler::end(); echo $render; ?>
Template
<html> <body> <h3>non-direct</h3> <bp:if expr="$number>=$border && $border==5"> Number is: <bp:$number/><br/> Border is: <bp:$border/><br/> </bp:if> <h3>direct</h3> <bp:if expr="$number>=$border && $border==5" direct="true"> Number is: <bp:$number/><br/> Border is: <bp:$border/><br/> </bp:if> </body></html>