bsesic
bsesic

Reputation: 609

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?

Upvotes: 2

Views: 958

Answers (2)

user
user

Reputation: 25728

You can {capture} a block to a variable and then use PHP's strip_tags() on it:

{capture name="withtags"}
    <em>Text</em> with <strong>tags</strong>
{/capture}
{$smarty.capture.withtags|strip_tags}

Upvotes: 0

pp19dd
pp19dd

Reputation: 3633

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.

Upvotes: 2

Related Questions