views:

825

answers:

4

Hi everyone,

Since I keep showing up late for answering questions tagged php where i actually know the answer i figured i'd try asking a question myself.

I've been working on so many complete rewrites of a custom template engine in php for so long and so many times that i thought i'd ask for opinions.

In short, this is the most important part i have implemented so far:

  1. Any http request is routed to handler.php
  2. based on the requested URL a controller is instantiated and a method on that controller is called.
  3. The controller method must return an IView compatible class instance ( IView defines a Render() method)
    1. The template engine does some xpath for every namespace that ends in 'serverside' sprintf('//%s:*[@runat="server"]', $namespaceprefix )
    2. For every found tag, it looks up the php class identified by $tag.localName and instantiates one and attaches it to the original template.
    3. Once attached, the original template node is fed to the 'ServerTag' so it can initialize properly
    4. the fully complete IView compatible instance is assigned to a temporary variable in the controller method.
  4. The controller asks pushes data from a Model class to the view which does some nifty xpath DOM replacement.
  5. The controller returns the completely filled view to main()the handler, which renders it.

I am basing my template on xml. a simple template currently looks like:

<html xmlns="http://www.w3.org/1999/xhtml" 
  xmlns:red="http://www.theredhead.nl/serverside"&gt;
<head>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
 <title>Title will be filed by the View depending on the Controller</title>
 <link rel="stylesheet" type="text/css" href="/Stylesheet/Get/Main/" />
</head>
<body>
 <!-- the entire body may be reset by the view using it, using XPath and DOM functions -->
 <!-- Usually the PageHeader and PageFooter would be put back after clearing the body  -->
 <div id="PageHeader">
  <img src="/Image/Get/theredhead_dot_nl.png" alt="Site Logo" />
 </div>
 <h1>www.theredhead.nl :: Test Template</h1>
 <p>
  Lorum ipsum dolar sit amet. blah blah blah yackadee schmackadee.
 </p>

 <div id="PageFooter">
  Built by
  <br />
  <red:UserProfileLink runat="server" Username="kris" />
 </div>
</body>
</html>

At current, this outputs (including the broken indentation):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:red="http://www.theredhead.nl/serverside"&gt;
<head>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
 <title>Welcome to my site</title>
 <link rel="stylesheet" type="text/css" href="/Stylesheet/Get/Main/"/>
 <link rel="stylesheet" type="text/css" href="/Stylesheet/Get/Custom/"/>
 <link rel="stylesheet" type="text/css" href="/Stylesheet/Get/Profile/"/>
</head>
<body>
 <!-- the entire body may be reset by the view using it, using XPath and DOM functions -->
 <!-- Usually the PageHeader and PageFooter would be put back after clearing the body  -->
 <div id="PageHeader">
  <img src="/Image/Get/theredhead_dot_nl.png" alt="Site Logo"/>
 </div>
 <h1>www.theredhead.nl :: ModelViewController</h1>
 <p>
  Lorum ipsum dolar sit amet. blah blah blah yackadee schmackadee.
 </p>

 <div id="PageFooter">
  Built by
  <br/>
  <div><div xmlns:profile="http://www.theredhead.nl/profile" class="ProfileBadge" style="font-size : .8em;">
<a style="text-decoration : none; border: none;" href="/Profile/View/kris">
 <img style="float : left;" src="http://www.gravatar.com/avatar/5beeab66d6fe021cbd4daa041330cc86?d=identicon&amp;amp;s=32&amp;amp;r=pg" alt="Gravatar"/>
 &#xA0;Kris
</a>
<br/>
<small>
&#xA0;Rep:&#xA0;1
</small>
</div></div>
 </div>
</body>
</html>
  • I've only touched on the tip of the iceberg here and yes, I will be stripping unused xmlns attributes from the output once I'm happy with the functionality
  • there are currently just over 200 classes in my mvc and core frameworks
  • I am aware there are existing solutions that can do similar things, but I want to build my own.

So the big question is: Do you have any input on must-have functionality?

Thanks in advance for any and all input.

Kris

P.S. if anyone is interested in the complete source-code, please leave a comment, I will be providing it on my site when I reach reasonable developer usability levels.

+2  A: 

Interesting - I have a gut feeling you're reinventing the wheel, but I like the approach. Wheel 2.0, so to say ;-)

How did you implement lists of data? Input validation? That'd be my two important points.

Phil Reif
There will be ValidationControls that you could use, extend, and/or generate in CustomControls. much like the asp.net Page model, but with some MVC sauce.
Kris
+5  A: 

Why not just use PHP as your templating system. PHP IS the template system.

What is wrong with just dumping <?php=$variable;?> in an HTML template? You can use foreach loops, etc. Just make sure that you run it from within a scope that cannot access any variables you do not want.

I have a really deep founded hate for overcomplicated template systems like this since my Java/Struts nightmares. You have to dive into namespaces, xpath, custom namespaces and all that stuff before you can change just the one thing you need.

SchizoDuckie
I'm doing this because it is fun, not because i need it. It started out as an idea to prove that php is no less capable than aspx, but now it's just hobby.
Kris
A: 

Here's an article on templating engines: http://massassi.com/php/articles/template_engines/

You're doing it wrong.

orlandu63
A: 

Seems to me only Phil Reif actually read and understood the question and its intention.

Those people claiming php is the template engine and that's that are ignoring too many facts and have blinded themselves from the real world where solid frameworks are important.

So, the points must have features so far (that haven't already been implemented) are:

  1. Lists. I'll be handling those in controls similar to <asp:DataGrid>
  2. Validation. Will be handled with validation controls. Regular expressions, comparisons etc.
  3. Output is forcibly valid xhtml 1.0, at least until html 5 has sinked in.
  4. Composite custom controls (based on xml templates instead of code)
  5. inline php code... I'm considering it, <?php ... ?> is a valid xml DOMProcessingInstruction node, but the judges are undecided.
  6. Configurable global exception handling.

I've set the first drafts online so you might take a look and maybe get back to me with some neat ideas.

By the way things look i'll have forms up and running in the next couple days. At the moment it's only a first draft of a design (both code and style wise)

Still hoping for some more input here, what kind of controls do you people use and love? (from any framework/language)

Kris

Kris