views:

702

answers:

8

I am looking to build my own templating layer in php as part of my OOP Framework, but Im looking so some example of how to do this properly. I'd like to avoid parsing each html file to find comments that tell the templating engine what to do but rather use a faster/cleaner/modern technique. I just need some examples, because i've looked around using google and nothing caught my eye. I'm trying to keep my presentation and logic separate but what i'm doing requires tons of logic through out the html code.

,Thanks

P.S. I'm not looking to use an existing templating engine such as smarty, I want to build my own.

A: 

Well you should consider looking at something like this which I have used. It's a very cool way to approximate Master Pages, like the ones seen in ASP pages using PHP: http://spinningtheweb.blogspot.com/2006/07/approximating-master-pages-in-php.html

If you want to build you own that should be something cool to look at.

Robert Massaioli
after visiting the link my McAfee SiteAdvisor redirected me to its page saying: spinningtheweb.blogspot.com/2006/07/approximating-master-pages-in-php.html may try to steal your information. The first time ever I seen SiteAdvisor take action. (Not the first time I visited some link posted on SO just to find myself in a trap)
Peter Perháč
A: 

... I'm trying to keep my presentation and logic separate but what i'm doing requires tons of logic through out the html code....

Hey, there's this...

Why not give us an example of what you are trying to do? Maybe it will give us an idea of why your situation is special, perhaps there is a special solution for your type of problem? Just saying, I want to write a template engine, what's the bestest, fastest way to do that doesn't help much. Seriously, I would take a number of them, look at what they do, understand why they've done, learn, and do something different if I want something better.

altCognito
Why doesn't saying what I need help much?
Because it's incredibly vague. "I need to write a really good application" is saying what you need as well, but that level of ambiguity does not help others answer your question.
Calvin
+2  A: 

Did you thought about the Model-View-Controller pattern? You could abstract all logic with functions and variables (inside controllers) and expose them to the views. It could be a first step for templating.

P.S: I've tested a template engine called PHPTAL (Template Attribute Language). I liked it because you can create html templates with logic embedded inside custom html attributes, without breaking the design.

I hope these ideas have been helpful for you

xaguilars
That P.S is very interesting!
+8  A: 

I'll ask the same question of you that I'd ask of anyone suggesting Smarty:

Why do you want to add a templating language to PHP, which is a templating language?

PHP is a templating language. I think people forget that or try to treat it like a pure OO language, which it isn't. Play to PHP's strengths. Don't try and make it something it's not.

That all being said, it's hard to answer your question because you don't say how simple or complex your templating system is, namely what features you intend to support.

If your templating language is relatively simple then regular expressions may be the way to go. That won't work for some trivial cases however. Nesting of control structures is probably the most common. In that case you'll need to write some form of rudimentary parser.

Let's say that all your templating structures are in blocks like this:

{:...:}

because that's relatively unlikely to occur otherwise in an HTML page (although, for completeness, you'll need to cater for the case where the user does actually want to use those character combinations.

You need to scan your file for such expressions and process them accordingly. Regular expressions can be used to find them all but not necessarily matching ones, like for conditional includes eg:

{:if ...:}
Some conditional content
{:endif:}

Why? Because of this:

{:if *some condition*:}
{:if *some other condition*:}
Some other conditional content
{:endif:}
Some conditional content
{:endif:}

Matching that with regular expressions can't reliably be done so you'll need to parse your file into some form of tree of lexemes and then process it.

cletus
A: 

You would require to implement parsers. But instead of parsing it each time do what smarty and others do save the parsed output for later use. It would be even better if output of your parser is php code itself.

Niran
When Smarty parses a template, it simply turns it into php. It parses once, on first page load. However, Smarty also supports caching, in which case it renders a php page or part of a page and saves the resulting html.
rick
A: 

I coded an php mvc framework too. I used smarty for templates. ;)

whitenexx
A: 

the fastest method is to use php itself, because php IS a templating language. of course, sometimes that's not an option because of security reasons, and the designer having to learn basic php (that really hurts a lot). sometimes, it gets really ugly too, where other, more specialized (and less mighty) templating systems can keep it clean.

so if you really have to build your own, there are 3 options top off my mind ...

  1. as a beginner i did some templating through custom parsing (strpos, substr and so on). it was very slow, inelegant and error prone. i wouldn't recommend that.

  2. regular expression mayhem. if you're not adept at regexps, forget it. but it's probably one of the faster methods.

  3. maybe you could path something together with the php tokenizer functions ...

  4. the DOM-extension! make your templates xhtml compliant and load them as xml files. whoa, all the parsing is done for you! now you have a nice tree with either custom tags, like


      <loop item="$myVariable" times="$x">
        <print name="$myVariable->name" entities="yes" nl2br="yes" purify="removeemptytags, xss"/>
        <hr/>
      </loop>

already looks a bit like smarty, imho. you just have to either do some interpreting (echoing the subnodes of the loop-node $x times), or maybe translate it to php and cache it (smarty does this for performance).

or, if you prefer comments, something like this (but here you have to do more parsing than using custom tags)


    <!-- begin loop:myVariable -->
      <-- print:myVariable->name() -->
      <hr/>
    <!-- end loop -->

i can't imagine the DOM method to be very fast, but if you really need moar speed, translation to native php code and/or caching of the rendered html would be better (if speed really is that important, don't write your own code. except if you're carmack or skeet maybe).

good luck.

Schnalle
Your xml tags and attributes in #4 aren't valid xhtml. You'd need to namespace them. Have a look at PHPTAL.
rick
uh, that was just an example! i don't want to write a templating engine myself, i'm happy with php for that matter :) but thanks for the link to phptal, it's interesting (for me) how close i came (not very close).
Schnalle
A: 

The best php templating tehnique is to keep your htmls untouched allowing your frontend devs to work on them even after the developers integrate them into your website, you can do this with Psttt! templating engine for php

full source code here http://github.com/givanz/psttt

codeassembly