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

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
TutelageSystems says

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 {*}
  if(regex())
  {
    $function = substr($word, 1, stripos($word, '('));
    switch($function)
    {
      case 'showVar':
        // Grab parameters
        showVar($yourParameters);
      break
    }
  }
}

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.

3100 posts
  • Located in Australia
  • Helps us moderate the forums
  • Has been part of the Envato Community for over 5 years
  • Helped several times protecting Envato Market against copyright violations
+16 more
dtbaker Moderator says

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);
include('header.php');
if(is_file($layout_file)) include($layout_file);
include('footer.php');

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

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

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.

3100 posts
  • Located in Australia
  • Helps us moderate the forums
  • Has been part of the Envato Community for over 5 years
  • Helped several times protecting Envato Market against copyright violations
+16 more
dtbaker Moderator says

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

by
by
by
by
by
by