Zurev Posted January 8, 2011 Share Posted January 8, 2011 So I made a template class, it's very simple, pretty much allows the usage like so: $template = new template("blogpost"); $template->fillBraces(array( "TITLE" => "How to bathe your chimpanzee", "AUTHOR" => "Jim Bo James" )); $template->render(); Simple, uses tpl files, similar to what phpbb used a long time back, not sure if they still do. My main goal for this is to have a file that has something like this: {HEADER} {MAIN_CONTENT} {FOOTER} and the above usage to stay the same, however that would fill the main_content part, and the header and footer would be grabbed from header/footer tpl files. Any idea on suggestions? The full class is posted below. class template { protected $_viewContents; function __construct($viewName) { $this->_viewContents = file_get_contents(TEMPLATE.$viewName.TEMPLATE_EXT); return; } function sourceOf($fileName) { return @file_get_contents(TEMPLATE.$fileName.TEMPLATE_EXT); } function fillLayout() { } function fillBraces($information, $replacement=NULL) { if (is_array($information)) { foreach ($information as $key => $val) { $this->_viewContents = str_ireplace("{".$key."}", $val, $this->_viewContents); } return; } elseif ($replacement!=NULL) { $this->_viewContents = str_ireplace("{".$information."}", $replacement, $this->_viewContents); return; } } function render() { echo $this->_viewContents; } } Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/223734-made-template-class-how-to-incorporate-into-overall-layout/ Share on other sites More sharing options...
trq Posted January 8, 2011 Share Posted January 8, 2011 Firstly, I'll just let you know that I think template classes like this (and any others really) are a complete waste of resources (IMO). There are much more efficient methods of creating templates using pure php. Anyway, to do what your looking for you would need another method to replace render() that simply returns the parsed template. You would then parse the main_content.tpl template and store it within a variable ready to use in your layout.tpl. Quote Link to comment https://forums.phpfreaks.com/topic/223734-made-template-class-how-to-incorporate-into-overall-layout/#findComment-1156560 Share on other sites More sharing options...
Zurev Posted January 8, 2011 Author Share Posted January 8, 2011 Firstly, I'll just let you know that I think template classes like this (and any others really) are a complete waste of resources (IMO) I felt it might be seeing as how it's taking a partially filled file's contents, editting them, and then re-rendering it. Seems like an awful lot of running around to do, however I'm unsure of another way to do it. There are much more efficient methods of creating templates using pure php. Care to elaborate on that bit? Quote Link to comment https://forums.phpfreaks.com/topic/223734-made-template-class-how-to-incorporate-into-overall-layout/#findComment-1156576 Share on other sites More sharing options...
gizmola Posted January 8, 2011 Share Posted January 8, 2011 include() does a pretty good job, and you can simply use a naming convention and some simple functions that support finding the right files by naming convention, and supporting the idea of loading of partials inside a template. A couple of simple wrappers around include, that use PATH variables to make sure that the inclusions are safe, and you'll have a lot of what you need. There's also tried and true template libraries like those available in zf (Zend_view), in PEAR and that oldie but goodie smarty. They each have a point of view and a reason for being, so it very much depends on what your goals are. Quote Link to comment https://forums.phpfreaks.com/topic/223734-made-template-class-how-to-incorporate-into-overall-layout/#findComment-1156580 Share on other sites More sharing options...
trq Posted January 9, 2011 Share Posted January 9, 2011 Care to elaborate on that bit? I'm more inclined to stick with a little bit of php within my templates. Your template class means that a designer would still need to learn a new syntax anyway, so why not have them learn a little bit of php, it's allot more efficient. Especially considering you haven't even implemented loops or anything yet. Let's create a simple 'blog' example. header.php <html> <head> <title><?php echo $this->title; ?></title> </head> <body> footer.php </body> </html> blog.php <?php include 'header.php'; ?> <?php foreach ($this->blogs as $blog): ?> <div> <span class="title"><?php echo $blog->title; ?></span> <span class="content"><?php echo $blog->content; ?></span> <span class="footer"><a href="/blog/comment/add/id/<?php echo $blog->id; ?>">Leave a comment</a></span> </div> <?php endforeach; ?> <?php include 'footer.php'; ?> Now, the View class. <?php class View { public function render($view) { ob_start(); include $view; echo ob_get_clean(); } } Now, your client code. <?php if ($result = mysql_query("SELECT title, content, id FROM blogs")) { if (mysql_num_rows($result)) { while ($row = mysql_fetch_object($result)) { $blogs[] = $row; } $view = new View; $view->title = 'blogs'; // page title $view->blogs = $blogs; $view->render('blogs.php'); } } Of course this is a VERY simplified version, but hopefully you get the idea. This is roughly how view work in allot of the frameworks around. Quote Link to comment https://forums.phpfreaks.com/topic/223734-made-template-class-how-to-incorporate-into-overall-layout/#findComment-1156876 Share on other sites More sharing options...
Anti-Moronic Posted January 9, 2011 Share Posted January 9, 2011 Agree fully with Pikachu..sorry, thorpe. PHP *is* a templating language, it is meant for stuff like this. Originally, this was the languages primary purpose. The above example is very well thought out explanation and example. Another idea might be to actually review some of the frameworks (like Zend) and apps (like Wordpress) and see how their MVC layer works. This will give you a more in depth look. Quote Link to comment https://forums.phpfreaks.com/topic/223734-made-template-class-how-to-incorporate-into-overall-layout/#findComment-1156880 Share on other sites More sharing options...
Zurev Posted January 9, 2011 Author Share Posted January 9, 2011 Agree fully with Pikachu..sorry, thorpe. PHP *is* a templating language, it is meant for stuff like this. Originally, this was the languages primary purpose. The above example is very well thought out explanation and example. Another idea might be to actually review some of the frameworks (like Zend) and apps (like Wordpress) and see how their MVC layer works. This will give you a more in depth look. Yeah the view thorpe gave me insight to is obviously very similar to the usage of codeigniter, so I may just have to take a look at how it works! Quote Link to comment https://forums.phpfreaks.com/topic/223734-made-template-class-how-to-incorporate-into-overall-layout/#findComment-1156885 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.