Beilpuz template examples

All Examples are included in the Beilpuz archive.

Basics

Include other templates

main php file

include_once('../Beilpuz.php');
$template = new BpTemplate('static main document.html');
echo $template->render();

templates/static main document.html

<html>
<bp:include template="static title.html"/>
<body>Im the main document. My title was declared by template 'static title.html'.
</body></html>

templates/static title.html

<title>Im a static title</title>

Render arrays and objects

main php file

include('../Beilpuz.php');
$template = new BpTemplate('arrays and objects.html');
$template->setValue('animals', Array('Katze', 'Affe', 'Kuh'));
$template->setValue('entry', new Entry);
echo $template->render();
 
class Entry {
	public function getNow() {
		return time();
	}
	public $nr = 3;
	public $array = Array('Eins','Zwei','Drei','Vier');
}

templates/array_values.html

<html><body>
First animal: <bp:$animals[0]/><br>
All Animals:<br>
<ul>
<bp:foreach in="animals" as="test" index="number" direct="true">
	<li><bp:$number/>. <bp:$test/></li>
</bp:foreach>
</ul>
Object:
<ul>
<li><bp:$entry.getNow()/></li>
<li><bp:$entry.nr/></li>
<li><bp:$entry.array[3]/></li>
</ul>
</body></html>

Write your own signatures

main php file

include('../Beilpuz.php');
$template = new BpTemplate('own signatures.html');
echo $template->render();

templates/own signatures.html

<html><body>
Im custom signature:<br>
<hr><bp:hello name="Welt" use="include/hello_signature"/>
<hr><bp:hello name="Dame" direct="true"/>
</body></html>

include/hello_signature.php

function bp_hello($sig, $template) {
	if ($sig->getAttribute('direct') === true) {
		return Beilpuz::PHP_BEGIN . ' echo \'direct: Hallo liebe ' . $sig->getAttribute('name') . '\'' . Beilpuz::PHP_END;
	} else {
		return 'Hallo liebe ' . $sig->getAttribute('name') . '<br>';
	}
}

Share values between templates

main php file

include('../Beilpuz.php');
$template = new BpTemplate('shared values.html');
echo $template->render();

templates/shared values.html

<html>
<bp:include template="shared title.html" use="include/set_value" shared_values="true"/>
<body>
Im a shared value, defined from my title template:
<bp:$tellmesomething/>
</body></html>

templates/shared title.html

<bp:set_value/>
<title><bp:$tellmesomething/></title>

include/set_value.php

function bp_set_value($sig, $template) {
	$template->setValue('tellmesomething', 'Nachts ist es k&auml;lter als drau&szlig;en');
}

Signatures with attributes

main php file

include('../Beilpuz.php');
$template = new BpTemplate('signature attributes.html');
echo $template->render();

templates/signature attributes.html

<html><body>
A div from signature with attributes:
<bp:draw_content_div width="300" height="200" color="green" border="true" use="include/draw_div"/>
</body></html>

include/draw_div.php

function bp_draw_content_div($sig, $template) {
	$content = '';
	if ($sig->tpl !== null) {
		$content = $sig->tpl->render();
	}
	$render = '<div style="width:' . $sig->getAttribute('width') . 'px;height:'.$sig->getAttribute('height').'px;background-color:' . $sig->getAttribute('color');
	if ($sig->getAttribute('border') === true) $render .= ';border-width:2px;border-color:#000000;border-style:solid;';
 	$render .= '">' . $content . '</div>';
	$sig->setAttribute('done', true);
	return $render;
}

Caching a template

main php file

include('../Beilpuz.php');
Beilpuz::allowCacheKey('dummy');
$template = new BpTemplate('cached template.html');
echo $template->render();

templates/cached template.html

<html><bp:include template="static title.html" cacheable="true"/>
<body>
Im a cached template, created at: <bp:current_time cacheable="true"/><br>
You have to change my master template or delete me for recreation.<br><br>
Im a non-cached signature: <bp:current_time use="include/current_time"/>
</body></html>

include/current_time.php

function bp_current_time($sig) {
	return date('r');
}

Signatures with content

main php file

function getmicrotime() {
    list($usec, $sec) = explode(" ",microtime());
    return ((float)$usec + (float)$sec);
}
$start_time = getmicrotime();
include('../Beilpuz.php');
$template = new BpTemplate('signature content.html');
echo $template->render();
echo '<font size=1>time:' . (getmicrotime() - $start_time).'</font>';

templates/signature content.html

<html><body>
A signature with content:
<bp:draw_content_div width="500" height="300" color="red" border="false" use="include/draw_div">
	<bp:draw_content_div width="400" height="260" color="yellow" border="false">
		Im content:
		<bp:draw_content_div width="10" height="10" color="green" border="true"/>
		<bp:draw_content_div width="10" height="10" color="green" border="true"/>
		<bp:draw_content_div width="10" height="10" color="green" border="true"/>
		<bp:draw_content_div width="10" height="10" color="green" border="true"/>
		End
	</bp:draw_content_div>
	<bp:draw_content_div width="400" height="20" color="blue" border="false">
		hello world
	</bp:draw_content_div>
</bp:draw_content_div>
</body></html>

Conditions

main php file

include('../Beilpuz.php');
$template = new BpTemplate('conditions.html');
$template->setValue('headline','Hello');
$template->setValue('subject','World');
echo $template->render();
$template->setValue('subject','Kitty');
echo $template->render();

templates/conditions.html

<bp:if value="headline">
	<h1><bp:$headline/></h1>
	<bp:if value="subject" is="World">
		<strong><bp:$subject/></strong>
	</bp:if>
	<bp:if value="subject" is="Kitty">
		<font color="#FFAACC"><bp:$subject/></font>
	</bp:if>
</bp:if>

Advanced

Decide caching

main php file

function getmicrotime() {
	list($usec, $sec) = explode(" ",microtime());
	return ((float)$usec + (float)$sec);
}
include('../Beilpuz.php');
$start_time = getmicrotime();
Beilpuz::enableCachingWithKeys($_GET);
$template = new BpTemplate('cache range.html');
echo $template->render();
echo '<font size=1>time:' . (getmicrotime() - $start_time).'</font>';

templates/cache range.html

<html>
<title><bp:print_title cacheable="true" use="include/check_range"/></title>
<body>
<h3><bp:value name="number" cacheable="true"/></h3>
<bp:check_range cacheable="true" use="include/current_time" >
	<b>Im in the allowed range of cacheable templates.</b><br>
	I was created at: <bp:current_time/>
</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>

include/check_range.php

function bp_print_title($sig,$template) {
	$number = intval($_GET['number']);
	if ($number < 1) $number = 0;
	$template->setValue('number', $number);
	return 'Page: ' . $number;
}
 
function bp_check_range($sig,$template) {
	$number = $template->getValue('number');
	if ($number >= 20 && $number <= 40) {
		Beilpuz::allowCacheKey('number');
		return $sig->tpl->render();
	}
}

Custom delimiters

main php file

include('../../Beilpuz.php');
Beilpuz::$templates = '../templates';
Beilpuz::$compiled = '../compiled';
Beilpuz::setDelimiter('{', '}');
$template = new BpTemplate('delimiter.html');
echo $template->render();
function bp_delimiter($sig,$tpl) {
	$sig->tpl->setValue('delimiter', Beilpuz::$SIG_BEGIN . Beilpuz::$SIG_END . ' / ' . Beilpuz::$SIG_CLOSE.Beilpuz::$SIG_END);
	return $sig->tpl->render();
}

templates/delimiter.html

<html><head><title>Custom delimiters</title></head>
<body>
{bp:delimiter}
My delimiters are now: {bp:$delimiter}
{/bp:delimiter}<br>
</body></html>

 
bp0.2/examples.txt · Last modified: 2009/06/08 13:41 (external edit)
 
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki