dbo Posted March 11, 2008 Share Posted March 11, 2008 I'm just full (or fool?) of thoughts today. I'm wondering what approach you guys use for generic theming needs. I inherit a lot of sites that have something like this: getHeader("About Us"); //page specific content getFooter(); The problem with this is that I've usually got a custom index page and then a slightly different design for second level pages etc. Something like Smarty is really overkill for me. I like CSS but am not sold on it completely, so I'd like to be able to swap out designs fairly easily. Dunno what I'm asking exactly, just some general ideas. I'm all about ideas Quote Link to comment https://forums.phpfreaks.com/topic/95629-generic-theming/ Share on other sites More sharing options...
Liquid Fire Posted March 11, 2008 Share Posted March 11, 2008 Xtemplate might not be bad. I only thing i don't really like about smarty(from my limited use of it) is that the template file are compiled. Xtemplate is similar but the template files are not compiled. Quote Link to comment https://forums.phpfreaks.com/topic/95629-generic-theming/#findComment-489811 Share on other sites More sharing options...
laffin Posted March 11, 2008 Share Posted March 11, 2008 I prefer building my own. I read a good article on building yer own Here Which is very basic, and since you can continue to build upon it, ya will get a better understanding of the engine itself Quote Link to comment https://forums.phpfreaks.com/topic/95629-generic-theming/#findComment-489825 Share on other sites More sharing options...
dbo Posted March 11, 2008 Author Share Posted March 11, 2008 Smarty is just a big beast with it's own language. I think I want something generic that uses PHP as it's scripting language. The next question is what makes the most sense for populating it. Do I use views to populate the template? Does the template system itself replace the view? I dunno yet. Trying to think through this carefully before I start writing a lot of code. The way I look at it, I've got a solid datamodel that provides me a shitton of flexibility... so the hard part is done. Now I just need to think through the best way of bringing that to life. Quote Link to comment https://forums.phpfreaks.com/topic/95629-generic-theming/#findComment-489946 Share on other sites More sharing options...
trq Posted March 11, 2008 Share Posted March 11, 2008 I don't believe template systems are required at all. They add extra overhead for very little gain IMO. As you stated yourself, you pretty much need to learn another language to learn the template engine, why not simply use minimal amount of php within your views instead. Back to your question though (I think), there is no harm including views within views. Hell, you could even include another controller (generating more views) within a view if your framework is setup for it. Quote Link to comment https://forums.phpfreaks.com/topic/95629-generic-theming/#findComment-489966 Share on other sites More sharing options...
dbo Posted March 11, 2008 Author Share Posted March 11, 2008 Yeah, maybe I just need to think through a better way to construct the views. I just like the idea of being able to drop in a new directory (theme) to change the entire look of a site. Though I guess, I could technically do the same thing with different views, it's just a matter of how the views get called. Quote Link to comment https://forums.phpfreaks.com/topic/95629-generic-theming/#findComment-489975 Share on other sites More sharing options...
trq Posted March 12, 2008 Share Posted March 12, 2008 I just like the idea of being able to drop in a new directory (theme) to change the entire look of a site. That should be what css is for, your view should contain markup, not presentation. Quote Link to comment https://forums.phpfreaks.com/topic/95629-generic-theming/#findComment-490379 Share on other sites More sharing options...
dbo Posted March 12, 2008 Author Share Posted March 12, 2008 Yeah, I'm not on board with CSS. Maybe when we get standards compliant browsers that all do what they're told I will be It doesn't seem practical to me to spend a bunch of time tricking a particular browser to look correctly (*cough* ie *cough*) when I can drop stuff in a table and it looks right across the board. That being said, I do think CSS has a place and I do use it. I'm just not committed to doing pure CSS layouts yet. There's just too much ambiguity. Quote Link to comment https://forums.phpfreaks.com/topic/95629-generic-theming/#findComment-490460 Share on other sites More sharing options...
dbo Posted March 12, 2008 Author Share Posted March 12, 2008 For anyone who is interested here's the very basic template class that I wrote up last night. It's based sort of off Smarty, Drupal, and an article found here: http://www.sitepoint.com/article/beyond-template-engine class DTemplate { private $path; private $vars; function DTemplate($path) { $this->path = $path; $vars = Array(); } public function set($key, $value) { $this->vars[$key] = $value; } public function load() { extract($this->vars); ob_start(); include($this->path); $contents = ob_get_contents(); ob_end_clean(); return $contents; } } Example template <html> <head> <title><?php echo $page_title; ?></title> </head> <body> <h1><?php echo $title; ?></h1> <?php echo $content; ?> </body> </html> And example usage: $master = new DTemplate("masterpage.tpl.php"); $master->set("page_title", "Template Test"); $master->set("title", "My Title"); $master->set("content", "Content"); echo $master->load(); Quote Link to comment https://forums.phpfreaks.com/topic/95629-generic-theming/#findComment-490474 Share on other sites More sharing options...
redbullmarky Posted March 12, 2008 Share Posted March 12, 2008 i like the way CakePHP do it (sure, it's not original, but Cake first introduced me to the concept) - they have a master "layout" as well as individual views. The master layout contains the main structure of the page, ie what's consistent through the site. in the final 'render' method, the template requested is merged in with the layout and the final result returned. i have to admit tho (and thorpe (i think) might be the one to agree) I love the way Django handle views and templates. Whilst some of the code may appear alien at first, the manual should give you another approach that is pretty good as far as building output goes - especially in terms of template inheritance. http://www.djangoproject.com/documentation/templates/ Quote Link to comment https://forums.phpfreaks.com/topic/95629-generic-theming/#findComment-490479 Share on other sites More sharing options...
dbo Posted March 12, 2008 Author Share Posted March 12, 2008 Great stuff. Thanks for the reply. I'm looking into the article you posted right now... and I'm very much considering the approach, that you mentioned, Cake uses. It's actually something I've been pondering the last couple of days before I even started the endeavor. getHeader(); getFooter(); This is clunky and you have like an opening tag in getHeader that doesn't get closed up until get footer. I think what I really need is a solid approach for my views. Having a master view that then calls additional views would probably work just fine. I think I just need to see a clean working example. Quote Link to comment https://forums.phpfreaks.com/topic/95629-generic-theming/#findComment-490486 Share on other sites More sharing options...
dbo Posted March 12, 2008 Author Share Posted March 12, 2008 The more I think of it, really all I need is a good method for loading additional views from a "master" view. Any suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/95629-generic-theming/#findComment-490498 Share on other sites More sharing options...
redbullmarky Posted March 12, 2008 Share Posted March 12, 2008 i dont use this one for everything, and this may be an old one, but it has done me good for many things. here's my full class, based on the "Beyond Template Engines" article mentioned above: <?php class View { public $vars = array(); public $html; public $layout = null; private $config = array(); function __construct() { // config settings - eg, gzip, etc $this->config = Config::load('view'); if (!isset($this->config['layout'])) { $this->config['layout'] = 'master.tpl'; } $this->layout = $this->config['layout']; // i have some vars that can be set as default - eg, copyright messages, etc $pagevars = Config::load('page'); if (sizeof($pagevars)) { $this->vars = array_merge($this->vars, $pagevars); } } /* * loads a template file and returns the output * */ function load($filename) { extract($this->vars); ob_start(); include(TEMPLATE_PATH . '/' . $filename); $this->html = ob_get_clean(); return $this->html; } /** * loads/parses a template but puts the output into a template var * */ function loadtovar($varname, $filename) { if ($html = $this->load($filename)) { $this->set($varname, $html); } else { return false; } } /* * sets a view var * */ function set($varname, $value = '') { if (!is_array($varname)) { $this->vars[$varname] = $value; } else { foreach($varname as $key=>$var) { $this->vars[$key] = $var; } } } /* * displays the final output * */ function render($return = false) { $this->vars['pagebody'] = $this->html; if ($this->layout) { ob_start(); echo $this->load($this->layout); $html = ob_get_clean(); } else { $html = $this->html; } if (!$return) { // gzip enabled by config? if (isset($this->config['gzip']) && $this->config['gzip'] == true) { ob_start(); ob_start('ob_gzhandler'); echo $html; ob_end_flush(); header('Content-Length: '.ob_get_length()); ob_end_flush(); } else { echo $html; } } else { return $html; } } } ?> i stripped out some code for sake of easier grasp, and it depends on a few things, so wont work "as is", but i'm sure you could work it out. TEMPLATE_PATH and the Config settings being the two main dependencies that shouldnt take much work do figure out how to remove. usage: $tpl = new View(); $tpl->set('title', 'Hello World!'); $tpl->loadtovar('body', 'mybody.tpl'); $tpl->load('mytemplate.tpl'); $out = $tpl->render(); the following is a layout example. master.tpl: <html> <head> </head> <body> <?=$pagebody ?> </body> </html> and the following is an individual "view" template. mytemplate.tpl: <h1><?=$title ?></h1> <p><?=nl2br($body) ?></p> and the following is an individual snippet, just used to show how 'loadtovar' would be used. mybody.tpl: I am a page body. Quote Link to comment https://forums.phpfreaks.com/topic/95629-generic-theming/#findComment-490555 Share on other sites More sharing options...
dbo Posted March 12, 2008 Author Share Posted March 12, 2008 Seems pretty similar to the method I was using. Hrmmm, thanks for the example. I think I'm ready to start tinkering. Quote Link to comment https://forums.phpfreaks.com/topic/95629-generic-theming/#findComment-490599 Share on other sites More sharing options...
Naez Posted March 17, 2008 Share Posted March 17, 2008 I *highly* reccomend smarty. However, at times (on smaller projects) I tend to use a simple templating system so my customers can arrange their layouts however they wish. Here is a very very simple template system that I wrote, and use ALL the time. test.php <?php function gettemplate($template,$ending="html") { return str_replace("\"","\\\"",implode("",file($template.".".$ending))); } for($i = 0; $i < 20; $i++) { $title = "TITLE RETRIEVED THROUGH SOME QUERY".$i; $text = "I CAN CREATE NICE LAYOUTS AND STYLES AND DON'T HAVE TO KNOW PHP OR MESS WITH CODE"; eval("\$entries .= \"".gettemplate("someentry")."\";"); } eval("echo \"".gettemplate("entries")."\";"); ?> someentry.html <br> <table align=center width=200> <tr> <td><b>$title</b></td> </tr> <tr> <td>$text</td> </tr> </table> <br> entries.html <html> <head> </head> <body align=center bgcolor=green> $entries </body> </html> Output: <html> <head> </head> <body align=center bgcolor=green> <br> <table align=center width=200> <tr> <td><b>TITLE RETRIEVED THROUGH SOME QUERY0</b></td> </tr> <tr> <td>I CAN CREATE NICE LAYOUTS AND STYLES AND DON'T HAVE TO KNOW PHP OR MESS WITH CODE</td> </tr> </table> <br> <br> <table align=center width=200> <tr> <td><b>TITLE RETRIEVED THROUGH SOME QUERY1</b></td> </tr> <tr> <td>I CAN CREATE NICE LAYOUTS AND STYLES AND DON'T HAVE TO KNOW PHP OR MESS WITH CODE</td> </tr> </table> <br> <br> <table align=center width=200> <tr> <td><b>TITLE RETRIEVED THROUGH SOME QUERY2</b></td> </tr> <tr> <td>I CAN CREATE NICE LAYOUTS AND STYLES AND DON'T HAVE TO KNOW PHP OR MESS WITH CODE</td> </tr> </table> <br> *** SNIP *** <br> <table align=center width=200> <tr> <td><b>TITLE RETRIEVED THROUGH SOME QUERY19</b></td> </tr> <tr> <td>I CAN CREATE NICE LAYOUTS AND STYLES AND DON'T HAVE TO KNOW PHP OR MESS WITH CODE</td> </tr> </table> <br> </body> </html> This may look as though it has security flaws, but a recent blog program I wrote for somebody, if someone enters in $dbname for intance, it just displays $dbname. Whoever suggested not seperating design and code logic is completely wrong. Every thing you do should be about seperating logic so that it's easier for designers and maintainers. Also, yea Smarty, I've just recently started playing with it and it's really nice. Quote Link to comment https://forums.phpfreaks.com/topic/95629-generic-theming/#findComment-493872 Share on other sites More sharing options...
dbo Posted March 17, 2008 Author Share Posted March 17, 2008 Thanks for the reply. I've actually been using the little templating system that I wrote on the last few things I've been working on. So far it's working out very well, but I need to play with it more to be sure. It's basically doing the same thing your system does, but uses output buffering and includes rather than eval. Quote Link to comment https://forums.phpfreaks.com/topic/95629-generic-theming/#findComment-493979 Share on other sites More sharing options...
Jenk Posted March 19, 2008 Share Posted March 19, 2008 Why not use DOM? php.net/dom Quote Link to comment https://forums.phpfreaks.com/topic/95629-generic-theming/#findComment-495553 Share on other sites More sharing options...
dbo Posted March 19, 2008 Author Share Posted March 19, 2008 What good would that do me? Quote Link to comment https://forums.phpfreaks.com/topic/95629-generic-theming/#findComment-495902 Share on other sites More sharing options...
Jenk Posted March 24, 2008 Share Posted March 24, 2008 Try reading it first. Quote Link to comment https://forums.phpfreaks.com/topic/95629-generic-theming/#findComment-499168 Share on other sites More sharing options...
dbo Posted March 24, 2008 Author Share Posted March 24, 2008 Thanks for the sarcastic response. I'm aware of what DOM is and I did read it. Yes it could be used, but I didn't see what value it would add, thus I asked the question. Quote Link to comment https://forums.phpfreaks.com/topic/95629-generic-theming/#findComment-499186 Share on other sites More sharing options...
keeB Posted March 29, 2008 Share Posted March 29, 2008 CSSZenGarden should make you sold on CSS. If it doesn't, there's no convincing you. Swapping the presentation of the site is what CSS was designed to do! Quote Link to comment https://forums.phpfreaks.com/topic/95629-generic-theming/#findComment-503840 Share on other sites More sharing options...
dbo Posted March 29, 2008 Author Share Posted March 29, 2008 I'm familiar with CSS Zen garden, it's pretty cool. My thing is just that there's not enough cross browser support to get things to work correctly without doing hacks to get it to look right. It seems silly to me to spend endless hours trying to use CSS for a certain display, if I can do it quickly with tables and know that it's going to look right w/o any hacks... especially if I have a good templating system that allows me to change things from a single location to quickly change designs. Quote Link to comment https://forums.phpfreaks.com/topic/95629-generic-theming/#findComment-503881 Share on other sites More sharing options...
keeB Posted March 29, 2008 Share Posted March 29, 2008 Actually you just shift the burden of where the countless hours are spent ). Maintaining a template engine for basic markup will be much more tedious than maintaining a CSS. But, hey, I get it.. I am no CSS fan either. Quote Link to comment https://forums.phpfreaks.com/topic/95629-generic-theming/#findComment-504010 Share on other sites More sharing options...
dbo Posted March 29, 2008 Author Share Posted March 29, 2008 Well not really. My templating system is very generic and also handles my server side caching. Basically all you are doing is creating an HTML document and defining content areas. Quote Link to comment https://forums.phpfreaks.com/topic/95629-generic-theming/#findComment-504037 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.