| 1 |
package Text::Hogan::Template; |
| 2 |
$Text::Hogan::Template::VERSION = '2.04'; |
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
|
| 6 |
use Clone qw(clone); |
| 7 |
use HTML::Escape qw(escape_html); |
| 8 |
use Ref::Util qw( is_ref is_arrayref is_coderef is_hashref); |
| 9 |
use Scalar::Util qw(looks_like_number); |
| 10 |
|
| 11 |
sub new { |
| 12 |
my ($orig, $code_obj, $text, $compiler, $options) = @_; |
| 13 |
|
| 14 |
$code_obj ||= {}; |
| 15 |
|
| 16 |
return bless { |
| 17 |
r => $code_obj->{'code'} || (is_hashref($orig) && $orig->{'r'}), |
| 18 |
c => $compiler, |
| 19 |
buf => "", |
| 20 |
options => $options || {}, |
| 21 |
text => $text || "", |
| 22 |
partials => $code_obj->{'partials'} || {}, |
| 23 |
subs => $code_obj->{'subs'} || {}, |
| 24 |
numeric_string_as_string => !!$options->{'numeric_string_as_string'}, |
| 25 |
}, ref($orig) || $orig; |
| 26 |
} |
| 27 |
|
| 28 |
sub r { |
| 29 |
my ($self, $context, $partials, $indent) = @_; |
| 30 |
|
| 31 |
if ($self->{'r'}) { |
| 32 |
return $self->{'r'}->($self, $context, $partials, $indent); |
| 33 |
} |
| 34 |
|
| 35 |
return ""; |
| 36 |
} |
| 37 |
|
| 38 |
sub v { |
| 39 |
my ($self, $str) = @_; |
| 40 |
return escape_html($str // ""); |
| 41 |
} |
| 42 |
|
| 43 |
sub t { |
| 44 |
my ($self, $str) = @_; |
| 45 |
return defined($str) ? $str : ""; |
| 46 |
} |
| 47 |
|
| 48 |
sub render { |
| 49 |
my ($self, $context, $partials, $indent) = @_; |
| 50 |
return $self->ri([ $context ], $partials || {}, $indent); |
| 51 |
} |
| 52 |
|
| 53 |
sub ri { |
| 54 |
my ($self, $context, $partials, $indent) = @_; |
| 55 |
return $self->r($context, $partials, $indent); |
| 56 |
} |
| 57 |
|
| 58 |
sub ep { |
| 59 |
my ($self, $symbol, $partials) = @_; |
| 60 |
my $partial = $self->{'partials'}{$symbol}; |
| 61 |
|
| 62 |
# check to see that if we've instantiated this partial before |
| 63 |
my $template = $partials->{$partial->{'name'}}; |
| 64 |
if ($partial->{'instance'} && $partial->{'base'} eq $template) { |
| 65 |
return $partial->{'instance'}; |
| 66 |
} |
| 67 |
|
| 68 |
if (! is_ref($template) ) { |
| 69 |
die "No compiler available" unless $self->{'c'}; |
| 70 |
|
| 71 |
$template = $self->{'c'}->compile($template, $self->{'options'}); |
| 72 |
} |
| 73 |
|
| 74 |
return undef unless $template; |
| 75 |
|
| 76 |
$self->{'partials'}{$symbol}{'base'} = $template; |
| 77 |
|
| 78 |
if ($partial->{'subs'}) { |
| 79 |
# make sure we consider parent template now |
| 80 |
$partials->{'stack_text'} ||= {}; |
| 81 |
|
| 82 |
for my $key (sort keys %{ $partial->{'subs'} }) { |
| 83 |
if (!$partials->{'stack_text'}{$key}) { |
| 84 |
$partials->{'stack_text'}{$key} = |
| 85 |
$self->{'active_sub'} && $partials->{'stack_text'}{$self->{'active_sub'}} |
| 86 |
? $partials->{'stack_text'}{$self->{'active_sub'}} |
| 87 |
: $self->{'text'}; |
| 88 |
} |
| 89 |
} |
| 90 |
$template = create_specialized_partial($template, $partial->{'subs'}, $partial->{'partials'}, $self->{'stack_subs'}, $self->{'stack_partials'}, $self->{'stack_text'}); |
| 91 |
} |
| 92 |
$self->{'partials'}{$symbol}{'instance'} = $template; |
| 93 |
|
| 94 |
return $template; |
| 95 |
} |
| 96 |
|
| 97 |
# tries to find a partial in the current scope and render it |
| 98 |
sub rp { |
| 99 |
my ($self, $symbol, $context, $partials, $indent) = @_; |
| 100 |
|
| 101 |
my $partial = $self->ep($symbol, $partials) or return ""; |
| 102 |
|
| 103 |
return $partial->ri($context, $partials, $indent); |
| 104 |
} |
| 105 |
|
| 106 |
# render a section |
| 107 |
sub rs { |
| 108 |
my ($self, $context, $partials, $section) = @_; |
| 109 |
my $tail = $context->[-1]; |
| 110 |
if (! is_arrayref($tail) ) { |
| 111 |
$section->($context, $partials, $self); |
| 112 |
return; |
| 113 |
} |
| 114 |
|
| 115 |
for my $t (@$tail) { |
| 116 |
push @$context, $t; |
| 117 |
$section->($context, $partials, $self); |
| 118 |
pop @$context; |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
# maybe start a section |
| 123 |
sub s { |
| 124 |
my ($self, $val, $ctx, $partials, $inverted, $start, $end, $tags) = @_; |
| 125 |
my $pass; |
| 126 |
|
| 127 |
return 0 if (is_arrayref($val)) && !@$val; |
| 128 |
|
| 129 |
if (is_coderef($val)) { |
| 130 |
$val = $self->ms($val, $ctx, $partials, $inverted, $start, $end, $tags); |
| 131 |
} |
| 132 |
|
| 133 |
$pass = !!$val; |
| 134 |
|
| 135 |
if (!$inverted && $pass && $ctx) { |
| 136 |
push @$ctx, (is_arrayref($val) || is_hashref($val)) ? $val : $ctx->[-1]; |
| 137 |
} |
| 138 |
|
| 139 |
return $pass; |
| 140 |
} |
| 141 |
|
| 142 |
# find values with dotted names |
| 143 |
sub d { |
| 144 |
my ($self, $key, $ctx, $partials, $return_found) = @_; |
| 145 |
my $found; |
| 146 |
|
| 147 |
# JavaScript split is super weird!! |
| 148 |
# |
| 149 |
# GOOD: |
| 150 |
# > "a.b.c".split(".") |
| 151 |
# [ 'a', 'b', 'c' ] |
| 152 |
# |
| 153 |
# BAD: |
| 154 |
# > ".".split(".") |
| 155 |
# [ '', '' ] |
| 156 |
# |
| 157 |
my @names = $key eq '.' ? ( '' ) x 2 : split /\./, $key; |
| 158 |
|
| 159 |
my $val = $self->f($names[0], $ctx, $partials, $return_found); |
| 160 |
|
| 161 |
my $cx; |
| 162 |
|
| 163 |
if ($key eq '.' && is_arrayref($ctx->[-2])) { |
| 164 |
$val = $ctx->[-1]; |
| 165 |
} |
| 166 |
else { |
| 167 |
for my $name (@names[1..$#names] ) { |
| 168 |
$found = find_in_scope($name, $val); |
| 169 |
if (defined $found) { |
| 170 |
$cx = $val; |
| 171 |
$val = $found; |
| 172 |
} |
| 173 |
else { |
| 174 |
$val = ""; |
| 175 |
} |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
return 0 if $return_found && !$val; |
| 180 |
|
| 181 |
if (!$return_found && is_coderef($val)) { |
| 182 |
push @$ctx, $cx; |
| 183 |
$val = $self->mv($val, $ctx, $partials); |
| 184 |
pop @$ctx; |
| 185 |
} |
| 186 |
|
| 187 |
return $self->_check_for_num($val); |
| 188 |
} |
| 189 |
|
| 190 |
# handle numerical interpolation for decimal numbers "properly"... |
| 191 |
# |
| 192 |
# according to the mustache spec 1.210 should render as 1.21 |
| 193 |
# |
| 194 |
# unless the optional numeric_string_as_string was passed |
| 195 |
sub _check_for_num { |
| 196 |
my $self = shift; |
| 197 |
my $val = shift; |
| 198 |
return $val if ($self->{'numeric_string_as_string'} == 1); |
| 199 |
|
| 200 |
$val += 0 if looks_like_number($val); |
| 201 |
|
| 202 |
return $val; |
| 203 |
} |
| 204 |
|
| 205 |
# find values with normal names |
| 206 |
sub f { |
| 207 |
my ($self, $key, $ctx, $partials, $return_found) = @_; |
| 208 |
my ( $val, $found ) = ( 0 ); |
| 209 |
|
| 210 |
for my $v ( reverse @$ctx ) { |
| 211 |
$val = find_in_scope($key, $v); |
| 212 |
|
| 213 |
next unless defined $val; |
| 214 |
|
| 215 |
$found = 1; |
| 216 |
last; |
| 217 |
} |
| 218 |
|
| 219 |
return $return_found ? 0 : "" unless $found; |
| 220 |
|
| 221 |
if (!$return_found && is_coderef($val)) { |
| 222 |
$val = $self->mv($val, $ctx, $partials); |
| 223 |
} |
| 224 |
|
| 225 |
return $self->_check_for_num($val); |
| 226 |
} |
| 227 |
|
| 228 |
# higher order templates |
| 229 |
sub ls { |
| 230 |
my ($self, $func, $cx, $ctx, $partials, $text, $tags) = @_; |
| 231 |
my $old_tags = $self->{'options'}{'delimiters'}; |
| 232 |
|
| 233 |
$self->{'options'}{'delimiters'} = $tags; |
| 234 |
$self->b($self->ct($func->($text), $cx, $partials)); |
| 235 |
$self->{'options'}{'delimiters'} = $old_tags; |
| 236 |
|
| 237 |
return 0; |
| 238 |
} |
| 239 |
|
| 240 |
# compile text |
| 241 |
sub ct { |
| 242 |
my ($self, $text, $cx, $partials) = @_; |
| 243 |
|
| 244 |
die "Lambda features disabled" |
| 245 |
if $self->{'options'}{'disable_lambda'}; |
| 246 |
|
| 247 |
return $self->{'c'}->compile($text, $self->{'options'})->render($cx, $partials); |
| 248 |
} |
| 249 |
|
| 250 |
# template result buffering |
| 251 |
sub b { |
| 252 |
my ($self, $s) = @_; |
| 253 |
$self->{'buf'} .= $s; |
| 254 |
} |
| 255 |
|
| 256 |
sub fl { |
| 257 |
my ($self) = @_; |
| 258 |
my $r = $self->{'buf'}; |
| 259 |
$self->{'buf'} = ""; |
| 260 |
return $r; |
| 261 |
} |
| 262 |
|
| 263 |
# method replace section |
| 264 |
sub ms { |
| 265 |
my ($self, $func, $ctx, $partials, $inverted, $start, $end, $tags) = @_; |
| 266 |
|
| 267 |
return 1 if $inverted; |
| 268 |
|
| 269 |
my $text_source = ($self->{'active_sub'} && $self->{'subs_text'} && $self->{'subs_text'}{$self->{'active_sub'}}) |
| 270 |
? $self->{'subs_text'}{$self->{'active_sub'}} |
| 271 |
: $self->{'text'}; |
| 272 |
|
| 273 |
my $s = substr($text_source,$start,($end-$start)); |
| 274 |
|
| 275 |
$self->ls($func, $ctx->[-1], $ctx, $partials, $s, $tags); |
| 276 |
|
| 277 |
return 0; |
| 278 |
} |
| 279 |
|
| 280 |
# method replace variable |
| 281 |
sub mv { |
| 282 |
my ($self, $func, $ctx, $partials) = @_; |
| 283 |
my $cx = $ctx->[-1]; |
| 284 |
my $result = $func->($self,$cx); |
| 285 |
|
| 286 |
return $self->ct($result, $cx, $partials); |
| 287 |
} |
| 288 |
|
| 289 |
sub sub { |
| 290 |
my ($self, $name, $context, $partials, $indent) = @_; |
| 291 |
my $f = $self->{'subs'}{$name} or return; |
| 292 |
|
| 293 |
$self->{'active_sub'} = $name; |
| 294 |
$f->($context,$partials,$self,$indent); |
| 295 |
$self->{'active_sub'} = 0; |
| 296 |
} |
| 297 |
|
| 298 |
################################################ |
| 299 |
|
| 300 |
sub find_in_scope { |
| 301 |
my ($key, $scope) = @_; |
| 302 |
|
| 303 |
return is_hashref($scope) ? $scope->{$key} : undef; |
| 304 |
} |
| 305 |
|
| 306 |
sub create_specialized_partial { |
| 307 |
my ($instance, $subs, $partials, $stack_subs, $stack_partials, $stack_text) = @_; |
| 308 |
|
| 309 |
my $Partial = clone($instance); |
| 310 |
$Partial->{'buf'} = ""; |
| 311 |
|
| 312 |
$stack_subs ||= {}; |
| 313 |
$Partial->{'stack_subs'} = $stack_subs; |
| 314 |
$Partial->{'subs_text'} = $stack_text; |
| 315 |
|
| 316 |
for my $key (sort keys %$subs) { |
| 317 |
if (!$stack_subs->{$key}) { |
| 318 |
$stack_subs->{$key} = $subs->{$key}; |
| 319 |
} |
| 320 |
} |
| 321 |
for my $key (sort keys %$stack_subs) { |
| 322 |
$Partial->{'subs'}{$key} = $stack_subs->{$key}; |
| 323 |
} |
| 324 |
|
| 325 |
$stack_partials ||= {}; |
| 326 |
$Partial->{'stack_partials'} = $stack_partials; |
| 327 |
|
| 328 |
for my $key (sort keys %$partials) { |
| 329 |
if (!$stack_partials->{$key}) { |
| 330 |
$stack_partials->{$key} = $partials->{$key}; |
| 331 |
} |
| 332 |
} |
| 333 |
for my $key (sort keys %$stack_partials) { |
| 334 |
$Partial->{'partials'}{$key} = $stack_partials->{$key}; |
| 335 |
} |
| 336 |
|
| 337 |
return $Partial; |
| 338 |
} |
| 339 |
|
| 340 |
|
| 341 |
sub coerce_to_string { |
| 342 |
my ($str) = @_; |
| 343 |
return defined($str) ? $str : ""; |
| 344 |
} |
| 345 |
|
| 346 |
1; |
| 347 |
|
| 348 |
__END__ |
| 349 |
|
| 350 |
=head1 NAME |
| 351 |
|
| 352 |
Text::Hogan::Template - represent and render compiled templates |
| 353 |
|
| 354 |
=head1 VERSION |
| 355 |
|
| 356 |
version 2.04 |
| 357 |
|
| 358 |
=head1 SYNOPSIS |
| 359 |
|
| 360 |
Use Text::Hogan::Compiler to create Template objects. |
| 361 |
|
| 362 |
Then call render passing in a hashref for context. |
| 363 |
|
| 364 |
use Text::Hogan::Compiler; |
| 365 |
|
| 366 |
my $template = Text::Hogan::Compiler->new->compile("Hello, {{name}}!"); |
| 367 |
|
| 368 |
say $template->render({ name => $_ }) for (qw(Fred Wilma Barney Betty)); |
| 369 |
|
| 370 |
Optionally takes a hashref of partials. |
| 371 |
|
| 372 |
use Text::Hogan::Compiler; |
| 373 |
|
| 374 |
my $template = Text::Hogan::Compiler->new->compile("{{>hello}}"); |
| 375 |
|
| 376 |
say $template->render({ name => "Dino" }, { hello => "Hello, {{name}}!" }); |
| 377 |
|
| 378 |
=head1 AUTHORS |
| 379 |
|
| 380 |
Started out statement-for-statement copied from hogan.js by Twitter! |
| 381 |
|
| 382 |
Initial translation by Alex Balhatchet ([email protected]) |
| 383 |
|
| 384 |
Further improvements from: |
| 385 |
|
| 386 |
Ed Freyfogle |
| 387 |
Mohammad S Anwar |
| 388 |
Ricky Morse |
| 389 |
Jerrad Pierce |
| 390 |
Tom Hukins |
| 391 |
Tony Finch |
| 392 |
Yanick Champoux |
| 393 |
|
| 394 |
=cut |