Search code examples
htmltagssmartyblock

Get rid of HTML tags in a block in Smarty


How can I get rid of HTML tags in a Smarty block?
I've tried {strip} within the block and it did not work.
Also I tried to write a plugin. But how can I parse the content of a block into a string variable?


Solution

  • The {strip} block actually does something else - it only strips whitespace off markup.

    You could write a simple block plugin:

    function smarty_block_sanitize($parms, $content, &$smarty, &$repeat ) {
        if( $repeat ) {
            return( strip_tags( $content ) );
        }
    }
    

    Put this somewhere in a PHP file called before displaying the template. To use it in template, do this:

    Sample text {sanitize}<more markup>test</more markup>{/sanitize}  End text.
    

    Beware allowing tags with strip_tags (with its PHP parameter), since onclick / onmouseover / other wicked attributes do not get filtered.