Page 1 of 1

Templates; caching

Posted: Mon Dec 06, 2010 9:57 am
by Technical
Hello, I'm new to templating. I'm writing little CMS.
I've started to separate HTML from PHP and stuck with a little problem: what generally should be kept out of site design templates? Should I put form generating upon designers or generate it with PHP? Should I put basic page layout (<html><head>...) in CMS theme's templates? If not then how should I store that HTML code: right in PHP files or put dedicated template folder?
P.S. Sorry for bad English.

Re: Templates

Posted: Mon Dec 06, 2010 10:31 am
by AbraCadaver
Technical wrote:Hello, I'm new to templating. I'm writing little CMS.
I've started to separate HTML from PHP and stuck with a little problem: what generally should be kept out of site design templates? Should I put form generating upon designers or generate it with PHP? Should I put basic page layout (<html><head>...) in CMS theme's templates? If not then how should I store that HTML code: right in PHP files or put dedicated template folder?
P.S. Sorry for bad English.
Several questions here:
what generally should be kept out of site design templates?
Generally, logic that is not specifically for display, business/control logic should not be in the template.
Should I put form generating upon designers or generate it with PHP?
This is personal preference. I guess if you want to force a specific form layout/structure then generate it with PHP. If you want HTML only designers to be able to load the template in a WYSIWYG editor and modify it, then probably HTML.
Should I put basic page layout (<html><head>...) in CMS theme's templates?
Again, personal preference, but if you have one "header" and one "footer" then this saves on duplication and possible bugs. You can then programmatically control what title, keywords, etc. are displayed.
If not then how should I store that HTML code: right in PHP files or put dedicated template folder?
I would have a separate template folder.

Re: Templates

Posted: Mon Dec 06, 2010 10:52 am
by Technical
Thanks for the reply.
So I will create system template folder with HTML code that doesn't affect page design.
I also decided to make Controls class, that I will use to create links, buttons, form inputs and etc. Do you think it's a good decision?

Re: Templates

Posted: Mon Dec 06, 2010 12:01 pm
by AbraCadaver
Technical wrote:Thanks for the reply.
So I will create system template folder with HTML code that doesn't affect page design.
I also decided to make Controls class, that I will use to create links, buttons, form inputs and etc. Do you think it's a good decision?
Many frameworks do this, and it does make things easier on the programmer and more dynamic.

Re: Templates; caching

Posted: Tue Dec 07, 2010 8:24 am
by Technical
Okay, thanks for replies. Now I got second problem - cache.
What exactly should I cache? Is caching database query results for each page and access level a good idea?

Re: Templates; caching

Posted: Thu Dec 09, 2010 4:16 am
by Zyxist
Technical -> contrary to the people above I disagree that form HTML code generation should not be a part of template engines. Such a practice is very hard to customize and basically reinvents the wheel: you must change the form layout in a completely different way, than layouts of other widgets. The only benefit I see is when you simply want a form, and the default configuration suits you. This is what I dislike in most of frameworks, but I understand why they do so: they neither have a good template engine nor time to develop it from scratch.

As I've been developing one of template engines for several years, I've been experimenting with moving the form rendering part to templates (and keeping the form logic in PHP). This requires some extra things to be added to the template language, but the general effect proves to be much more flexible and intuitive than using OOP and pure PHP for form generation.

The second question: generally caching database results is a good idea, but you must remember to remove outdated files when you modify the particular resources. In many cases, caching the template engine output (or at least its part) is also possible. A situation when it is hard to use cache are personalized and user-specific pages, which depend on the logged user settings. In this case using cache would be very inefficient, because it could produce lots of cached objects that are almost unused, because they apply to a single user only.

Re: Templates; caching

Posted: Thu Dec 09, 2010 4:54 am
by Technical
Thanks for the reply. I'm staying with caching database results only because of user's custom settings. Honestly, I think that caching plain output is quite dire.

Re: Templates; caching

Posted: Thu Dec 09, 2010 5:05 pm
by Christopher
Zyxist wrote:Technical -> contrary to the people above I disagree that form HTML code generation should not be a part of template engines. Such a practice is very hard to customize and basically reinvents the wheel: you must change the form layout in a completely different way, than layouts of other widgets. The only benefit I see is when you simply want a form, and the default configuration suits you. This is what I dislike in most of frameworks, but I understand why they do so: they neither have a good template engine nor time to develop it from scratch.
It is not clear what the distinction is between Template code and what many people would call Helper code. I think most frameworks have loadable helpers that do some of these less common "templating" functions.

I am also not sure how this affects layout much. In the cases where I need to generate a form field server-side, I simply set the id or class of the tag and let the CSS determine what it looks like.

Re: Templates; caching

Posted: Sat Dec 11, 2010 2:46 am
by Zyxist
You are probably talking about the <INPUT> or <TEXTAREA> tag only, where setting ID and CSS class is the only thing you can actually do to customize the look. But these tags are not presented in a complete vacuum. There are some other HTML tags that determine, where the title is displayed, where errors are shown and how they affect the layout, where to display additional information, some help probably. When you generate a single form field tag, like <INPUT, then yes - helpers are enough and this is how I do it. But rendering the whole neighbourhood in this way leads to serious problems, if you want something more than "Tesco Value Standard HTML Form". Any non-standard layouts or extra additions are much harder and unnatural to add, than in case where the template engine lets you to handle it.