Show
Ignore:
Timestamp:
12/15/08 18:01:10 (4 years ago)
Author:
kazuho
Message:

adjust function names, more doc

Location:
lang/perl/Text-MicroTemplate/trunk
Files:
4 modified

Legend:

Unmodified
Added
Removed
  • lang/perl/Text-MicroTemplate/trunk/lib/Text/MicroTemplate.pm

    r26770 r26781  
    1414our $VERSION = '0.01'; 
    1515our @ISA = qw(Exporter); 
    16 our @EXPORT_OK = qw(as_html encoded_string); 
     16our @EXPORT_OK = qw(encoded_string build_mt render_mt); 
    1717our %EXPORT_TAGS = ( 
    1818    all => [ @EXPORT_OK ], 
     
    3232        tag_end             => '?>', 
    3333        escape_func         => 'Text::MicroTemplate::escape_html', 
    34         ref($_[0]) ? %{$_[0]} : @_, 
     34        @_ == 1 ? ref($_[0]) ? %{$_[0]} : (template => $_[0]) : @_, 
    3535    }, $class; 
    3636    if (defined $self->{template}) { 
     
    337337} 
    338338 
    339 sub mt_build { 
    340     my $_mt = Text::MicroTemplate->new( 
    341         ref($_[0]) ? $_[0] : { template => shift }, 
    342     ); 
     339sub build_mt { 
     340    my $_mt = Text::MicroTemplate->new(@_); 
    343341    my $_code = $_mt->code; 
    344342    my $expr = << "..."; 
     
    361359} 
    362360 
    363 sub as_html { 
    364     my $builder = mt_build(shift); 
     361sub render_mt { 
     362    my $builder = build_mt(shift); 
    365363    $builder->(@_); 
    366364} 
     
    390388=head1 SYNOPSIS 
    391389 
    392     use Text::MicroTemplate qw(as_html); 
     390    use Text::MicroTemplate qw(:all); 
    393391 
    394392    # 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; 
    398401 
    399402    # complex form 
     
    402405    ); 
    403406    $code = $mt->code; 
    404     $builder = eval << "..." or die $@; 
     407    $renderer = eval << "..." or die $@; 
    405408    sub { 
    406409        my \$query = shift; 
     
    408411    } 
    409412    ... 
    410     $html = $builder->(CGI->new); 
     413    $html = $renderer->(CGI->new)->as_string; 
    411414 
    412415=head1 DESCRIPTION 
     
    457460=head1 EXPORTABLE FUNCTIONS 
    458461 
    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 
     464Utility 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 
     481Intertally, the function is equivalent to: 
     482 
     483    build_mt($template)->($args); 
     484 
     485=head2 build_mt($template) 
     486 
     487Returns 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    } 
    466497 
    467498=head2 encoded_string($str) 
     
    471502=head1 OO-STYLE INTERFACE 
    472503 
    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 
     504Text::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 
     510In 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>. 
    484511 
    485512=head2 code() 
  • lang/perl/Text-MicroTemplate/trunk/t/02-as_html.t

    r26770 r26781  
    55 
    66is( 
    7     as_html('hello <?= $args->{user} ?>', { user => 'fo<o' })->as_string, 
     7    render_mt('hello <?= $args->{user} ?>', { user => 'fo<o' })->as_string, 
    88    'hello fo&lt;o', 
    99); 
    1010is( 
    11     as_html('hello <?= $args->{user} ?>', user => 'fo<o')->as_string, 
     11    render_mt('hello <?= $args->{user} ?>', user => 'fo<o')->as_string, 
    1212    'hello fo&lt;o', 
    1313); 
     
    1717    my $s = 0; 
    1818    eval { 
    19         as_html('hello <?= $nonexistent ?>'); 
     19        render_mt('hello <?= $nonexistent ?>'); 
    2020        $s = 1; 
    2121    }; 
  • lang/perl/Text-MicroTemplate/trunk/t/03-modes.t

    r26764 r26781  
    55 
    66# comment 
    7 is as_html(<<'...')->as_string, "aaa\nbbb\n"; 
     7is render_mt(<<'...')->as_string, "aaa\nbbb\n"; 
    88aaa 
    99?#  
     
    1111?#  
    1212... 
    13 is as_html('aaa<?# a ?>bbb')->as_string, "aaabbb"; 
     13is render_mt('aaa<?# a ?>bbb')->as_string, "aaabbb"; 
    1414 
    1515# expression and raw expression 
    1616do { 
    17     is as_html('<?= $args ?>', 'foo<a')->as_string, 'foo&lt;a'; 
    18     is as_html('<?=r $args ?>', 'foo<a')->as_string, 'foo<a'; 
     17    is render_mt('<?= $args ?>', 'foo<a')->as_string, 'foo&lt;a'; 
     18    is render_mt('<?=r $args ?>', 'foo<a')->as_string, 'foo<a'; 
    1919    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'; 
    2222}; 
    2323do { 
    2424    use utf8; 
    25     is as_html('あ<?= $args ?>う', 'い<')->as_string, 'あい&lt;う'; 
     25    is render_mt('あ<?= $args ?>う', 'い<')->as_string, 'あい&lt;う'; 
    2626    my $rs = encoded_string('い<'); 
    27     is as_html('あ<?= $args ?>う', $rs)->as_string, 'あい<う'; 
     27    is render_mt('あ<?= $args ?>う', $rs)->as_string, 'あい<う'; 
    2828} 
  • lang/perl/Text-MicroTemplate/trunk/t/04-multiline.t

    r26764 r26781  
    77do { 
    88    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'); 
    1010abc <?= 1 
    1111 $args->() ?> def 
     
    1717do { 
    1818    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'; 
    2020abc <?=r 1 
    2121 $args->() ?> def 
     
    2525 
    2626# automatic semicolon insertion 
    27 is as_html(<<'...')->as_string, "abc\n1\n-1\ndef\n", 'expr auto-sci'; 
     27is render_mt(<<'...')->as_string, "abc\n1\n-1\ndef\n", 'expr auto-sci'; 
    2828abc 
    2929?= 1 
     
    3131def 
    3232... 
    33 is as_html(<<'...')->as_string, "abc\n1\n-1\ndef\n", 'expr auto-sci'; 
     33is render_mt(<<'...')->as_string, "abc\n1\n-1\ndef\n", 'expr auto-sci'; 
    3434abc 
    3535?=r 1 
     
    3939 
    4040# no automatic semicolon insertion for code 
    41 is as_html(<<'...')->as_string, "abc\n0\ndef\n", 'no auto-sci for code'; 
     41is render_mt(<<'...')->as_string, "abc\n0\ndef\n", 'no auto-sci for code'; 
    4242abc 
    4343? my $a = 1