Dotiac::DTL::Compiled - Stores a compiled Django template.
Autocompiling:
require Dotiac::DTL; $t=Dotiac::DTL->newandcompile("file.html"); #file.html is now compiled to file.html.pm, $t is still the original version, unless it was already compiled. $t=Dotiac::DTL->new("file.html"); #$t is now the compiled version.
Other compiled templates:
package MyTemplate; sub print { my ($vars,$escape)=(shift(),shift()); print "There are ".keys(%$vars)." parameters registered and x is $vars->{x}\n"; } sub string { my ($vars,$escape)=(shift(),shift()); return "There are ".keys(%$vars)." parameters registered and x is $vars->{x}\n"; } sub eval { #nothing for now. } package main; require Dotiac::DTL; my $mytemplate=Dotiac::DTL->compiled("MyTemplate"); # now you can use $mytemplate as a normal template. $mytemplate->print({x=>123}); # This doesn't seem quite useful you could easily just write the code here, until you do this: my $templatedata="{% for x in array %}{% include template %}{% endfor %}"; my $t = Dotiac::DTL->new(\$templatedata); #File templates work just as well. $t->print({array=>[1..100],template=>$mytemplate); # This will now include and print the above package a hundert times and # will be a lot faster, depending on the contents of that for loop.
This also works with Dotiac::DTL::Reduced;
Compiled templates are just perl modules that act like templates, those are a lot faster than normal templates, but more difficult to write.
Some issues can be removed by compiling all the templates once and then just run the webserver with tight security, i.e. no write access.
This compiles all templates ending with .html in the current folder:
require Dotiac::DTL; Dotiac::DTL->newandcompile($_) foreach (<*.html>);
You can use other packages as templates with:
Dotiac::DTL->compiled("packagename");
These packages must provide these three methods:
This is called when print() is called somewhere above this template.
sub print { my $vars=shift; #Reference to variable/parameter hash my $escape=shift; #Autoescape status: 0 off, 1 on print Dotiac::DTL::devar("somevar",$vars,$escape,@_) # Don't forget @_ here. for now it contains nothing, but it might in the future. }
Similar to print(), is called when string() is called on the template and returns the data.
print() and string() should always produce the same data.
sub string { my $vars=shift; #Reference to variable/parameter hash my $escape=shift; #Autoescape status: 0 off, 1 on print Dotiac::DTL::devar("somevar",$vars,$escape,@_) # Don't forget @_ here. for now it contains nothing, but it might in the future. }
This is only called when this template is included and the including template (or one including that or ...) has an {% extends %} tag.
This is only used to set blocks for extend.
sub eval { my $sub = sub { return "Hello World" } my $subref=\$sub; unshift @{$Dotiac::DTL::blocks{"myblockname"}},$subref; unshift @{$Dotiac::DTL::blocks{"someotherblock"}},Dotiac::DTL->new("file.html")->{first}; }
There should not be many occaisions where this is needed.
There might be some bugs/problems in the autocompiled templates that won't appear in normal mode.
The autocompiled perl code is definitely not the most optimized and best solution.
The memory-saving way of print() will be less effective if extends is involved and templates are autocompiled. However it is still faster than uncompiled templates but uses a bit more memory. I just have no idea how to fix it properly without producing even more code.
http://www.djangoproject.com, Dotiac::DTL
Dotiac::DTL was built according to http://docs.djangoproject.com/en/dev/ref/templates/builtins/.
Marc-Sebastian Lucksch
perl@marc-s.de
To install Dotiac::DTL, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Dotiac::DTL
CPAN shell
perl -MCPAN -e shell install Dotiac::DTL
For more information on module installation, please visit the detailed CPAN module installation guide.