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.
<ul> <bp:foreach in="animals" as="test" index="number" > <li><bp:$number/>. <bp:$test/></li> </bp:foreach> </ul>
<?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>
<ul> <bp:foreach in="animals" as="test" index="number" direct="true"> <li><bp:$number/>. <bp:$test/></li> </bp:foreach> </ul>
<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>
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. |
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>'; } }
if ($sig->getAttribute('direct') === true) { throw new ErrorException('Direct rendering of ' . $sig->name . ' not implemented.'); }