greetings,
I use something similar to filter the HTML altogether. Just for that
purpose.
MACRO escape( text )
BLOCK;
GET text.replace('<(/?)([biau])>', '').replace('<a href=', '').replace('\>',
'');
END;
But I only allow bold, italic, underline, and links, to be there in the
first place.
It's used like so:
[% escape(temp.var) | truncate(50) %]
Also I attempt to filter html out of some sections altogether. However my
regexe isn't the best, so I just filter things I feel could impact page
layout and design.
style, iframes, headers and images....
stripHTML => sub {
(my $r = shift) =~ s/[\r]/<br \/>/g;
$r =~ s/(<[^<|>]*?)(style=".*?")|/$1/g;
$r =~
s/(<[^<|>]*?img.*?\/?>|<[^<|>]*?iframe.*?<\/iframe>|<[^<|>]*?style.*?<\/style>|<h[0-9]>.*?<\/h[0-9]>)*?//gx;
return $r;
},
I pass it along as a temp var. Then use in Template like so:
[% stripHTML(user.input) %]
Hope this helps. :\
PS- Any improvements to my regexe are graciously accepted :)
On Fri, Apr 10, 2009 at 4:38 AM, Nicolas Ramz <[email protected]>wrote:
> Hi,
>
> I have a variable which contains some HTML text which may contain some
> tags, like <strong> or <p> and I'd like to truncate it. The problem is that
> truncate doesn't preserve the HTML tags, so this can break my page, like in
> this example:
>
> $foo = "<p>blablablabla foooo</p>"
>
> in my template:
>
> [% FILTER truncate(10, '...') %]
> [% foo | html %]
> [% END%]
>
> => the <p> isn't closed anymore.
>
> Is there a function that would allow to do that ? Or an easy way to do so ?
>
> Thanks!
>
> Leo.
>
> _______________________________________________
> templates mailing list
> [email protected]
> http://mail.template-toolkit.org/mailman/listinfo/templates
>
>
--
Kelly Thompson
Vice President
Live Audio Magazine Inc.
www.liveaudiomag.com
(919) 389-5784
_______________________________________________
templates mailing list
[email protected]
http://mail.template-toolkit.org/mailman/listinfo/templates