Skip to content

Instantly share code, notes, and snippets.

@hanabokuro
Created June 6, 2011 06:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hanabokuro/1009831 to your computer and use it in GitHub Desktop.
Save hanabokuro/1009831 to your computer and use it in GitHub Desktop.
text::xslate
#!/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 = (
'<' => '&lt;',
'>' => '&gt;',
q{'} => '&#039;',
q{"} => '&quot;',
# '&' => '&amp;', # 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