409 posts
  • Has been part of the Envato Community for over 4 years
  • Sells items exclusively on Envato Market
  • Located in India

Building a PHP Templating system is lot harder than what i had taught, So this question is for all the PHP Gurus here, how can i implement a PHP Templating System for my application.

I am not asking for any tutorials, but what are the different ways to achieve this? I already tried (search and replace , set variables and include file) methods

Both worked fine, but i want to know is there any other way to to do this?

I don’t want to use Smarty or any other templating system.

I am looking for something similar to wordpress templating system.

499 posts
  • Has referred 1+ members
  • Has sold $1,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Located in Canada
+2 more

Just out of curiosity why do you not want to include one of the main template engines such as Smart, Flexy, Template IT?

These types of engines can give you so much power and you do not have to reinvent the wheel.

Templating systems have different ways of working, I have tried to come up with a small simplistic solution below.

Search and Replace

This is pretty much the easiest way of doing a template. It will search and replace any variables it finds. You could also code your template to perform function calls. If you built your function that searches and replaces the items, you could have your function do separate items if it came across different words… Such as

I have not tested this, just writing it on my laptop

function parser($template_text)
$template_words = explode($template_text, ' ');
foreach($template_words as $word)
  // run regex check to find all with {*}
    $function = substr($word, 1, stripos($word, '('));
      case 'showVar':
        // Grab parameters

The benefit of functions is that you could have you could call the same functions in multiple templates without having to worry about what is inside the PHP code for that file.

It may not be the best but allows for a lot of flexibility.

5165 posts
  • Provided great contribution to our forums
  • Achieved the monthly Community Superstar Award
  • Helps us moderate the forums
  • Has been part of the Envato Community for over 6 years
+17 more

avoid replacing variables or inventing your own (smarty’ish) looking language

if you’re trying to separate code from layout, try something basic like this:

  • /index.php
  • /header.php
  • /footer.php
  • /code/home.php
  • /layout/home.php

inside index.php put this:

$page = isset($_REQUEST['p']) ? basename($_REQUEST['p']) : 'home';
$code_file = 'code/'.$page.'.php';
$layout_file = 'layout/'.$page.'.php';
if(is_file($code_file)) include($code_file);
if(is_file($layout_file)) include($layout_file);

inside header.php put your header html code (like wordpress)

inside footer.php put your footer html code (like wordpress)

inside /code/home.php put all the code logic

stuff like mysql_query’s, handling $_POST data, etc…
anything that doesn’t deal with html layout, just raw data processing and preparation

$my_data = array(
'test', 'foo', 'moo',

inside /layout/home.php put all your layout code

this is everything that generates html

<?php foreach($my_data as $data){ ?> 
<li> <a href="?p=home"> <?php echo $data;?> </a> </li>
<?php } ?> 

that should get u started on a basic php templating system, with a similar design to wordpress (index.php page forking off to various code / layout files)

ps: put php tags in the code above where needed, the forum doesn’t support them so i removed them.

5165 posts
  • Provided great contribution to our forums
  • Achieved the monthly Community Superstar Award
  • Helps us moderate the forums
  • Has been part of the Envato Community for over 6 years
+17 more

then linking to /index.php?p=home will load the home.php files wrapped with header/footer

create extra pages in the home and layout directory, and you can then link to them like: index.php?p=other_page

Helpful Information

  • Please read our community guidelines. Self promotion and discussion of piracy is not allowed.
  • Open a support ticket if you would like specific help with your account, deposits or purchases.
  • Item Support by authors is optional and may vary. Please see the Support tab on each item page.

Most of all, enjoy your time here. Thank you for being a valued Envato community member.

Post Reply

Format your entry with some basic HTML. Read the Full Details, or here is a refresher:

<strong></strong> to make things bold
<em></em> to emphasize
<ul><li> or <ol><li> to make lists
<h3> or <h4> to make headings
<pre></pre> for code blocks
<code></code> for a few words of code
<a></a> for links
<img> to paste in an image (it'll need to be hosted somewhere else though)
<blockquote></blockquote> to quote somebody

:grin: :shocked: :cry: Complete List of Smiley Codes