开发者

How to implement display like smarty in PHP?

开发者 https://www.devze.com 2022-12-12 10:43 出处:网络
$smarty->assign(\'name\',$value); $smarty->display(\"index.html\"); So that it automatically replaces $variables in index.html which saves a lot of echo开发者_运维百科?You can use something li
$smarty->assign('name',$value);
$smarty->display("index.html");

So that it automatically replaces $variables in index.html which saves a lot of echo开发者_运维百科?


You can use something like this:

// assigns the output of a file into a variable...
function get_include_contents($filename, $data='') {
    if (is_file($filename)) {
        if (is_array($data)) {
            extract($data);
        }
        ob_start();
        include $filename;
        $contents = ob_get_contents();
        ob_end_clean();
        return $contents;
    }
    return false;
}


$data = array('name'=>'Ross', 'hobby'=>'Writing Random Code');
$output = get_include_contents('my_file.php', $data);
// my_file.php will now have access to the variables $name and $hobby


taken from the previous question

class Templater {

    protected $_data= array();

    function assign($name,$value) {
      $this->_data[$name]= $value;
    }

    function render($template_file) {
       extract($this->_data);
       include($template_file);
    }
}

$template= new Templater();
$template->assign('myvariable', 'My Value');
$template->render('path/to/file.tpl');

in the template

<?= $foobar ?>

would print foobar .... if you need to make your own syntax you can use preg_replace_callback

for example :

function replace_var($matches){
    global $data;
    return $data[$matches[1]];
}
preg_replace_callback('/{$([\w_0-9\-]+)}/', 'replace_var');


Using the Templater class from the previous answer you could change the render function to use regular expressions

function render($template_file) {
  $patterns= array();
  $values= array();
  foreach ($this->_data as $name=>$value) {
    $patterns[]= "/\\\$$name/";
    $values[]= $value;
  }
  $template= file_get_contents($template_file);
  echo preg_replace($patterns, $values, $template);
}

......

$templater= new Templater();
$templater->assign('myvariable', 'My Value');
$templater->render('mytemplate.tpl');

And the following template file:

<html>
<body>
This is my variable <b>$myvariable</b>
</body>
</html>

Would render:

This is my variable My Value

Disclaimer: haven't actually run this to see if it works! See the PHP manual on preg_replace, example #2: http://php.net/manual/en/function.preg-replace.php


The functionality you describe is handled by the extract php function, for example:

// Source: http://www.php.net/manual/en/function.extract.php
$size = "large";
$var_array = array("color" => "blue", "size"  => "medium", "shape" => "sphere");
extract($var_array, EXTR_PREFIX_SAME, "wddx");
echo "$color, $size, $shape, $wddx_size\n";

But i would strongly advise you to use one of the classes posted by Sergey or RageZ since else you'll be reinventing the wheel, there are plenty of lowprofile and highend template classes available in PHP, to many actually :)

0

精彩评论

暂无评论...
验证码 换一张
取 消