Perl templates with inheritance
Asked Answered
A

5

5

Exists in the Perl world any template system with template inheritance?

Just checked in the wikipedia Comparison_of_web_template_engines (really incomplete list) - and here isn't listed any.

Inheritance = Supports the ability to inherit a layout from a parent template, separately overriding arbitrary sections of the parent template's content.

Mean something like python's Jinja2:

#body.html
<body>
   {% block content %}
   <!-- the content go here -->
   {% endblock %}
</body>

#hi.html
{% extends "body.html" %}
{% block content %}
<h1>Hi!</h1>
{% endblock %}

rendering hi.html gives

 <body>
 <h1>Hi!</h1>
 </body>

Don't looking for exact Jinja2 syntax, simply looking for any template engine from the perl world what supports inheritance. (not only plain includes - like Template::Toolkit)

Asking here because searching CPAN is a pain for words like "template inheritance" - shows thousands modules what not related to this question.

Ps: ... and it shouldn't be something like embedded perl - should be "user editable" allowing users builds own templates without compromise a whole system - therefore can't use Mason or HTML::Mason)

Avril answered 9/1, 2014 at 10:58 Comment(4)
Don't know if it supports your exact definition of template inheritance, but Template Toolkit has some nice features for reusing components and composing templates from multiple files.Faro
Why include/process/wrapper of Template Toolkit do not help you?Haircloth
@PseftiS it is like: "Why do you uses perl objects inheritance" and not only require '/some/file.pl' ;) ;). Mostly the difference is only in the bless...Avril
Does this answer your question? Can I set up template inheritance inside a template? (Template Toolkit)Gesticulate
E
3

I'll second the suggestion for Text::Xslate. Its syntax is even quite similar to your Jinja2 examples:

In layouts/main.tx:

...
<div id="page">
  : block sidebar -> { }
  <div id="content">
    : block header-> { <h1>Default Title</h1> }
    : block content -> { }
  </div>
</div>
...

In another template:

: cascade layouts::main
: override header -> {
<div id="header">
...
</div>
: }

: override content -> {
<div id="content-body">
...
</div>
: }
Electrical answered 9/1, 2014 at 14:54 Comment(0)
M
2

A colleague recently started using Text::Xslate.

Mephitis answered 9/1, 2014 at 11:46 Comment(0)
D
2

Template::Jade, a port of Jade to perl.

parent.jade

doctype html
html
  head
    title Jade template
  body.class
    p#id_name
      block content

child.jade

extends parent

block content
  p.
    This is a test.
    This is a test.
    This is a test.
Digression answered 19/5, 2014 at 22:19 Comment(0)
D
2

DTL::Fast Is my Perl implementation of Django templates with inheritance and other stuff.

Dixiedixieland answered 15/2, 2015 at 9:20 Comment(0)
G
0

The Perl Template Toolkit has a feature called VIEW which unfortunately is documented as "experimental" but has been around for almost 20 years, works very well, and allows you to do object-oriented programming in Templates. Blocks within a view are similar to methods, and local variables are similar to attributes. See https://template-toolkit.org/docs/manual/Views.html.

Here is a skeleton :

[% VIEW parent attr1='foo' attr2=123 %]
  [% BLOCK meth1 %] hello [% foo %] [% END # BLOCK %]
  [% BLOCK meth2 %] ...
[% END # VIEW %]

[% VIEW child     # this new view can be in the same file or in another file
     base=parent  # inheritance 
     attr1='bar'  # override of parent attribute
  %] 
   [% BLOCK meth1 # override of parent method %] salut, [% foo %] [% END %]
[% END # VIEW child %]
Gesticulate answered 19/5 at 6:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.