Text::APL - non-blocking and streaming capable template engine
$template->render( input => \$input, output => \$output, vars => {foo => 'bar'} );
$template->render( input => sub { my ($cb) = @_; # Call $cb($data) when data is available # Call $cb->() on EOF }, output => sub { my ($chunk) = @_; # Print $chunk to the needed output # $chunk is undef when template is fully rendered }, vars => {foo => 'bar'} );
This is yet another template engine. But compared to others it supports non-blocking (read/write) and streaming output.
Reader and writer can be a subroutine references reading from any source and writing output to any destination. Sane default implementations for reading from a string, a file or file handle and writing to the string, a file or a file handle are also available.
Parser can parse not only full templates but chunk by chunk correctly resolving any ambiguous leftovers. This allows immediate parsing.
This for example works just fine:
$parser->parse('<% $hello'); $parser->parse(' %>');
Compiler compiles templates into Perl code but when evaluating does not create a Perl string that accumulates all the template output, but rather provides a special print function that pushes the content as soon as it's available (streaming).
print
The generated Perl code can looks like this:
Hello, <%= $nickname %>! # becomes __print(q{Hello, }); __print_escaped(do {$foo}); __print(q{!});
Syntax is borrowed from the template standards shared among several web framewoks in different languages:
<% foo() %> # evaluate code % foo() <%= $foo %> # insert evaluation result %= $foo <%== $foo %> # insert evaluation result without escaping %== %foo
No new template language is provided, just the old Perl.
new
my $template = Text::APL->new;
Create new Text::APL instance.
Accepted options:
parser (by default Text::APL::Parser)
translator (by default Text::APL::Translator)
compiler (by default Text::APL::Compiler)
reader (by default Text::APL::Reader)
writer (by default Text::APL::Writer)
render
$template->render( input => \$input, output => \$output, vars => {foo => 'bar'}, helpers => { time => sub {time} } );
input and output can be a filename, a reference to scalar, a file handle and a reference to subroutine. Read more at Text::APL::Reader and Text::APL::Writer.
input
output
vars are Perl variables available in the template.
vars
helpers are Perl subroutines. available in the template.
helpers
For working examples see examples/ directory in distribution.
examples/
http://github.com/vti/text-apl
Viacheslav Tykhanovskyi, vti@cpan.org.
vti@cpan.org
Copyright (C) 2012-2017, Viacheslav Tykhanovskyi
This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0.
To install Text::APL, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Text::APL
CPAN shell
perl -MCPAN -e shell install Text::APL
For more information on module installation, please visit the detailed CPAN module installation guide.