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.

I'm seeking for a template-engine that uses XML/XHTML compilant tags in his template files, e.g <myvar value=2 />, <myvar />, <if myvar>HI!</if> or something like this. Do you know anyone???

Everybody know that Smarty uses template tags like {$myvar} that are assigned for engine recognizes it. Thus, would be possible to modify the Smarty code to accept template tags like <myvar /> once this tag was assigned for engine???

I've got no idea who to agree with on this one. On one side there's the template zealots, however, templates IMO are cumbersome, slow, difficult to read and have less control. On the other side is Vincent with his class lay out, problem with this, is it all adds an extra layer to PHP. PHP was designed to be easy!

This is how my site runs, and it allows me to have a common layout, while still maintaining SIMPLE and easy to read code, and being faster than all other methods you've got. There is no reason why an extra layer has to be added onto PHP. If, you have the programming skill to add this extra layer, then you should problably implement it in your own programming language, but instead of coding in C, code in assembly so it's nice and fast.

BTW, why does it matter is PHP shows up in dreamweaver or not (It does)? As, web site designers should not be using a WYSIWYG editors? If they are using WYSIWYG and they are getting paid for it, then personally i think that isn't very professional. Only basic layouts, first designs should be done in WYSIWYG editors, where as things like details, elegant code, and designing with PHP, should be done the old fashion and better (This is a very rant'able topic) way; notepad

BTW, I'm no where near qualified as some of you, with only 2 years of PHP programming, and some other programming languages.

Originally posted by marcoBR I'm seeking for a template-engine that uses XML/XHTML compilant tags in his template files, e.g <myvar value=2 />, <myvar />, <if myvar>HI!</if> or something like this. Do you know anyone???

well I am glad that I stumbled on this thread which has explained everything I needed on templates. I too am pretty new to PHP and any programming in general, I have been asked to look into creating templates for a site. Being that I no the name smarty I thought that I would have a look at what people thought about it... I am so glad that I did.

Like Peter all my pages started off like his, but I am now having to create multiple pages and functins and well to be honest I am getting myself lost in my code code I know terrible, and needing to re-train how I write the code so that its simpler for me and also for any clients / others that need to use it.

Vincent, I am afriad that due to my real lack of knowledge on OO etc your example didn't make too much sense, I think I need something more basic (not in this thread guys don't worry) just really asking for soem pointers on where to find basic example, tuts etc (mayeb I will reread the SP ones) and also any decnet book son teh subject.

Like most people I have Kev book but am finding that it hasn't really taught how I should be laying out my code seperating php and html etc and I am starting to get in a muddle - also errors are occurring which I think is from my code not being written exactly correct.

Anyway I am enjoying this thread so I do hope it continues, although going back to the beginning, I do have to make one point.

You mentioned that you design all yoru sites in css only and to change the site you essentially don't touch the html, how? Maybe it because I have to set my sites to work in NS4 and IE4 (client restriction) I find this impossible (although I wish that it was true) just thought that I would ask incase I was missing out on something

I shall leave the ranting to others but kepe up the good work this thread seems to have helped many people .. hoefully many more

I'm with Vincent here, complex template systems like Smart are overkill, if a designer can alter a template with smarty tags in, then they can alter it with basic PHP e.g.

Code:

<title><?=$pageTitle?></title>

and

Code:

<title>{pageTitle}</title>

They are not that different that a designer can't work out that both of them signify the title and move it round to somewhere else in the presentation. If they are going to learn templating syntax they may as well learn some very basic PHP and we are talking very basic here.

Personally I use XML/XSL for my "templating" along with some output caching as well.

Why XML + XSLT? - Well the simple answer is that with XML + XSLT I can specify another XSLT file to render with in my config file so I can output to any format. I could detect WAP browsers in my apps init routine and specify to use the XSLT for WML output, or I could allow a user to select a style sheet in their preferences that is more suited to a screen reader or to visually impaired persons - Now let's see you do that with Smarty easily

It's all a case of getting as near to the basics as possible whilst mainting a balance between usability, maintainability, reliability and efficiency - Something no PHP book I have ever seen teaches you.

There's a lot more to building software then just writing code, I realised that even more when I started my Software Engineering Degree - Programming and coding are two wholly separate things

As a side issue, I can't wait for the ZEv2, PHP will finally have mature OO support and efficient OO support too.

Well I will say I agree with vincent also. I am currently working with a couple bigger projects and we are redoing our whole engine to be oop. I sure hope it is more or less up to vincents standards as I really like his work. Anyway I have expanded on the template benchmarks and am in the proccess of adding more.

This thread is interesting to me as I will be doing the new theme engine for the core. We have made it so that we can plug in any template engine but we were also unsatisfied with the solutions out there. In my quest to know more about it I have collected about 30 or 40 template engines and about 20 different cache systems. Not all of them are benchmarked yet but I'm working on it.

Anyway after seeing this I think the one I work on will be amazingly allot like what vincent has discussed above. Thanks vincent for the ideas as I have always believed that anybody can make anything but a genious is the one that makes it work the simpelest way possible. Most of us have a tendancy to over complicate things.

Personally I use XML/XSL for my "templating" along with some output caching as well.

Why XML + XSLT? - Well the simple answer is that with XML + XSLT I can specify another XSLT file to render with in my config file so I can output to any format. I could detect WAP browsers in my apps init routine and specify to use the XSLT for WML output, or I could allow a user to select a style sheet in their preferences that is more suited to a screen reader or to visually impaired persons - Now let's see you do that with Smarty easily

One thing to consider there may be applying an XSLT "on top of" the finished page, assuming that its XHTML (well formed XML). That is rather than generating XML from the database then applying XSL to generate the desired output, we choose XHTML as the main output format then apply XSL on top of that as needed...

the case of phpBB templates

The first template engine I worked with was the phplib engine used in phpBB templates. At first, like many, I thought it was amazing. I could edit these template pages and voila, the result would show up on my forums and I never had to mess with the php code.

Now that I have "matured" a bit in my understanding of templates (thanks mostly to Vincent) I realized a very, very critical point. Templating is NOT an engine, it is an idea. Pulling the presentation layer away from the code so that you don't have to modify the source code to get a different look can be done using PHP+CSS because it is not an engine, but simply a way of dividing up the tasks.

This actually became apparent to me earlier while making a "MOD" on my board. I realized that if you want to change the presentation of the forum, it is actually necessary to modify both the code and the templates. It is the case that the templates can actually get "outdated" as the code refactors. To me, this is not right.

The templates should be "logic-free" widgets. You should probably have one main template that does the primary painting on the page (header, footer and sidebars) and then for each widget you add, it should populate and insert the contents of its result. For instance, if my application had a list page, where I showed a paginated list of items, the "list" template should be used. That way, if I later added a new "box" on one of my pages that showed a list, I would not have to change the widget template (unless of course the template was bad to begin with). Look at this forum and try to put your finger on the different widgets it uses, and make a template for each of them. Many of these templates will be very small, and you simply include them when the data is ready and buffer the result to be used in the main template.

This follows what Vincent pounds in our heads of code reuse. If you think about it, phpBB templates repeat html code all over the place. They have a template for posting, template for viewing forum list, template for viewing message list, template for searching, template for search result list. That is TONS of repeated presentation layer code. If they had narrowed it down to widget templates (whether the tempates are PHP or some language it doesn't really make a difference) they could have reduced the amount of work the template artist had to do.

So whether you choose to use PHP (like me) or you choose to use "insert favorite template engine of choice," you should think of your templates as widgets instead of as page-specific templates, with one or two "main" templates that wrap the entire page in a basic layout. And the nice thing about this method is that you will see your widget "refactor" themselves as new situations present themselves, but never will you have to modify 20+ templates to make a new style. Try to consider "merging" similar widgets together that only differ by class definitions and come up with standard class names just like Qt or GTK. For instance, if you use a sideblock, but some are red and some need to be green, instead of making several templates for almost the same code, try to make the classname dynamic on the block or something like that.

By the way, if you are interested in a template engine that is just php with alternative wrapper tags, check out HTML_Template_Xipe in PEAR. All it does is let you change the tags used for php, eliminates the need for brackets by using indentation and does courtesy "echo" statements when a simple variable is enclosed in the tags. It is simply a layer between the designer and the cached version which allows you to type less and get a php result page. In the end I will just type the php thanks and skip the processing, but since they can be cached outside of the program being run, you can just create the template when the designer hands it to you and drop it in place (maybe use a commandline script).

I agree with the fact that 3rd party template parsers are mostly useless with the fact that they have a ton of features which could **Possibly** be useful for **some** project that **someone** out there **might** want to do.

A template system needs to be more specific than that. My template system does exactly that. The logic it supports are if/else tags, as well as including other templates directly within a template, using an <include> tag. It also does allow use of PHP code within the template, if it is enabled, and if it doesn't need to be used it is disabled, Also, templates are only parsed once, and page execution for a full forum thread is about 0.18 seconds, (with all features enabled).

This is hardly overkill in my opinion, and does allow the average user to make changes easier, with XHTML-style tags.

One thing I really hate about Smarty is it's dumb syntax, especially If statements, in mine it is direct PHP syntax:

Code:

<if $hello==1><include template="header" /></if>

versus a similar PHP Code:

PHP Code:

<?php if ($hello==1)
{
$template->include('header');
}
?>

and then there's the dumb smarty method:

Code:

{if %Hello% eq 1}something weird...{/if}

I don't think templates are right for everybody, but I think it is useful in some cases. It's like everything, sometimes things are right for a situation, sometimes not, that's the way it goes.

<end my opinion>

Who walks the stairs without a care
It shoots so high in the sky.
Bounce up and down just like a clown.
Everyone knows its Slinky.

Smarty can do if statements that look like this, without the % and 'eq':

PHP Code:

{if $hello == 1}{include file="header"}{/if}

I don't see how this is anymore "dumb syntax" then your

PHP Code:

<if $hello==1><include template="header" /></if>

If all your template does is simple if / else statements and perhaps loops, you might as well use PHP syntax for it.

I don't think templates are right for everybody, but I think it is useful in some cases. It's like everything, sometimes things are right for a situation, sometimes not, that's the way it goes.

IMO, templates are always useful as soons as your script goes anything beyond "Hellow World"-complexity. It's just that template engines aren't always needed. Unfortunately a lot of people mix these two terms in their posts, while they are totally different concepts.

actually it doesn't do loops either, and I didn't know Smarty could use that sort of if syntax, i take back my previous statement, but I still think it has way too many features to make it a useful solution.

Who walks the stairs without a care
It shoots so high in the sky.
Bounce up and down just like a clown.
Everyone knows its Slinky.

It might make the initial compilation of the template slower as the regexp needs to be more complicated to support it, but from there on you are just executing native PHP until the template changes again. Smarty will only ever parse your template once if the template never changes.