Direct rendering signatures

Signatures with the direct=“true” attribute compiles their functionality as native PHP code directly into the template. Take a look on the difference between direct and non-direct rendered signatures using the foreach example.

Non-direct signature

Template

<ul>
<bp:foreach in="animals" as="test" index="number" >
	<li><bp:$number/>. <bp:$test/></li>
</bp:foreach>
</ul>

Compiled Output

<?php if($s===null){Beilpuz::baseInc();$s=unserialize('a:1:{i:0;O:11:"BpSignature":3:{...}}');}?>
<ul><?php echo bp_foreach($s[0],$this)?></ul>

Direct signature

Template

<ul>
<bp:foreach in="animals" as="test" index="number" direct="true">
	<li><bp:$number/>. <bp:$test/></li>
</bp:foreach>
</ul>

Compiled Output

<ul>
<?php foreach ($this->v['animals'] as $this->v['number'] => $this->v['test']){?>
<li><?php echo $this->v['number']?>. <?php echo $this->v['test']?></li>
<?php }?>
</ul>

Comparison

But what are the (dis)advantages of both methods?

direct non-direct
advantages Fast Caching possible.
Easy to implement.
Advanced conditions at runtime possible.
Dynamic.
disadvantages Difficult to implement.
No caching possible.
No runtime access on signatures instances.
Slower due the decompiling and calling overhead.

Writing own direct signatures

The example Write your own signatures shows, how to determine the direct-flag and return direct signatures.

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>';
	}
}

Prevent using the direct attribute

if ($sig->getAttribute('direct') === true) {
	throw new ErrorException('Direct rendering of ' . $sig->name . ' not implemented.');
}

 
technology/direct_rendering_signatures.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