xeoncross / PHP-Template

Simple PHP template inheritance and view management class using pure PHP
28 stars 4 forks source link

filters for method end() #3

Closed raxan closed 9 years ago

raxan commented 9 years ago

Hi is not mentiond in docs third params "filters" for method end()

end($name, $keep_parent) end a template block and save

how to use it and what is purpose of this ?

Thanks!!

xeoncross commented 9 years ago

The filter functions are an array of closures to call.

https://github.com/Xeoncross/PHP-Template/blob/master/Template.php#L130

So you would do something like

<?php 
$this->end(
    'content',
    true,
    array('trim', 'strtolower', 'yourfunctionhere', function($buffer) { return 'foo' . $buffer; }, etc.... )
);
?>
raxan commented 9 years ago

Hi

thanks for your reply.

not sure if correctly understand.

Let me repeat, this array of function are all called before output? and receive as param the content of block?

So I can so something like:

<?php $this->end( 'content', true, array(function($buffer) { return strip_tags($buffer); }) ); ?>

which will remove html tags from content?

cool !

raxan commented 9 years ago

can this be used for create like wordpress shortcodes?

xeoncross commented 9 years ago

Yes, you can make shortcodes. Assuming they look like [yourwordhere]...

<?php 
$this->end(
    'content',
    true,
    array(function($buffer) {
        if(preg_match('~\[\(w+)\]~', $buffer, $match)) { print_r($match); }
    } )
);
?>
raxan commented 9 years ago

Great! Thanks! I will play with it.