{"id":80,"date":"2013-08-16T10:44:43","date_gmt":"2013-08-16T10:44:43","guid":{"rendered":"http:\/\/nikola-breznjak.com\/blog\/?p=80"},"modified":"2015-08-20T11:30:38","modified_gmt":"2015-08-20T11:30:38","slug":"building-a-really-simple-php-templating-engine","status":"publish","type":"post","link":"https:\/\/nikola-breznjak.com\/blog\/php\/building-a-really-simple-php-templating-engine\/","title":{"rendered":"Building a really simple PHP templating engine"},"content":{"rendered":"<p>Recently I had to make use of templates in PHP, and as a lot of people on StackOverflow (<a href=\"http:\/\/stackoverflow.com\/questions\/8318033\/outputting-html-with-echo-considered-bad-practice-in-php\">[1]<\/a>, <a href=\"http:\/\/stackoverflow.com\/questions\/62605\/php-as-a-template-language-or-some-other-php-templating-script\">[2]<\/a>) suggested &#8220;you don&#8217;t need another templating engine like <a href=\"http:\/\/www.smarty.net\/\">Smarty <\/a>on top of PHP, \u00a0as PHP itself is a templating engine&#8221;.<\/p>\n<p>So, I ended up making a simple function which looks like this:<\/p>\n<pre class=\"lang:php decode:true\">function getTemplate($file, $variables = NULL){\r\n    if (! is_null($variables))\r\n    extract($variables);\r\n\r\n    include(TEMPLATES_FOLDER . $file);\t\r\n}<\/pre>\n<p>TEMPLATES_FOLDER is a <a href=\"http:\/\/php.net\/manual\/en\/language.constants.php\">PHP constant<\/a>\u00a0which is defined in the config.php file like this:<\/p>\n<pre class=\"lang:php decode:true\">if ( !defined('ABSPATH') )\r\n\tdefine('ABSPATH', dirname(__FILE__) . '\/');\r\n\r\ndefine(\"TEMPLATES_FOLDER\", ABSPATH . \"templates\/\");<\/pre>\n<p>So, for example a template may look like this:<\/p>\n<pre class=\"lang:php decode:true\">&lt;!-- Navigation START --&gt;\r\n&lt;div class=\"navigation\"&gt;\r\n\t&lt;div class=\"welcome\"&gt;Hi, &lt;?=$user;?&gt;&lt;\/div&gt;\r\n\t&lt;div class=\"nav\"&gt;\r\n\t\t&lt;ul&gt;\r\n\t\t\t&lt;li&gt;&lt;a href=\"home.php\" class=\"&lt;? echo (isset($currentHome) ? 'current' : '') ?&gt;\"&gt;Home&lt;\/a&gt;&lt;\/li&gt;\r\n\t        &lt;li&gt;&lt;a href=\"members.php\" class=\"&lt;? echo (isset($currentMembers) ? 'current' : '')  ?&gt;\"&gt;Members&lt;\/a&gt;&lt;\/li&gt;\r\n\t\t&lt;\/ul&gt;\r\n\t&lt;\/div&gt;\r\n\r\n\t&lt;div class=\"clear\"&gt;&lt;\/div&gt;\r\n&lt;\/div&gt;\r\n&lt;!-- Navigation START --&gt;<\/pre>\n<p>Here you can see the usage of <a href=\"http:\/\/davidwalsh.name\/php-shorthand-if-else-ternary-operators\">ternary if operator<\/a>:<\/p>\n<pre>echo (isset($currentHome) ? 'current' : '')<\/pre>\n<p>And a way to call this template from a home.php file would be:<\/p>\n<pre class=\"lang:default decode:true\">&lt;? getTemplate(\"navigation.php\", array(\"user\" =&gt; getUser(), \"currentHome\" =&gt; true) ); ?&gt;<\/pre>\n<p>So the getTemplate() function loads the navigation.php file and passes it its variables &#8220;user&#8221; and &#8220;currentHome&#8221; which are then in the getTemplate() function extracted by using the <a href=\"http:\/\/php.net\/manual\/en\/function.extract.php\">extract()<\/a> function and echoed out. The currentHome variable is set \u00a0when called from the home.php file so that a template &#8220;knows&#8221; to set the &#8220;current&#8221; class to that element.<\/p>\n<p>Templating engines have their pros and cons, and since I didn&#8217;t need much leverage on the templating this proved to serve me just fine.<\/p>\n<p>What do you use for your template management in PHP?<br \/>\n<a href=\"http:\/\/www.codeproject.com\/script\/Articles\/BlogFeedList.aspx?amid=7823767\" rel=\"tag\">Link to this article on CodeProject<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently I had to make use of templates in PHP, and as a lot of people on StackOverflow ([1], [2]) suggested &#8220;you don&#8217;t need another templating engine like&hellip;<\/p>\n","protected":false},"author":1,"featured_media":88,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,3],"tags":[],"class_list":["post-80","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-codeproject","category-php"],"_links":{"self":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/80","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/comments?post=80"}],"version-history":[{"count":10,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/80\/revisions"}],"predecessor-version":[{"id":116,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/80\/revisions\/116"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media\/88"}],"wp:attachment":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media?parent=80"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/categories?post=80"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/tags?post=80"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}