Created
June 6, 2011 06:45
-
-
Save hanabokuro/1009831 to your computer and use it in GitHub Desktop.
text::xslate
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use strict; | |
use warnings; | |
use Text::Xslate; | |
use Test::More; | |
my $tx = Text::Xslate->new(cache => 0, | |
function => { | |
custom_raw => sub { Text::Xslate::Util::mark_raw(shift); }, | |
}, | |
); | |
my $tx_with_custom_escape = Text::Xslate->new(cache => 0, | |
function => { | |
custom_raw => sub { Text::Xslate::Util::mark_raw(shift); }, | |
html_escape => \&custom_html_escape, | |
}); | |
is($tx->render_string(q{<: $value | raw :>}, { value => '<div>'}), q{<div>}); | |
is($tx->render_string(q{<: raw($value) :>}, { value => '<div>'}), q{<div>}); | |
is($tx->render_string(q{<: $value | custom_raw :>}, { value => '<div>'}), q{<div>}); | |
is($tx->render_string(q{<: custom_raw($value) :>}, { value => '<div>'}), q{<div>}); | |
is($tx_with_custom_escape->render_string(q{<: $value | raw :>}, { value => '<div>'}), q{<div>}); | |
is($tx_with_custom_escape->render_string(q{<: raw($value) :>}, { value => '<div>'}), q{<div>}); | |
is($tx_with_custom_escape->render_string(q{<: $value | custom_raw :>}, { value => '<div>'}), q{<div>}); | |
is($tx_with_custom_escape->render_string(q{<: custom_raw($value) :>}, { value => '<div>'}), q{<div>}); | |
done_testing(); | |
sub custom_html_escape { | |
my $s = shift; | |
my %h = ( | |
'<' => '<', | |
'>' => '>', | |
q{'} => ''', | |
q{"} => '"', | |
# '&' => '&', # Don't espcae &. allowing to use character-entitfy-references(like '&#hearts) | |
); | |
$s =~ s/(.)/$h{$1} or $1/xmsge; | |
Text::Xslate::Util::mark_raw($s); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment