Changeset 26781 for lang/perl/Text-MicroTemplate
- Timestamp:
- 12/15/08 18:01:10 (4 years ago)
- Location:
- lang/perl/Text-MicroTemplate/trunk
- Files:
-
- 4 modified
-
lib/Text/MicroTemplate.pm (modified) (9 diffs)
-
t/02-as_html.t (modified) (2 diffs)
-
t/03-modes.t (modified) (2 diffs)
-
t/04-multiline.t (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
lang/perl/Text-MicroTemplate/trunk/lib/Text/MicroTemplate.pm
r26770 r26781 14 14 our $VERSION = '0.01'; 15 15 our @ISA = qw(Exporter); 16 our @EXPORT_OK = qw( as_html encoded_string);16 our @EXPORT_OK = qw(encoded_string build_mt render_mt); 17 17 our %EXPORT_TAGS = ( 18 18 all => [ @EXPORT_OK ], … … 32 32 tag_end => '?>', 33 33 escape_func => 'Text::MicroTemplate::escape_html', 34 ref($_[0]) ? %{$_[0]}: @_,34 @_ == 1 ? ref($_[0]) ? %{$_[0]} : (template => $_[0]) : @_, 35 35 }, $class; 36 36 if (defined $self->{template}) { … … 337 337 } 338 338 339 sub mt_build { 340 my $_mt = Text::MicroTemplate->new( 341 ref($_[0]) ? $_[0] : { template => shift }, 342 ); 339 sub build_mt { 340 my $_mt = Text::MicroTemplate->new(@_); 343 341 my $_code = $_mt->code; 344 342 my $expr = << "..."; … … 361 359 } 362 360 363 sub as_html{364 my $builder = mt_build(shift);361 sub render_mt { 362 my $builder = build_mt(shift); 365 363 $builder->(@_); 366 364 } … … 390 388 =head1 SYNOPSIS 391 389 392 use Text::MicroTemplate qw( as_html);390 use Text::MicroTemplate qw(:all); 393 391 394 392 # simple form 395 $user = 'John'; 396 $html = eval as_html('hello, <?= $user ?>') 397 or die $@; 393 $html = render_mt( 394 'hello, <?= $args->{user} ?>', 395 { user => 'John' }, 396 )->as_string; 397 398 # cache compiled template as subref 399 $renderer = build_mt('hello, <?= $args->{user} ?>'); 400 $html = $renderer->({ user => 'John' })->as_string; 398 401 399 402 # complex form … … 402 405 ); 403 406 $code = $mt->code; 404 $ builder = eval << "..." or die $@;407 $renderer = eval << "..." or die $@; 405 408 sub { 406 409 my \$query = shift; … … 408 411 } 409 412 ... 410 $html = $ builder->(CGI->new);413 $html = $renderer->(CGI->new)->as_string; 411 414 412 415 =head1 DESCRIPTION … … 457 460 =head1 EXPORTABLE FUNCTIONS 458 461 459 =head2 as_html($template) 460 461 Utility funtion that returns an expression that renders given template when evaluated. 462 463 # outputs: hello, John! 464 $user = 'John'; 465 $html = eval as_html('hello, <?= $user ?>!') or die $@; 462 =head2 render_mt($template, $args) 463 464 Utility function that renders given template and returns an EncodedString. 465 466 # render 467 $hello = render_mt( 468 'hello, <?= $args->{user} ?>!', 469 { user => 'John' }, 470 ); 471 472 # print as HTML 473 print $hello->as_string; 474 475 # use the result in another template (no double-escapes) 476 $enc = render_mt( 477 '<h1><?= $args->{hello} ?></h1>', 478 { hello => $hello }, 479 ); 480 481 Intertally, the function is equivalent to: 482 483 build_mt($template)->($args); 484 485 =head2 build_mt($template) 486 487 Returns a subref that renders given template. Parameters are equivalent to Text::MicroTemplate->new. 488 489 # build template renderer at startup time and use it multiple times 490 my $renderer = build_mt('hello, <?= $args->{user} ?>!'); 491 492 sub run { 493 ... 494 my $hello = $renderer->({ user => $query->param('user') }); 495 ... 496 } 466 497 467 498 =head2 encoded_string($str) … … 471 502 =head1 OO-STYLE INTERFACE 472 503 473 Text::MicroTemplate provides OO-style interface to handle more complex cases. The constructor accepts a hash (or a hasref) with following arguments. 474 475 =head2 template 476 477 template 478 479 =head2 escape_func 480 481 name of function used to escape variables. If set to undef, no escape occurs. (default: html escape) 482 483 =head1 OO-STYLE ACCESSORS 504 Text::MicroTemplate provides OO-style interface to handle more complex cases. 505 506 =head2 new($template) 507 508 =head2 new({ template => $template, escape_func => 'escape_funcname' }) 509 510 In the first form, constructs a HTML template renderer. In the second form, constructs a renderer that would escape non-encoded strings using given escape function. If escape function is set to undef, no escape will occur. The default escape function is C<Text::MicroTemplate::escape_html>. 484 511 485 512 =head2 code() -
lang/perl/Text-MicroTemplate/trunk/t/02-as_html.t
r26770 r26781 5 5 6 6 is( 7 as_html('hello <?= $args->{user} ?>', { user => 'fo<o' })->as_string,7 render_mt('hello <?= $args->{user} ?>', { user => 'fo<o' })->as_string, 8 8 'hello fo<o', 9 9 ); 10 10 is( 11 as_html('hello <?= $args->{user} ?>', user => 'fo<o')->as_string,11 render_mt('hello <?= $args->{user} ?>', user => 'fo<o')->as_string, 12 12 'hello fo<o', 13 13 ); … … 17 17 my $s = 0; 18 18 eval { 19 as_html('hello <?= $nonexistent ?>');19 render_mt('hello <?= $nonexistent ?>'); 20 20 $s = 1; 21 21 }; -
lang/perl/Text-MicroTemplate/trunk/t/03-modes.t
r26764 r26781 5 5 6 6 # comment 7 is as_html(<<'...')->as_string, "aaa\nbbb\n";7 is render_mt(<<'...')->as_string, "aaa\nbbb\n"; 8 8 aaa 9 9 ?# … … 11 11 ?# 12 12 ... 13 is as_html('aaa<?# a ?>bbb')->as_string, "aaabbb";13 is render_mt('aaa<?# a ?>bbb')->as_string, "aaabbb"; 14 14 15 15 # expression and raw expression 16 16 do { 17 is as_html('<?= $args ?>', 'foo<a')->as_string, 'foo<a';18 is as_html('<?=r $args ?>', 'foo<a')->as_string, 'foo<a';17 is render_mt('<?= $args ?>', 'foo<a')->as_string, 'foo<a'; 18 is render_mt('<?=r $args ?>', 'foo<a')->as_string, 'foo<a'; 19 19 my $rs = encoded_string('foo<a'); 20 is as_html('<?= $args ?>', $rs)->as_string, 'foo<a';21 is as_html('<?=r $args ?>', $rs)->as_string, 'foo<a';20 is render_mt('<?= $args ?>', $rs)->as_string, 'foo<a'; 21 is render_mt('<?=r $args ?>', $rs)->as_string, 'foo<a'; 22 22 }; 23 23 do { 24 24 use utf8; 25 is as_html('あ<?= $args ?>う', 'い<')->as_string, 'あい<う';25 is render_mt('あ<?= $args ?>う', 'い<')->as_string, 'あい<う'; 26 26 my $rs = encoded_string('い<'); 27 is as_html('あ<?= $args ?>う', $rs)->as_string, 'あい<う';27 is render_mt('あ<?= $args ?>う', $rs)->as_string, 'あい<う'; 28 28 } -
lang/perl/Text-MicroTemplate/trunk/t/04-multiline.t
r26764 r26781 7 7 do { 8 8 my $y; 9 is( as_html(<<'...', sub { $y = 1 })->as_string, "abc 1 def\n", 'multiline expr');9 is(render_mt(<<'...', sub { $y = 1 })->as_string, "abc 1 def\n", 'multiline expr'); 10 10 abc <?= 1 11 11 $args->() ?> def … … 17 17 do { 18 18 my $y; 19 is as_html(<<'...', sub { $y = 1 })->as_string, "abc 1 def\n", 'multiline rawexpr';19 is render_mt(<<'...', sub { $y = 1 })->as_string, "abc 1 def\n", 'multiline rawexpr'; 20 20 abc <?=r 1 21 21 $args->() ?> def … … 25 25 26 26 # automatic semicolon insertion 27 is as_html(<<'...')->as_string, "abc\n1\n-1\ndef\n", 'expr auto-sci';27 is render_mt(<<'...')->as_string, "abc\n1\n-1\ndef\n", 'expr auto-sci'; 28 28 abc 29 29 ?= 1 … … 31 31 def 32 32 ... 33 is as_html(<<'...')->as_string, "abc\n1\n-1\ndef\n", 'expr auto-sci';33 is render_mt(<<'...')->as_string, "abc\n1\n-1\ndef\n", 'expr auto-sci'; 34 34 abc 35 35 ?=r 1 … … 39 39 40 40 # no automatic semicolon insertion for code 41 is as_html(<<'...')->as_string, "abc\n0\ndef\n", 'no auto-sci for code';41 is render_mt(<<'...')->as_string, "abc\n0\ndef\n", 'no auto-sci for code'; 42 42 abc 43 43 ? my $a = 1
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)