Hey! I'm very interested in learning, but none of the tutorials I've found so far have covered the matter in a more deeper way, they've only scratched the surface. I wanna know more about how it works, and how to script it.
So I'm requesting some of you PHP pro's here on talkphp.net to make an article/tutorial on how to script an elite/1337/pro template class!
Well, depends on how complex you want it to be. Im planning on writing one (customized for my CMF) soon, so i might write a tutorial about it soon. ;) However, i won't make it as advanced as my class is gonna be, also since its gonna be commercial (Not alone, but with my CMF).
The problem is that a "template class" is many things for many people. The requirements for a template class can vary from project to project. Because of that, it would be nice to know what you consider should be required features (and optional/nice to have features) in your "elite/1337/pro template class".
It could end up being something as simple as the following class, or 1,000s of lines of code.
public function display($szView)
if (file_exists('views/' . $szView . '.php'))
include 'views/' . $szView . '.php';
$tpl = new Template;
$tpl->display('example'); // views/example.php
Salathe is spot on. Template files can be really extensive. However, to extend onto our simple template class, I would add the following code to store page states as well. That way you can have like so:
profile.default.php: Page to allow members to edit their profile
profile.complete.php: Page to inform users that profile update is complete
profile.error.php: Page to pull users back when they've left a required field empty
We create our views folder and then inside there we have our individual page folders, and then in our individual page folders we have the page states as listed above.
Well, I mean a class that allows me to change the template of the website with a click from a menu, like on this forum.. you can quite easially change the theme from this theme to another(ofcourse you have to import it etc etc.. but you get the point).
And make a management system, like in vB edit template feature in the admin cp, where you can change the template code quite easially.
And they're using both php and html in the same document without any tags(don't quite understand how..), they write stuff like:
if($condition['show'] == 'member')
<a href="something.php">Forum index</a>
And that's how I would want it! To easially be able to CHANGE the templates imported, via an admin cp.
So, that's my pro/elite/1337 template class idea! :)
(hard - yes, impossible - noo :D)
You then render your inner template (I call them Views - from an MVC pattern), store it in a string and push it to the layout as $pageContent. I will wrire up a tutorial for a simplified version of this soon, however it wont be for a few days as I am currently busy with other things :(
Smarty seems quite easy to use tbh, but if u really wanna do your own im sure that you could build on some of the examples above, just tinker with them, its the best way to teach yourself PHP or indeed any langauge (i mean learn by example, i dont mean copy code and just use it, that would get u nowhere).
a simple smart template could be constructed like this:
$smarty = new Smarty;//create new smarty object
//assign smarty variable Username with the value 'Sam' in the template file
//tell smarty which template file you want to use
now we need to create the "index.tpl" template page that we told smarty to use