tags:

views:

46

answers:

2

I'm using HTML_Template_Flexy in PHP but the question should apply to any language or template library. I am outputting a list of relatively complex objects.

In the beginning I just iterated over a list of the objects and called a toHtml method on them. When I was about to have my layout designer look over the template I noticed that it was too opaque and that he would have ended up looking through and/or editing many additional php source files to see what really gets generated by the toHtml method.

So I extracted most of the HTML strings in the php classes up to the template which made for one clear file where you can see the whole page structure at once. However this causes problems when you want to add an object to the list using javascript. Then I have to keep the old toHtml method and maintain both the main template and the html strings at the same time, so I can output just the HTML for a new object that should be added to the page.

So I'm back to the idea of using smaller templates for the objects that make up the page, but I was wondering if there was some easy way of having my cake and eating it too by having one template that shows the whole page but also the mini-templates for smaller objects on the page.

Edit:

Yes, updating the page is not a problem at all. My concern is with having both maintainability and transparency of the template files.

If I have one single template for the whole page, then I must maintain mini-templates of the objects that are shown on the page.

If I just have the mini-templates and include them from the higher-level template it becomes more difficult to look at the top-level html and imagine what the end result will look like.

A: 
  • You don't explain the problem you have adding and object to the list with javascript. There should be no reason you can't add objects to the full document using javascript. You just need to find (or add) the right handles to the HTML so you can do it (classes, ids, or even comment-blocks depending on the situation...).

However, that doesn't solve the problem of maintainability. I assume you want small templates because they're easier to maintain.

  • Maybe you need to write a small testing framework that will allow your layout designer to view the overall template given fixed sets of data input. e.g., given dataset A, the full template will be A*, given dataset B, the full template will be B*. That is, have your renderer render templated HTML rather than real HTML.

Maybe that's over-engineering your issue, but you might need to clarify the problems.

Glenn
A: 

Ok as far as I understood, your Problem is, that you have parts (e.g. a list) on your HTML page template that has exactly the same source code as the HTML you output your objects for AJAX requests and you don't want to maintain them twice.

Why aren't you consequent then and query your objects ONLY with AJAX (my favourite would actually be to template your view on the client side in JavaScript, but that is just a personal preference)? Meaning, that you just need to template your pieces in PHP and combine them only through AJAX requests in JavaScript.

Daff
I have also considered this approach. However, on page load my application already has to go through most of the data in the application's database to be able to get the values for some properties of "higher-level" objects that depend on "lower-level" objects. So I figured since I already get all this data once I may as well output it all to the client.
Sam
Ok that makes sense but why don't you generate your data as JavaScript Objects (which you output directly on the page) and render the HTML on the client side?Another Option would be to separate your big template into pieces (in PHP) which you just include into your big template or render directly on an AJAX request. So you still have to maintain them only once
Daff