{"id":59,"date":"2009-05-12T06:16:02","date_gmt":"2009-05-12T04:16:02","guid":{"rendered":"https:\/\/paul-anton.vanhandel.at\/?p=59"},"modified":"2009-05-12T06:16:02","modified_gmt":"2009-05-12T04:16:02","slug":"php-templating-systems-what-you-really-need","status":"publish","type":"post","link":"https:\/\/paul-anton.vanhandel.at\/?p=59","title":{"rendered":"PHP Templating Systems &#8211; What You Really Need!"},"content":{"rendered":"<p>When it comes to larger PHP sites you want (or should seriously think about wanting) to utilize a templating system of some sort, to strictly separate design from logic.<\/p>\n<p>There are numerous templating systems out there.<\/p>\n<p>All I know and worked with have at least two major disadvantages &#8211; in my opinion &#8211; the view of a backend PHP programmer:<br \/>\nFor one they are expensive &#8211; they eat up a lot of server resources.<br \/>\nAnd then they come with this or another type of own language that is needed to get the content out.<\/p>\n<p>To avoid server loads most of them introduce caching systems. Which is good and necessary and I could live with that. Since caching is (or should be) everywhere throughout a potentially high scaling and busy web application.<\/p>\n<p>However, the second downside is harder to overcome.<br \/>\nOr isn&#8217;t it?<\/p>\n<p>So, you&#8217;ve got your project lined out, have the frontend guys ready to start working (bear in mind, I&#8217;m talking as a backend programmer as well as a project lead) and the first thing to do &#8211; before you even get started, is to have these guys start learning the templating system you decided to use.<\/p>\n<p><strong>HALT.<\/strong><\/p>\n<p><!--more--><\/p>\n<p>Granted, one or the other might just have quite some experience in the system he needs to work with for your project, some others &#8211; on the other hand &#8211; might have used a different one, or never have used one at all.<br \/>\nFirst thing you need to do is to make sure, all the frontend guys are on the same page and level.<\/p>\n<p>Now is the time to remember, that PHP can live embedded in HTML code.<br \/>\nIn fact, even today, many rookies build their apps that way &#8211; mix HTML and PHP no matter what. However, that is, what I didn&#8217;t want to talk about (MVC etc.) so let&#8217;s move on.<\/p>\n<p>Why I&#8217;ve been saying the above though is to remind you, that PHP is the ultimate template language by itself.<br \/>\nWhy bother your people with learning another scripting language that will only have to be parsed, interpreted, whatever when there is PHP.<br \/>\nChances are, that all of them at least have some knowledge in PHP.<\/p>\n<p>Why would anyone use a construct like<\/p>\n<pre escaped=\"true\" lang=\"php\">&lt;?php\n    $arr = array( 1001,1002,1003);\n    $smarty-&gt;assign('custId', $arr);\n?&gt;<\/pre>\n<pre escaped=\"true\" lang=\"smarty\">{foreach from=$custId item=customer_id} id: {$customer_id}&lt;br&gt; {\/foreach}<\/pre>\n<p>if you simply could do it the php way:<\/p>\n<pre escaped=\"true\" lang=\"php\">&lt;?php\n    $arr = array( 1001,1002,1003);\n    foreach($arr as $customer_id) :\n?&gt;\nid : &lt;?=$customer_id;?&gt;&lt;br \/&gt;\n&lt;?php endforeach; ?&gt;<\/pre>\n<p>This is why &#8211; many years ago &#8211; I came up with that super simple yet extremly powerfull (I think) PHP-templating system.<br \/>\nThe cool thing about it is, that it only needs a very few lines to implement. It is plain PHP, slim, fast, easy to debug, all you&#8217;ll ever need.<\/p>\n<p>So, how does it work?<\/p>\n<p>Hell, it&#8217;s so simple I wouldn&#8217;t even dare thinking to copyright that!<\/p>\n<p>All you need is a Page class (call it all you want) and a Template class. The Page class is what you will instatiate in your controller &#8211; be it a php file or a method in a ZF controller class or however you have your controllers designed.<\/p>\n<p>Now all you need is to register your templates &#8211; that may be one or many for your HTML code to be served &#8211; and assign PHP variables that are being used in the template.<\/p>\n<p>Usage:<br \/>\n<strong>Template<\/strong><\/p>\n<pre lang=\"php\">\n<h1>This is a Template file.<\/h1>\nHTML with<\/pre>\n<p><strong>Controller file<\/strong><\/p>\n<pre lang=\"php\">registerTemplate('tplName', PATHTOTEMPLATES . 'template.tpl');\n\n\/\/assign a value to the template\n$what = 'PHP';\n$tpl-&gt;set('what', $what);\n\n\/\/render the page\n$html = $page-&gt;renderHtml();\n\n\/\/output HTML\necho $html;\nexit();\n?&gt;<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>When it comes to larger PHP sites you want (or should seriously think about wanting) to utilize a templating system of some sort, to strictly separate design from logic. There are numerous templating systems out there. All I know and worked with have at least two major disadvantages &#8211; in my opinion &#8211; the view <a href=\"https:\/\/paul-anton.vanhandel.at\/?p=59\"> [&#8230;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16,1,3,11],"tags":[],"class_list":["post-59","post","type-post","status-publish","format-standard","hentry","category-devel","category-allgemein","category-english","category-php"],"_links":{"self":[{"href":"https:\/\/paul-anton.vanhandel.at\/index.php?rest_route=\/wp\/v2\/posts\/59","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/paul-anton.vanhandel.at\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/paul-anton.vanhandel.at\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/paul-anton.vanhandel.at\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/paul-anton.vanhandel.at\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=59"}],"version-history":[{"count":0,"href":"https:\/\/paul-anton.vanhandel.at\/index.php?rest_route=\/wp\/v2\/posts\/59\/revisions"}],"wp:attachment":[{"href":"https:\/\/paul-anton.vanhandel.at\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=59"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/paul-anton.vanhandel.at\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=59"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/paul-anton.vanhandel.at\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=59"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}