The SitePoint Forums have moved.

You can now find them here.
This forum is now closed to new posts, but you can browse existing content.
You can find out more information about the move and how to open a new account (if necessary) here.
If you get stuck you can get support by emailing forums@sitepoint.com

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Its been said before, but their isn't much point in converting your MySQL data to XML, then on to xHTML or PDF. There are plenty of scripts out there that can make PDF or WAP files using regular PHP code.

I know that there isn't much point in that. But I think it would be a challenge to change my whole working approach. Mind you, I would be willing to do that if the rewards were truly convincing.
But one of the things that keeps me yet from doing it is that I could throw away my beloved dreamweaver (and all my other standard tools with it, as far as I see it). No advanced coding features, no assistance with designing any template stuff anymore. If I'd find some great (and cost effective) tools, maybe I would start completely anew, make all my pages in xml from scratch. I think tools like Altova's "Authentic" are a step in the right direction. Because obviously you need not only a tool to design your static pages with xml, but also a tool for your cms where you can have users type in sth easily without caring about xml.
But right now I'm not ready to jump on the band wagon yet.

hmm, I agree with Trav, what is the bloddy point of using xml files to store data that is constantly changing and being read (I see the point in using them when you needn't to read/insert data often), after all, databases are just text files that store the data in organised way so that you can quickly get/insert/update/delete data from them, why invent your own?

Didn't read the article. But I can tell you that when you need to get the data to something that doesn't have native database access in useable way, returning query results in XML format is exactly what you need. For instance getting recordsets into flash for one.

I'm not trouncing in here to advocate .NET, as I read the article with great interest, however .NET is the only solution I'm aware of that lets you separate both the HTML from the logic, as well as teh business and the presentation logic, which are both very important parts of any large-scale web project, if it's to survive past the typical 2-3 year lifecycle.

For instance, imagine if you could (for a moment) completely separate those 3 oft-integrated components: actual HTML, presentational logic and business logic.

At any time you could rewrite entire, or parts (depending on how you'd designed your app of course... I'm assumign we're all smart enough to design modularly, right?) of your business logic without affecting your presentation logic.

Likewise, you should (where most tempalte engines actually fall short) be able to write your entire HTML structure without any worries about the presentational logic.

And, extending that, you should be able to change your presentational logic without touching either the actual HTML or the business logic.

To me, that's what a templating engine is meant to do: allow the 3 classes of "Developers" (html, web and application) to work completely independently and yet as a team.

If there are any other solutions out there which allow this (I hear J2EE does to a degree, though I haven't seen examples of this, even in the Java PetStore example), I'd love to see them.

Hopefully new versions of PHP means that these kinds of systems will be available for PHP as well. Choice is good for all of us

Its been said before, but their isn't much point in converting your MySQL data to XML, then on to xHTML or PDF. There are plenty of scripts out there that can make PDF or WAP files using regular PHP code.

i dont disagree with you but what if...

1. you want to allow anyone/everyone to write "tempates" for you.
2. you want to use a standardized markup so that you dont have to waste your time writing lots of documentation/examples/specs.
3. you want to supply your template writers with a chunk of data to work with; you dont want them to have the ability to get whatever data they want.

??

Jeremy W,

J2EE can do that now that it has custom tags. CFMX can also do that; although, i dont see many people who take advantage of it.

It is completely possible to separate presentation logic from business logic without using templates.

If you attempt to separate Business Logic from presentation logic by putting the presentation logic into a template language, then the template language has to be as complicated as the language used to express the business logic.

The article author says "smarty has it closest to right", but also says "[smarty is] simply too big."

Any language capable of expressing complicated presentation logic will by necessity be a full language on its own, and therefore big.

What do you gain by using templates to separate presentation logic and business logic using templates that you do not gain by separating them using PHP?

One benefit mentioned for templates is the ability to allow "HTML designers with no PHP experience to modify the look of the site without having to look at any PHP code."

I have clients who are very comfortable editing PHPLIB style templates themselves who refuse to touch files created by smarty that has any kind of complicated logic in it.

If a required change in presentation requires a change in presentation logic (changing ifs and loops) and my html designer does not understand programming (ifs and loops), the it does not matter if the logic is in a template file or in a php file, it is still going to require a programmer to make the change.

In my book, there are strong arguments for separating PHP code into one file, and HTML markup into a different file.

There are also strong arguments for separating presentation logic into one file and business logic into a different file.

You can acheive both by having two PHP files and one HTML file.

I do not see the value in putting presentation logic in the html file. (The smarty approach.)

.NET basically copied Java and made it work a whole lot better by simplifying the process of development. I guess a big round of applause to whoever thought about that at MS. If you don't believe me, read the JSP documentation, .NET is litterally a MS'd style copy of the exact same thing. JSPs are compiled on the fly into java code, they also support tag libraries which run code on the backend.

I guess I should become a .NET developer, seems to be the right thing to do now. Java has gotten so complicated that I need a dictionary for all the Acronyms(no joke).

And yes I am a full-advocate of the next-generation MVC pattern which is so greatly done in Jakartas Struts project, all PHP devs who profess the separation of various logic should use that as a case study, then follow its lead. Seriously... Java might not be the best language in the world, but that project has it where it counts, a very scalable application design. Though I think people take PHP a little too seriously and treat it like some enterprise development language when all it is really capable of is simple scripting and emulating enterprise languages. If you want to be serious, check into Java or .NET (preferably .NET, you'll get a massive headache with Java).

The beauty of the Struts project is that your data and logic is already done and you can choose whatever you want as your "View" part which is basically some sort of templating system. You have a choice of using JSP by default or modifying it to use XSLT(Its incredibily easy to dump data into an XML file in Java for XSLT transformation, you basically serialize a object into XML data and presto your done, never have to worry about heavily modifying code to shove the data into another container) or you can use a Java templating system like Velocity. I am not telling everyone here to go grab a Java book, just showing you that Java has great examples of the correct way to do things (not language wise but by just design patterns) and we should all be taking some notes(im referring to .NET and PHP devs alike).

Personally like the article - makes the point about seperation of presentation logic from application logic well which is, IMO, a different problem to providing a "designer friendly" mechanism for customizing the look and feel of a site.

The presentation logic is usually not completely the realm of a designer to deal; in fact think it's mainly the job of the developer to decide what happens there. The problem is there isn't a clear divide between who does what and what each should be expected to know.

I think there are effectively three generations of approach solving this;

1st Generation: Smarty etc which create their own language - this includes XSLT IMO. They are all characterized by having programming language type syntax e.g.;

Code:

{if:x=y}
<p>Hello</p>
{else}
<p>Goodbye</p>
{endif}

2nd Generation: I think these are distinguished by using classes to handle all the logic of the presentation layer and bind it to the application logic layer.

The result makes the templates pretty minimalistic and encourages seperation of presentation logic from application logic.

There's only two examples of what I'd called 2nd generation template systems that I know of.

What happens when the .NET template parser reads the above template is it first encounters this tag;

Code:

<asp:repeater id="ArticlesListing" runat="server">

In response to the tag it instantiates the Repeater class which is a presentation logic class. The Repeater object then instantiates the ArticlesListing class which is an application logic class which will be used to fetch article data.

Inside the opening and closing <asp:repeater /> element, all elements found there will be used to build the output for as many rows as the ArticlesListing class delivers.

Also inside the repeater section of the template are tags like this;

Code:

<%# DataBinder.Eval(Container.DataItem, "Title") %>

This is essentially what extracts elements from an ArticlesListing row and implants them in the page. I guess "DataBinder" is a Singleton object, globally available, and "Container" is the object reference that the Repeater class instance is stored in, while "Container.DataItem" is where the reference to the instance of the ArticlesListing class is stored.

We're still talking a language here which a designer would have to learn but it's got an XML-like syntax which in many ways infers the control structures. The tags refer to presentation logic classes which solve practically all common template related operations - and that's where the money comes home for MS - by having a well defined set of tags and classes for templates, they can then sell tools which make web page design a question of "drag and drop" - that's compelling reason to use ASP.NET because it makes life alot easier. It's worth noting the PHP has no such tool - there are PHP editors which are good for PHP source but where are the drag and drop page designers? The problem is PHP doesn't have a standard set of presentation logic classes, which .NET does. Even outside of PHP, I've found no page design tools that allow you to layout a page but define your own tags which your own template parser will use to load classes for handling the tags. That surprises me to be honest - there's only a limited set of widgets and control structures associated with an HTML page - why someone hasn't designed an editor that allows you to specify your own tag naming convention I don't know.

Don't let that sound like doom and gloom for PHP though. Remember ASP.NET is a templating language just like any other, in the end. If it can be parsed by .NET and can be parsed by anything. What sort of parser MS use I'm not 100% sure - it may be DOM based or SAX based but I am sure it's some form of XML parser. I've tried a variety of approaches with DOM so far, none of which I've been satisfied with - PHP's DOM still isn't stable enough for this problem. But SAX wise things are looking good - using SAX filters, as with <html><a href="http://www.phppatterns.com/index.php/article/articleview/48/1/2/" target="_blank">converting XUL to PHP-GTK</a></html> it's very doable - I'm working on a prototype right now which can already to simple things with ASP.NET pages. I'm not going to reproduce the entire .NET presentation logic classes but I'm hoping to get it to a point where it can be easily extended then perhaps submit to PEAR and see what happens. The big incentive for doing this one is to make the ASP.NET web design tools available to PHP coders.

The other second generation template system I know of is eZ publish 3.x. Here's an example equivalent to the .NET example;

Having said that, if eZ systems get the time they could probably also develop a drag and drop IDE for eZ publish, assuming they construct a standard set of basic "widget" templates or better yet, write native PHP classes to do the same.

The main difference between ASP.NET and eZ publish 3.0 in terms of how the templates are used is with ASP.NET, everything begins with the template - the template tells .NET which classes to load and where to get started.

eZ publish begins with the request URI and uses it to decide which template to load, which in turn specifies the application logic classes. In short eZ publish is an MVC framework while ASP.NET isn't.

Read somewhere in the Petshop wars that MS don't believe MVC is a good approach for web applications - they may have a point as implementing an MVC architecture means taking control of serving pages away from the web server and handling it in your code. For a Java Servlet this is required but if you've already got a web server, why re-invent the wheel?

The problem with both of these template systems is there's alot of syntax to learn, which makes them fairly difficult to hand code but for MS it means they can sell their tools.

As to the Java Standard Template Library, by my definition I think I'd call that a generation 1.5 system - there's still plenty of control structures placed in the template itself.

3rd Generation: Pure CSS. If web browsers all support CSS fully, the whole problem basically does away. As web developers we can simply output a ton of <div /> elements and nothing else. A web designer then has full control over layout using a CSS page e.g.;

Using CSS, a designer has complete control over the positioning and look and feel of the above "table". My guess is you could probably do it already with Mozilla. If you check the source of Simon Wilson's weblog: http://simon.incutio.com/ he's pretty much doing this already.

Of course we don't have to reduce everything to <div /> tags - right now we'd still need the form elements but I wonder when you'll be able to determine how an element looks with CSS?

The designer still has complete control but the developer is now a big more involved in the design process, deciding what type of tags should be used.

More importantly, the designer now has absolutely no part in what goes on in the presentation logic layer - it's up to the developer to make the division between application and presentation logic correctly - the designer purely does layout, look and feel. The template engine can now be thrown out of the window - this can be done with pure PHP.

The problem today is IE doesn't fully support CSS and perhaps CSS needs another version anyway to give designers everything they need.

Phew - long rant - looking back still don't think I've got this one fully pegged.

And yes I am a full-advocate of the next-generation MVC pattern which is so greatly done in Jakartas Struts project, all PHP devs who profess the separation of various logic should use that as a case study, then follow its lead.

Harry's starting to understand .NET, but is still a long way off... :sigh:

If it's wrong tell me where. One thing on further review that's not quite right is this;

The Repeater object then instantiates the ArticlesListing class which is an application logic class which will be used to fetch article data.

After reviewing some of the sample code as asp.net, ArticlesList would also be an application logic class which is already instantiated at the top of the page. ArticlesList is just the reference to the object, declared with something like;

Code:

public HtmlContainerControl ArticleList;

Seems you have to bind the data to this in the first place to be able to access it later within the template.

Would be interested to see a simple version of the ASP.NET template above backed with some C#.

Isn't struts kind of the same as the ASP.NET Web Forms framework? I haven't looked at struts too closely, but I would love to know what the differences are!

I kinda touched on that in the above post (you got there moments later);

Read somewhere in the Petshop wars that MS don't believe MVC is a good approach for web applications - they may have a point as implementing an MVC architecture means taking control of serving pages away from the web server and handling it in your code. For a Java Servlet this is required but if you've already got a web server, why re-invent the wheel?

For me struts is characterized by there being a central piece of code/PHP script which is the first point of contact for all page requests. This script works out the next level of decision making that has to be made then calls the code to make the decision, which in turn calls more code and so on. Perhaps the measure of struts is how form validation can be handled - you can submit a form to a completely different URL within the site and still have it validated.

My understanding of Web Forms, in terms of how MS expect you to use it is the form validation will happen via the same script that rendered the form in the first place as shown here. This is much like PEAR::QuickForm. Of course that doesn't tie you to doing it this way - you could write your own version of struts using Web Forms (as you could with QuickForm if that's your pleasure) but you'd have to seperate the validation from the presentation of the form.

Struts has the advantage that once you know how it's being implemented, it provides a very rigorous and flexible framework to build on. But it's often very complex; the abstractions it introduces is not going to appeal to someone who only knows a little HTML. Struts I guess is a product of Java Servlets, which may be an important detail.

I guess eZ publish 3.0 is now a form of struts - check out how they handle validation, for example, it seems to be on a completely site wide basis depending on the custom data type you use / define (e.g. data type "eZEmail" is automatically validated every time an email address is received from any form, anywhere). There's certainly a pretty steep learning curve for developers with ezp3 although for site administrators, it's Content Class system is extremely powerful.

But who knows - MS may be right that MVC (the struts architecture) is wrong for web sites, given that a web server has an in built "decision making" mechanism which will pass requests for the right page to the right script. This is certainly a down to earth approach which makes it easy to find the code which handles a request.

For PHP, been thinking perhaps we should throw out the whole idea of using index.php as a central place for all incoming requests and instead use the php.ini setting "auto_prepend_file" to handle all "global" operations such as loading general data access layer classes and user authentication while leaving the specifics that happen at a particular URL to the script that resides there. That could also include form validation at a "global" level. That approach would be perhaps ideal if you're not using an application framework like eZ publish.