如何在PHP中实现像smarty一样的显示?

发布于 2024-08-11 23:07:07 字数 172 浏览 4 评论 0原文

$smarty->assign('name',$value);
$smarty->display("index.html");

这样它就会自动替换index.html中的$variables,从而节省大量echo

$smarty->assign('name',$value);
$smarty->display("index.html");

So that it automatically replaces $variables in index.html which saves a lot of echo?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

糖果控 2024-08-18 23:07:07

你可以使用这样的东西:

// 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

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
懒的傷心 2024-08-18 23:07:07

取自

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');

模板中的

<?= $foobar ?>

上一个问题将打印 foobar ....如果您需要编写自己的语法,您可以使用 preg_replace_callback

例如:

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

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');
夏花。依旧 2024-08-18 23:07:07

使用 上一个答案中的 Templater 类可以更改渲染函数以使用正则表达式

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');

以及以下模板文件:

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

将渲染:

这是我的变量我的价值

免责声明:还没有实际运行它来看看它是否有效!请参阅有关 preg_replace 的 PHP 手册,示例 #2: http://php.net /manual/en/function.preg-replace.php

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

内心激荡 2024-08-18 23:07:07

您描述的功能由 extract php 函数处理,例如:

// 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";

但我强烈建议您使用 Sergey 或 RageZ 发布的类之一,否则您将重新发明轮子,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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文