Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Looking for a "Template Engine"

by rovf (Priest)
on Jul 22, 2008 at 10:42 UTC ( [id://699258]=perlquestion: print w/replies, xml ) Need Help??

rovf has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks, does anybody know a CPAN module which could help me solving the following problem? I tried to search CPAN, but the closest I could found was Template, and I don't see how it can help for my application:

Assume I have a piece of code looking like this (just an example - I'm really flexible with regards to the syntax of the HERE document below):

sub foo { $_[0]+$_[1] } my $template=<<END; This is some line. This is a line with some expression: # $foo(3,5) # # foreach my $s qw(this that) { # This is # $s # line # } # This is the end END
I would like to "process" the code so that it evaluates the embedded expressions, resulting in a string
This is some line. This is a line with some expression: 8 This is this line This is that line This is the end
As I said, I do not have to stick to the syntax outline above. Whether the Perl expressions are delimited by '#' or by some other means, I don't care. It is not even necessary that the embedded expressions are full-blown Perl code; even some pseudocode would be OK, as long as it is powerful enough so that I can access my Perl variables, and that I can do looping construct and 'if' statements.

In practice, the surrounding text code will be XHTML, but I would like to be able to use the same concept with other text file formats, such as JSON or RTF.

Hence, I look for something similar to what Java and Ruby programmers find in JSP and RubyOnRails, but with the twist that it should not be restricted to HTML output (the program is not running in the context of a web application; it is a stand-alone application, which just creates these text files).

BTW, for PHP, there exists a solution which comes pretty close to what I'm looking for: It's called Smarty.

-- 
Ronald Fischer <ynnor@mm.st>

Replies are listed 'Best First'.
Re: Looking for a "Template Engine"
by Corion (Patriarch) on Jul 22, 2008 at 11:00 UTC

    If you're bent on including Perl code in your templates, you can also use PLP, which is a templating engine that allows that.

    There are many discussions of the benefits and drawbacks of the various templating systems. I have a (short, German) comparison of templating systems in my Advanced Perl Techniques tutorial on slide 158 (no direct linking in this version of S5, sorry), and Mason ranks high up there in terms of power and perlishness. You should question yourself though how much power you need in the template side of things. Even PHP, which basically is Mason for purposes of this discussion, went the route of having its own templating systems instead of using pure PHP. Using a templating system forces you to separate data munging , business logic and displaying, and IMO that's why a powerful templating system is a danger because you will end up implementing business logic in the display templates.

Re: Looking for a "Template Engine"
by pjotrik (Friar) on Jul 22, 2008 at 11:11 UTC
Re: Looking for a "Template Engine"
by moritz (Cardinal) on Jul 22, 2008 at 10:48 UTC
    So you're looking for a template engine that executes perl code.

    I'm pretty sure that Mason does what you want. eperl also looks like it might be interesting for you.

      I'm pretty sure that Mason does what you want. eperl also looks like it might be interesting for you.

      Hmmm.... From the documentation at CPAN: CONFIGURING MASON. ... This section assumes that you are able to install and configure a mod_perl server, so if I understand it right, it seems to assume that it runs with a web application. And as for eperl, I only found Apache::ePerl, and here it says: The intent is to use this special variant of ePerl for scripts which are directly under control of the webmaster... For the average user you should not use Apache::ePerl. Instead additionally install the regular stand-alone ePerl facility (nph-eperl) for those users. So this too assumes Apache. I looked for nph-eperl, but found nothing on CPAN (and the few references I found on nph-eperl on Google seem to suggest that this too assumes somehow to run under a webserver).

      Also it looks as if Mason assumes to be embedded in HTML, though perhaps it will work with other text files too....

      -- 
      Ronald Fischer <ynnor@mm.st>

        HTML::Mason does not depend on mod_perl, but it can use it ;-)

        Here's your example rendered through Mason:

        #!/usr/bin/perl use strict; use warnings; use HTML::Mason; sub foo { $_[0] + $_[1] } my $template = << 'END'; This is some line. This is a line with some expression: <% foo(3,5) %> % for my $s qw(this that) { This is <% $s %> line % } This is the end END my $output; my $interp = HTML::Mason::Interp->new( out_method => \$output, in_package => __PACKAGE__, ); my $comp = $interp->make_component( comp_source => $template ); $interp->exec($comp); print $output;

        You mentioned you don't care too much about the syntax, that's why I preferred do not show you how to override Mason's default syntax. Check out http://masonhq.com/docs/manual/ if you're interested.

        --
        altblue.

        Also it looks as if Mason assumes to be embedded in HTML, though perhaps it will work with other text files too...

        Most template systems ignore the type of document they are embedded in, mostly it's relevant for choosing the delimiters of the interpreted parts, and for stuff like escaping (which is always optional).

        I have no experience with Mason myself, but I'd guess it's similar there (and I'm quite sure there's also a way to use Mason wihout mod_perl. Just look at the test cases, it has to be tested somehow).

Re: Looking for a "Template Engine"
by perrin (Chancellor) on Jul 22, 2008 at 11:50 UTC
    Template Toolkit does everything you're asking for. What makes you think it won't work for you?
      Template Toolkit does everything you're asking for. What makes you think it won't work for you?

      For example, the way how to produce tables out of arrays. If I understood the examples correctly, Template::Toolkit requires you to loop over the array and append the code to be generated to a special variable OUT. Since the majority of my generated code happens inside a loop over the data, I will end up generating 90% of the result by just sending string to OUT; from the viewpoint of readability certainly not something which is a "template" anymore.

      Actually, after a first glance, I meanwhile found that Text::Template::Simple and Text::ScriptTemplate could do what I want, so I will give them a try.

      -- 
      Ronald Fischer <ynnor@mm.st>

        Let me show you how hard it is to work with Template for the example you gave.

        the Perl script

        use strict; use warnings; use Template; my $template = Template->new({ INCLUDE_PATH => '.', EVAL_PERL => 1, }) || die $!; my $foo = foo(3,5); my @s = ('this', 'that'); $template->process("tpl.txt", {foo => $foo, s => \@s}) || die $templat +e->error(); sub foo { $_[0]+$_[1] }

        the template file

        This is some line. This is a line with some expression: [% foo %] [% FOREACH l = s %] This is [% l %] line [% END %] This is the end

        btw. It's up to you to make your code readable. If you have arrays, you'll have to cycle through its elements. Template is not to blame.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://699258]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2024-07-25 09:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.