My favorites | Sign in
Project Logo
                
Search
for
Updated Sep 16, 2009 by erdmann.tester
Labels: Featured
Overview  
All Features described in detail

Overview

Serpent uses PHP itself as its template language so it should be easy for you to work with. But there are a few improvements compared to pure PHP which are explained on this page.

short syntax for php tags

Because it is annoying to write the full php open and close tags, serpent uses the tilde (which gave it its name) to determine the start and the end of a php block. Serpent uses the same delimiter at both sides because it is faster written than different characters or multiple characters. Additionally it is possible to write two tildes at the beginning to express a "<?php echo".

~echo $foo~

is the same as

~~$foo~

It is also possible to use the default php tags like "<?php", "<?" and "<?=". The short tags which will be expanded to the longer version so you can use them also on servers with "short_open_tag = 0" (php.ini).

Escaping the tilde

If you want to output a tilde you have to escape it in your template with a backslash:

Now here comes a tilde: \~.

no additional markup language

You do not have to learn another markup language because all code you write in between the delimiters is pure php code.

To write a simple condition you could write that:

~if ($foo == $bar) {~ 
    ~~$foo~
~}~

For better readability I recommend the alternative syntax for control structures:

~if ($foo == $bar):~ 
    ~~$foo~
~endif~

template comments

The syntax for comments in a template is the same as in php. So the following template code is valid because it does not matter, if you write your comment between or outside the delimiters:

/* Now iterate an array */
~foreach ($var as $value): /* add $key to iterate the keys too */~
    ~~$value~
~endforeach~

dot syntax for arrays

If you have ever worked with Smarty you will probably like the handy dot syntax for arrays. As Smarty does Serpent allows you to mix the dot syntax and the standard php syntax in your php code.

~~$foo.bar~
~~$foo.bar[$bar.index].$foo.bar~

shortcuts for functions (mappings)

Because you are able to write pure php code it is simple to call own template helper functions. Imagine you have a method in a static helper class that generates linked email addresses. You would call it like that:

~~templateHelperClass::mailto('foo@bar.com')~

But this is not very readable. For this reason it is possible to map functions to shortcuts. Mapped functions always start with a colon. For the example above you could define a mapping which let you call the line above that way:

~~:mailto('foo@bar.com')~

template inheritance

Template inheritance is a ingenious feature first used in the templating engine of "Django", a Python web framework. Template inheritance allows you to build a base "skeleton" template that contains all the common elements of your site and defines blocks that child templates can override.

You all know the old way the most template engines use. You define a master template with includes which also includes other templates and so on. But that way some day you will get into trouble when you try to integrate a feature your templates were not built for.

Imagine you have a simple two column layout with the navigation at the left side and the content at the right side. In your master template you build the two column layout with the navigation and an include tag for the content area. Now try to include a teaser below the navigation which should only be visible on one single page. Tricky, hm?

Here is the way you would solve that with template inheritance.

"page" template (that is the page you render with Serpent)

~:extend('base')~

~[content]~
    The Content for this page.
~[/content]~

~[teaser]~
    Because we defined this block the empty teaser block of the base template gets overwritten with this text. 
~[/teaser]~

"base" template

<html>
<head>
    <title>My site</title>
</head>
<body>
    <div id="left">
        <!-- navigation -->
        ~[teaser]~
        ~[/teaser]~
    </div>
    <div id="right">
        ~[content]~
            This text appears if content is not defined in the extending template.
        ~[/content]~
    </div>
</body>
</html>

It is possible to extend as many levels as you want.

Keep in mind: It makes no sense to define text outside from block tags in an extending template.

By the way: Serpent does not force the use of template inheritance. Simple old fashioned include tags are also possible of course.

definable resources and compilers

It is possible to define other templates resources than those working with the file system. Imagine you write a CMS where your templates are stored in a database. No problem. Just write your own resource handler (it is really easy) and you are done. For more take a look at the Resources.

And now imagine you want to build a website with the serpent syntax and then include a CMS template which was written in a Wiki syntax. Also no problem. It is possible to change the compiler on the fly. For more take a look at the Compilers.

compiling engine for best performance

Serpent parses your templates and creates PHP scripts from them. When your Web page is viewed, Serpent reads from these PHP scripts instead of pulling the templates themselves, which saves the work of having to parse your templates again. And it only re-compiles your templates when you make changes to them, so you don't have to worry about manually compiling the templates.

You don't even have to know the PHP scripts are there, nor how compiling works. It just works.


Comment by littlexiang521, Jun 24, 2009

Thanks for great job! That's exactly what I'm thinking about. I am trying to integrate this into my framework.(see http://littleframework.googlecode.com/)


Sign in to add a comment
Hosted by Google Code