How to create php template engine?

I need to create a small and simple php template engine, which I searched a lot, and many of them were too complicated to understand, and I do not want to use smarty and other similar engines, I have an idea from Stack Overflow, 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 the echo, the template that I just want to add includes "file.html" and it will display the file with the correct variable values, and I want to put the engine in a separate place and just include it in the template, that I want to use it to declare array, and at the end include an html file, for example phpbb. Sorry, I ask a lot, but can anyone explain the basic concept of this?

EDIT: , , script, , , phpbb, , , . ..: p

+5
4

, script , ?

- :

<?php

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

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

   return $template;
}

:

<?php

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

echo get_content($file, $data);
+5

file.html:

<html>

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

</html>

file.php:

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

, . , , , . "http://example.com/file.php" .

, , "file.html" , - .html PHP, , , , , :

file.html:

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

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

</html>
+11

As soon as you smooth out all the errors, fix the huge performance problem you hit, you will get a template engine like Smarty and others.

This find'n'replace approach is much slower than compiling with PHP. It does not cope with success (you will run into XSS issues). It will be quite difficult to add conditions and cycles, and you will need them sooner or later.

+2
source
    <?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();
+1
source

All Articles