Welcome Guest, Not a member yet? Register   Sign In
New library for doing "template inheritance" *without* a template parser
#1

[eluser]arshaw[/eluser]
Hello all! I've always found django's template inheritance to be invaluable when organizing my template code. I've always wanted the same in PHP, but without a cumbersome template parser like smarty. I've just developed a library to do this in pure PHP:

PHP Template Inheritance

Internally, it uses output buffers, but its API is very simple and easy to use. For example, here is a "parent" template:

Code:
<html>
<body>
<h1>
&lt;? startblock('title') ?&gt;
&lt;? endblock() ?&gt;
</h1>
<div id='article'>
&lt;? startblock('article') ?&gt;
&lt;? endblock() ?&gt;
</div>
&lt;/body&gt;

and here is the "child" template:

Code:
&lt;? include 'parent.php' ?&gt;

&lt;? startblock('title') ?&gt;
   This is the title
&lt;? endblock() ?&gt;

&lt;? startblock('article') ?&gt;
   This is the article
&lt;? endblock() ?&gt;

and here is what it outputs:

Code:
&lt;html&gt;
&lt;body&gt;
<h1>
   This is the title
</h1>
<div id='article'>
   This is the article
</div>
&lt;/body&gt;
&lt;/html&gt;

I really think this would be useful in CodeIgniter, which I praise for its lack of template engine (by default), and going the straight PHP route for view code. I'd love to hear what you guys think, and whether you think it'd be appropriate to include this in a codeigniter distro.


Messages In This Thread
New library for doing "template inheritance" *without* a template parser - by El Forum - 03-29-2010, 10:26 PM



Theme © iAndrew 2016 - Forum software by © MyBB