Keshav Nair
Keshav Nair

Reputation: 423

How to make a php template engine?

I need to make a small and simple php template engine I searched a lot and many of them were too complex to understand and I don't want to use smarty and other similar engines, I have got some idea from Stack Overflow like this:

$template = file_get_contents('file.html');
$array = array('var1' => 'value',
                'txt' => 'text');

foreach($array as $key => $value)
{
  $template = str_replace('{'.$key.'}', $value, $template);
}

echo $template;

Now instead of echo the template I just want to add include "file.html" and it will display the file with correct variable values and I want to put the engine in a separate place and just include it in the template what I want to use it declare the array and at the end include the html file like phpbb. Sorry I am asking to much but can anyone just explain the basic concept behind this?

EDIT: Well let me be frank i am making a forum script and i have got tons of ideas for it but i want make its template system like phpbb so i need a separate template engine custom one if you can help then please you are invited to work with me. sorry for the ad.. :p

Upvotes: 8

Views: 18035

Answers (4)

Petter Thowsen
Petter Thowsen

Reputation: 1727

    <?php
    class view {
        private $file;
        private $vars = array();

        public function __construct($file) {
            $this->file = $file;
        }

        public function __set($key, $val) {
            $this->vars[$key] = $val;
        }

        public function __get($key, $val) {
            return (isset($this->vars[$key])) ? $this->vars[$key] : null;
        }

        public function render() {
            //start output buffering (so we can return the content)
            ob_start();
            //bring all variables into "local" variables using "variable variable names"
            foreach($this->vars as $k => $v) {
                $$k = $v;
            }

            //include view
            include($this->file);

            $str = ob_get_contents();//get teh entire view.
            ob_end_clean();//stop output buffering
            return $str;
        }
    }

Here's how to use it:

    <?php
    $view = new view('userprofile.php');
    $view->name = 'Afflicto';
    $view->bio = "I'm a geek.";
    echo $view->render();

Upvotes: 2

ariefbayu
ariefbayu

Reputation: 21979

What if, for a script easier to maintain, you move those to functions?

something like this:

<?php

function get_content($file, $data)
{
   $template = file_get_contents($file);

   foreach($data as $key => $value)
   {
     $template = str_replace('{'.$key.'}', $value, $template);
   }

   return $template;
}

And you can use it this way:

<?php

$file = '/path/to/your/file.php';
$data = = array('var1' => 'value',
                'txt' => 'text');

echo get_content($file, $data);

Upvotes: 9

Marc B
Marc B

Reputation: 360602

file.html:

<html>

<body>
<h3>Hi there, <?php echo $name ?></h3>
</body>

</html>

file.php:

<?php
    $name = "Keshav";
    include('file.html');
?>

Doesn't get simpler than this. Yes, it uses global variables, but if simple is the name of the game, this is it. Simply visit 'http://example.com/file.php' and off you go.

Now, if you want the user to see 'file.html' in the browser's address bar, you'd have to configure your webserver to treat .html files as PHP scripts, which is a little more complicated, but definitely doable. Once that's done, you can combine both files into a single one:

file.html:

<?php
    $name = "Keshav";
?>
<html>

<body>
<h3>Hi there, <?php echo $name ?></h3>
</body>

</html>

Upvotes: 13

Kornel
Kornel

Reputation: 100100

Once you iron out all bugs, fix huge performance problem you're getting yourself into, you'll end up with template engine just like Smarty and otheres.

Such find'n'replace approach is much slower than compilation to PHP. It does not handle escaping very well (you'll run into XSS problems). It will be quite difficult to add conditions and loops, and you will need them sooner or later.

Upvotes: 3

Related Questions