Stevenn Posted March 20, 2012 Share Posted March 20, 2012 I've made a template system, but the way I've chosed to do it doesn't give me the ability to choose a design. I've posted the code below with a bad solution for my design problem. Does anyone have any suggestions how to make a very simple and smart templatesystem? I don't have any problems coding. I just need ideas :-) <?php final class View implements IView { protected $filename, $variables = array(); public function __construct($filename = null, $variables = array()) { $this->filename = $filename; $this->variables = $variables; } public function __toString() { if(!file_exists($this->filename)) { throw new Exception("View does not exists."); } $content = file_get_contents($this->filename); foreach($this->variables as $variable => $value) { $content = str_replace("#{{$variable}}", $value, $content); } return str_replace("#{content}", $content, file_get_contents("design.html")); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/259320-template-system-with-layout-views/ Share on other sites More sharing options...
trq Posted March 20, 2012 Share Posted March 20, 2012 Your code makes little sense. $this->filename should be set to your template. Anyway, why re-invent the wheel? If you must use a template engine of some sort (which I would recommend against) there are plenty of solutions already out there. Quote Link to comment https://forums.phpfreaks.com/topic/259320-template-system-with-layout-views/#findComment-1329329 Share on other sites More sharing options...
Stevenn Posted March 20, 2012 Author Share Posted March 20, 2012 Your code makes little sense. $this->filename should be set to your template. Anyway, why re-invent the wheel? If you must use a template engine of some sort (which I would recommend against) there are plenty of solutions already out there. Hi thorpe, Is a template system a bad idea? I've looked at Smarty, but I don't like to use code I haven't coded myself. Smarty is too big. Quote Link to comment https://forums.phpfreaks.com/topic/259320-template-system-with-layout-views/#findComment-1329594 Share on other sites More sharing options...
trq Posted March 20, 2012 Share Posted March 20, 2012 Is a template system a bad idea? I personally don't like the overhead, or the idea of having to learn some other syntax. You can achieve the same goals using php alone. Quote Link to comment https://forums.phpfreaks.com/topic/259320-template-system-with-layout-views/#findComment-1329613 Share on other sites More sharing options...
scootstah Posted March 20, 2012 Share Posted March 20, 2012 A template parser (like Smarty, Twig) is generally frowned upon. They are usually very large libraries (Smarty), and cost a lot of overhead to use. Besides that, they are completely unnecessary. Probably one of the biggest arguments I see in favor of template engines is something along the lines of: "well I want my designers who don't know PHP to be able to make templates". This is a load of crap. Smarty's syntax is nearly identical to PHP, if they can do that they can definitely use PHP. You shouldn't have logic in templates anyway, just conditionals, loops, and variables. Another big reason I see for using stuff like Smarty is the separation of the presentation. This is a good argument, but has nothing to do with template parsers. The same thing can be achieved using pure PHP in the templates. but I don't like to use code I haven't coded myself. This is going to cripple you as a developer. Don't be so stubborn, libraries exist for a good reason. Quote Link to comment https://forums.phpfreaks.com/topic/259320-template-system-with-layout-views/#findComment-1329614 Share on other sites More sharing options...
cpd Posted March 20, 2012 Share Posted March 20, 2012 I personally have developed my own contemplating system that slots in perfectly for my page execution concept. I don't personally use procedural style programming and as such find it annoying writing code into the pages hence the want for a template system. If you do want to make a template system, keep it simple. There is no point creating a massive, elaborate, do it all template system as it'll consume unnecessary resources. Whilst the above posters wouldn't recommend a template system, I would argue it is required in certain programming concepts. And take scootstah's advice, don't be so stubborn. I was like that two years ago and learn the hard way. Quote Link to comment https://forums.phpfreaks.com/topic/259320-template-system-with-layout-views/#findComment-1329651 Share on other sites More sharing options...
scootstah Posted March 21, 2012 Share Posted March 21, 2012 Whilst the above posters wouldn't recommend a template system, I would argue it is required in certain programming concepts. I would like to know what "programming concepts" require a template parser. Quote Link to comment https://forums.phpfreaks.com/topic/259320-template-system-with-layout-views/#findComment-1329669 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.