Templates; caching

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
Technical
Forum Commoner
Posts: 81
Joined: Thu Dec 02, 2010 5:30 am

Templates; caching

Post 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.
Last edited by Technical on Tue Dec 07, 2010 8:15 am, edited 1 time in total.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Templates

Post 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.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
Technical
Forum Commoner
Posts: 81
Joined: Thu Dec 02, 2010 5:30 am

Re: Templates

Post 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?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Templates

Post 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.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
Technical
Forum Commoner
Posts: 81
Joined: Thu Dec 02, 2010 5:30 am

Re: Templates; caching

Post 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?
User avatar
Zyxist
Forum Contributor
Posts: 104
Joined: Sun Jan 14, 2007 10:44 am
Location: Cracow, Poland

Re: Templates; caching

Post 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.
User avatar
Technical
Forum Commoner
Posts: 81
Joined: Thu Dec 02, 2010 5:30 am

Re: Templates; caching

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Templates; caching

Post 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.
(#10850)
User avatar
Zyxist
Forum Contributor
Posts: 104
Joined: Sun Jan 14, 2007 10:44 am
Location: Cracow, Poland

Re: Templates; caching

Post 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.
Post Reply