Nov 23, 2009

Creating A Template Engine - General Advice?

free web hosting
Open Discussion & Free Web Hosting > Computers & Tech > Programming > Scripting > PHP

Creating A Template Engine - General Advice?

Mordent
So while in the process of revamping some of my websites I've decided to have a good shot at writing my own PHP/HTML template engine. For those who aren't sure what sort of thing I'm talking about, have a look at Smarty and think of a slightly toned down version of that. My question for you is what do you consider necessary in a simple template engine? On the basis that it'll be used by one-man (namely me, but I'll likely show people what I come up with if it works out to be any good), someone who has both knowledge of PHP and HTML etc., what sorts of features would you consider as "standard" in a template engine?

The main question I have for you is, really, the differentiation between a template and the code that uses the template. As far as I can tell there are two different schools of thought on how each should be done:

The first one suggests that all the programming bits (namely the PHP) should be separate from the HTML, whereas the second (and the one I think makes more sense) is that all the logic code should be kept separate from the display code. There's a subtle difference between the two:

What constitutes as "logic code" will almost certainly be entirely PHP, as are the "programming" bits, so the two methods agree in this respect. The difference occurs when it comes to the templates themselves: the first suggestion implies that all PHP (so <?php ?> tags) should be in the programming bit, with a much simpler variable implementation system for using the variables set by the coding section directly to show what's needed. The second suggestion makes a strong differentiation between the language and the purpose. Logic code and display code are two different things from PHP and HTML.

Overall, I certainly agree with the second of the two, although the main disadvantage of it is the requirement for the template designer to have some knowledge of PHP. Is that such a bad thing, though? You don't have to know much, perhaps a bit of variable manipulation and looping? There is, of course, the other disadvantage that by allowing the person who works with the templates access to directly run PHP code, so that could potentially post a security risk. That said, if you trust someone enough to upload templates to your site, surely that's not really a problem. Similarly, if you're to do most of the work yourself it's equally not a problem.

Has anyone attempted to make a template engine before? If so, how did it go? If not, do you use another one, and have you had any luck with it?

 

 

 


Comment/Reply (w/o sign-up)

Quatrux
Well, the template engine I'm using is a long time ago written logic structure by me, I didn't even use smarty.. even though smarty is much much smarter than my system, but what I'm using is Output buffering, I can catch the outputted html or parsed code, I can play with it using different functions and I have a callback function which will evaluate in the end of the code, I always have a template file or several files of the same template/site, where different parts of it is looking something like this: HTML code {description} HTML code {title} HTML code {section} and lets say I've got something like a variable $a = array('{section}' => $section); which is used for that purpose, of course it's not as easy as it can look here, you can add or append different values for it, also I'm usually using PHP code in the Template files, because using only HTML or {something} is a bit not so organized, usually the code in the Template files looks like:

if (something) then something or HTML else something HTML.. I call a function/method or I output something different for that situation in the Template, it's much easier to understand that in the template, rather than always adding a {something} and replacing with what should be done, for other people it might be hard to find where the hell is that {something} coming from.. For this kind of logic looking much more organised you should use OOP, create a class and use it or use more than one.. I don't know if it's the best method, but it works and it's really fast, I never get a very big page of code over 1 second, sometimes using 100 includes is slower than replacing parts of template..

I also created a Publishing, my current CMS can publish all the files into HTML only files logically, all using Output buffering and manipulating it, if you want to read more about OB, then have a look at: http://www.php.net/ob_start

 

 

 


Comment/Reply (w/o sign-up)


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

This textarea will convert to Rich-Text automatically (IE, Firefox, Chrome)

Similar Topics

Keywords : creating, template, engine, general, advice,

  1. Login Script
    PHP Help #3 - Need help creating one (5)
  2. Xgrid With Php
    Creating a script to post a blender file to Xgrid using PHP (0)
    I am doing pre planning for the blenderxgrid.com script. I was originally going to do this with
    PERL, but elected against it. Eventually I'll be moving the site off astahost and replacing it
    with a website hosted on the same machine as the xgrid controller. I am setting up a test version
    on my latop using OSX's apache server, MySQL, and PHP on a localhost config. Here is my step
    list for the script: ------------------------------------- Form: (within Xoops CMS, so user will
    have to be logged in) Username Password (where they can upload the .blend file....
  3. Help: Php + GD - Creating A Progress Bar
    Creating A Progress Bar (5)
    I've recently been trying to read up on using PHP and GD to create PNG's on the fly.
    I've been doing so in hopes that I will be able to learn how to compile a script that will
    output an image of a progress bar with certain attributes based on the url that called it (which
    will be accessed predominately from a forum, so it must be able to be coded as standard BBC). These
    attributes would include the bar's title (based on the event mentioned in the url) printed
    centered and right above the meter, a background image (to hold the meter), the meter (the ....
  4. Need Advice On Good Free Blog Scripts
    and good ones? (7)
    Anybody know of any free good blog scripts that are easy to install and use, that can also fit
    easily into your website layout? This is really important so if anyone knows can you please help me
    asap... thanks in advanced.....
  5. Permission Denied In Creating A Directory
    mkdir() (7)
    QUOTE Warning: mkdir(/home/whistle/public_html/see/seeto): Permission denied in
    /home/whistle/public_html/see/config.php on line 56 My web hosting is on the astahost. The
    absolute path of my php script is "(/home/whistle/public_html/see". When I try to create a directory
    named "seeto", the error message about permission denied is displayed. The same script I tried on
    another web hosting server "Lycos", it works perfectly. Can anyone tell me what is wrong with it?
    Or anything I should notice more? Thanks. CODE if (mkdir("/home/whistle/public_html/see/seeto....
  6. How Do I Create And Write To Files?
    creating, writing, deleting files (4)
    Hi, Can someone please tell me how to create files and write to them in PHP. I just want to create
    a simple file containing text, and then be able to read it or update it. Thanks Alfie....
  7. Free software for creating PHP sites
    (17)
    This is good for those who doesnt have knowledge bout PHP they are totally free here are my 2 know
    free web portal system Postnuke -> www.postnuke.com and PHPnuke -> www.phpnuke.org /tongue.gif"
    style="vertical-align:middle" emoid=":P" border="0" alt="tongue.gif" />....

    1. Looking for creating, template, engine, general, advice,

See Also,

*SIMILAR VIDEOS*
Searching Video's for creating, template, engine, general, advice,
advertisement



Creating A Template Engine - General Advice?

Affordable Web Hosting, Low cost Web Hosting - ComputingHost.com