Michdd Posted July 27, 2009 Share Posted July 27, 2009 I'm always looking for ways to do things better, improve and learn. Before getting into creating a template class I never really looked at any tutorials or anything. I just created my own based upon the principles of it. I'm just curious as to how some of you accomplish this. The way I generally do it is something like this: $templateObjects = Array( 'some_file' => Array( '{EXAMPLE_1}', '{EXAMPLE_2}' ) ); class Template { var $template; function __construct($title) { $this->template = str_replace('{PAGE_TITLE}', $title, file_get_contents('template/html/main.html')); } public function setContent($section, $content) { $this->template = str_replace($section, $content, $this->template); return true; } public function display() { echo $this->template; return true; } } function getContent($template, $content) { global $templateObjects; $structure = @file_get_contents('html/' . $template . '.html'); if(!$structure) return 'Error: Template file not found (' . $template. '.html)'; return str_replace($templateObjects[$template], $content, $structure); } The way it would work is I'd have a main template file (main.html), that would contain the skeleton of a page, outlined so breifly enough so it can be used for all pages. Say within this skeleton I had something called {MAIN_CONTENT}, and I wanted to replace that with the contents of some_file.html while replacing {EXAMPLE_1} and {EXAMPLE_2} with "Hello" and "World" I would do something like this: //Create class instance $template->setContent('{MAIN_CONTENT}', getContent('some_file', Array('Hello', 'World'))); $template->display(); How do you do it? Is mine appropriate and is there anything I could do better? Link to comment https://forums.phpfreaks.com/topic/167701-template-class-discussion/ Share on other sites More sharing options...
waynew Posted July 28, 2009 Share Posted July 28, 2009 You shouldn't be using var $template; Should be public, private or protected. Link to comment https://forums.phpfreaks.com/topic/167701-template-class-discussion/#findComment-884698 Share on other sites More sharing options...
gevans Posted July 28, 2009 Share Posted July 28, 2009 I never tend to use template scripts. The amount of effort that goes into writting one (or learning someone else's) doesn't really compensate for the time saved (if there is any) of writting the template code in place of php. Imagine a for loop used with a template script; {foreach:items as item} <div> {item} </div> {endforeach} Where a comparable php script; <?php foreach($items as $item):?> <div> <?=$item?> </div> <?php endforeach; ?> Please note I wouldn't recomend short tags in just any part of your development. I'm very reserved and only use it in view files to save myself time, help out my designer, and keep the view looking tidier. And what if someone else needs to do some work on your project? Suddenly they have to learn a random a brand new language, your template language.... :-\ Link to comment https://forums.phpfreaks.com/topic/167701-template-class-discussion/#findComment-884725 Share on other sites More sharing options...
waynew Posted July 28, 2009 Share Posted July 28, 2009 I hate learning other people's template solutions. If you are going to use a solution, use Smarty. But what Gevans said is spot on in regards to template solutions not being needed. Link to comment https://forums.phpfreaks.com/topic/167701-template-class-discussion/#findComment-884737 Share on other sites More sharing options...
Zyx Posted July 28, 2009 Share Posted July 28, 2009 Technically speaking, there could be many things that need to be improved and optimized in your class. First of all, you call str_replace() every time you want to display a variable in a template. This function can take arrays of items to be replaced. Add one method to assign new variables into an array, and replace them all at once in display(). Another problem is that you mix template engine logic with the things that should be done by your script by using the class. For example: why did you put this line into constructor: $this->template = str_replace('{PAGE_TITLE}', $title, file_get_contents('template/html/main.html')); And why getContent() function is outside the class? Think of the class like a kind of tool or device, because now you've created something that resembles a hammer with a head separated from the handle . gevans -> smart words, but they apply only to template languages that are in fact a PHP syntax in curly brackets. There isn't any rule that limits the features of the parsers written in PHP to a subset of PHP - such parsers are just much simpler to implement. Some areas where a new template language could beat PHP: * Understanding HTML/XML - much easier way to manipulate tags and attributes. * Hiding the implementation details - with for, foreach and if, you can just write an algorithm that displays something. The algorithms are not very complicated, but quite annoying in more complex websites. Template engine can provide ready solutions built-in into the language syntax. * Automatic HTML escaping in the displayed script data. * Performing some operations during the compilation, so as a result, we get better performance. PHP in fact has nothing interesting that helps writing templates. Loops, functions and conditional instructions appear even in C... Link to comment https://forums.phpfreaks.com/topic/167701-template-class-discussion/#findComment-884850 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.