Set parent template from PHP

10 views
Skip to first unread message

al james

unread,
Apr 6, 2009, 1:56:37 PM4/6/09
to H2o template PHP
Hi there...

Template inheritance is very powerful. However, sometimes it is useful
to set the parent for a template from PHP. So instead of :

{% extends base.tpl %}

We would have:

$my_h2o_template->setParent('base.tpl');

The advantage being that I can set the default 'master template' from
a config file...

Taylor luk

unread,
Apr 7, 2009, 5:26:36 AM4/7/09
to H2o template PHP
Hi al james,

That is a very interesting idea and can be quiet useful. It will be
difficult to answer you this since it may require to change the
current flow of internal parsing, i will definitely look into this


Cheers

Taylor Luk

Taylor luk

unread,
May 13, 2009, 4:19:41 AM5/13/09
to H2o template PHP
I haven't got a chance to think about this problem seriously. Lately i
am building a cms service app
and it is still one of the design decision i haven't got a good
solution yet.

The way i would do it will be following hack

$template = "{% extends '{$parent_template}' %}\n" . file_get_contents
("${tempate}.html");
H2o::parseString($template)->render($awesome);


I actually want to hear your opinion on this

For a system used by more of designers and less hardcore developers,
"template inheritance vs inclusion" which is easier for them to
understand,

Inclusion (2 phase rendering, render inner template then assign it to
content_for_layout for outter template)
<html>
<head><title>{% block title %}{{ page.title }} {%endblock%}</title></
head>
<body>
{{ content_for_layout }}
</body>
</html>

Or inheritance
<html>
<head><title>{% block title %}{{ page.title }} {%endblock%}</title></
head>
<body>
{% block body %}

{% endblock %}
</body>
</html>

Given that both options can build in a way user can select what parent
or layout to use for a page, what do u think?


Cheers

al james

unread,
Jun 4, 2009, 6:42:18 PM6/4/09
to H2o template PHP
Hi, sorry a bit slow replying to this!

Personally I think the block system is much more flexible, and from my
experience with 'design first people' they seem to think it makes more
sense. Some common problems (e.g. 2 column layouts) are very easy with
the block system, but harder with the content_for_layout system...

As for my original comment on allowing you to specify template
inheritance from code, the obvious way would be to allow you to use a
parameter in the extends block.

E.g. not

{% extends "base" %}

It would be:

{% extends :base_temple %}

Or does that break some internal system?

Al

Taylor Luk

unread,
Jun 4, 2009, 8:22:35 PM6/4/09
to h2o-temp...@googlegroups.com
What a awesome idea, definitely going into version 0.5

Support variable in extends tag

{% extends "base.html" %}
{% extends layout_for_page %}

Cheers
Reply all
Reply to author
Forward
0 new messages