I am trying to make a simple site template. Something which will be easy to update. I do not want to use template engines like smarty.. Has anybody already got something like this? This is just an example <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <!--start head.php--> <?php include("head.php"); ?> <!--end head.php--> </head> <body> <!--start top.php--> <div class="top"> <?php include("top.php"); ?> </div> <!--end top.php--> <!--start left.php--> <div class="left"> <?php include("left.php"); ?> </div> <!--end left.php--> <!--start page.php--> <div class="page"> <?php include("page.php"); ?> </div> <!--end page.php--> <!--start footer.php--> <div class="footer"> <?php include("footer.php"); ?> </div> <!--end footer.php--> </body> </html> PHP:
I simply do something like this: $header_title = 'Bla'; $footer_text = 'Bla'; //any other variables needed by header & footer.. require 'header.php'; echo 'Content goes here'; require 'footer.php'; ?> PHP:
everything you can do with HTML do it. its faster and better. making a header.php just for the img is not a good idea.
I made my own custom template class. I wanted to make it easy to use for designers that are not familiar with PHP, so it's pure HTML. It's not the best solution and there's probably similar solution somewhere, but it works for me. It's simplified usage goes something like this: $tpl->setTemplateDir(); $tpl->loadTemplateContent(); // Do $tpl->set("{placeholder}", $templateElement); here $tpl->compileTemplateContent(); $tpl->clearTemplateData(); $tpl->loadTemplateMain(); $tpl->set("{content-goes-here}", $tpl->template("content")); $tpl->compileTemplateMain(); $tpl->printTemplate(); $tpl->clearGlobal() PHP:
Might as well use Smarty when you get to that point. As more designers and developers are familiar with the smarty template engine.
Can anyone tell me why exactly you use smarty? I've always wondered. It adds an extra layer of code to your site which isn't really necessary. You may claim that it's a little more readable but I don't see much difference.. {variable} =Â <?php echo $variable; ?> etc...
I can tell you why I don't use it. Same reason you don't. Only rational reasons I can think of would be for standardizing, useful if lots of different people work on the project etc. and caching is built-in. And to the OP's code, I would question why you are putting the doctype into the main file? Why not have all of that in the header file? If you wanted/needed to change doctypes, it's in one file. <?php $page_title = "Demo"; $page_meta ="Keywords List"; @include "includes/site.php"; @include "includes/header.php"; ?> <div id="content"> <!-- content starts here --> <h2 class="article_title">Article Title</h2> <!-- content ends here --> </div> <?php @include "includes/sidebar.php"; @include "includes/footer.php"; ?> Code (markup): site.php checks if $page_title is set, if not it puts a default in place. Also sets the stylesheet to use and the other META tags. The DIVs for page layout are defined in the included files, could wrap those like the OP did in the main file but I find it easier to work with them in the include files. If you formalize the structure of the HTML you can have almost any layout, check layout gala http://blog.html.it/layoutgala/ for how to do this.
http://smarty.net/manual/en/language.modifiers.php http://smarty.net/manual/en/language.builtin.functions.php http://smarty.net/manual/en/language.custom.functions.php Also some major projects use it such as phpBB, SMForum, x-cart, etc. I mean if you're coding strictly for yourself, sure use a dumbed down token method, otherwise standardizing and separating the coding layer from the design layer can help with people who may not be entirely familiar with your coding style. Not to mention it can also allow you to encode your logic while still allowing the design to be adjusted. To each their own of course.
You're exchanging one coding style for another, smarty still is a coding style. Someone can't just come in and make changes to the foreach loop, etc.. This can be done just the same with normal PHP. Do your preprocessing (db calls, etc) in a separate file, then assign template variables and require the template file. Anyway, I guess this is just personal preference again, can't really debate on such things
More people are likely to understand smarty's rules, modifiers, and other functions that they can use to present the same data. Plus has it's own caching system if desired.
Ok this is what i have got so far. My root folder: config #config.php #functions.php files #footer.php #header.php #left.php #right.php #topmenu.php images #logo.gif pages #about.php #contact.php #index.php index.php about.php contact.php On index.php I have got <?php $page_title = "title of site"; $page_keyword = "keywords"; define('_This_IS_not-FOR_YOU', TRUE); // start config include "config/config.php"; // end config // start header include "files/header.php"; // end header // start topmenu include "files/topmenu.php"; // end topmenu // start content include "pages/index.php"; // end content // start footer include "files/footer.php"; // end footer ?> PHP: On pages/index.php I have got <?php defined('_This_IS_not-FOR_YOU') or die('Direct access not allowed.'); ?> <table width="800" border="1" bgcolor="#FFFF00"> <tr> <td> </td> <td>Index</td> <td> </td> </tr> </table> PHP: On files/header.php I have got this <?php defined('_This_IS_not-FOR_YOU') or die('Direct access not allowed.'); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title><?php echo $page_title; ?></title> </head> <body> PHP: How does this system look? Any feedback