php:简单的模板引擎

发布于 2024-10-13 10:08:44 字数 885 浏览 0 评论 0原文

我有一个名为 load_template() 的函数,

该函数有两个参数

  • $name =>模板的名称
  • $vars =>键数组 =>模板中要替换的值变量。

我希望这个工作的方式是。

在模板(“测试”)中,我希望能够编写

<?php echo $title; ?>

然后调用

load_template('test', array('title' => 'My Title'));

并填写它。

我该怎么做?


Output buffering method. I have come up with the code below.
I am sure it can be improved.

public static function template($name, $vars = array()) {
  if (is_file(TEMPLATE_DIR . $name . '.php')) {
    ob_start();
    extract($vars);
    require(TEMPLATE_DIR . $name . '.php');
    $contents = ob_get_contents();
    ob_end_clean();
    return $contents;
  }
  throw new exception('Could not load template file \'' . $name . '\'');
  return false;
}

I have a function called load_template()

this function has two parameters

  • $name => the name of the template
  • $vars => array of key => value variables to be replaced in the template.

the way I want this to work is.

in the template ('test') I want to be able to write

<?php echo $title; ?>

then call

load_template('test', array('title' => 'My Title'));

and have it fill it out.

how can I do this?


Output buffering method.
I have come up with the code below.
I am sure it can be improved.

public static function template($name, $vars = array()) {
  if (is_file(TEMPLATE_DIR . $name . '.php')) {
    ob_start();
    extract($vars);
    require(TEMPLATE_DIR . $name . '.php');
    $contents = ob_get_contents();
    ob_end_clean();
    return $contents;
  }
  throw new exception('Could not load template file \'' . $name . '\'');
  return false;
}

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

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

发布评论

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

评论(2

情泪▽动烟 2024-10-20 10:08:44
function load_template($name, $vars)
{
  extract($vars);
  include $name;
}

ob_startob_get_clean 如果您想捕获变量中的输出。

function load_template($name, $vars)
{
  extract($vars);
  include $name;
}

Wrap with ob_start and ob_get_clean if you want to capture the output in a variable.

我爱人 2024-10-20 10:08:44

像这样的东西吗?

function load_template($name, $vars)
{
  include('template/'.$name.'.tpl'); //.tpl, .inc, .php, whatever floats your boat
}

template/whatever.tpl 中,您会得到:

...
<title><?php echo $vars['title'] ?></title>
...

...
<?php if (!empty($vars['content'])): //template still needs to know if the content is empty to display the div ?>
    <div id="content">
<?php echo $vars['content']; ?>
    </div>
<?php endif; ?>
...

当然,这是假设直接打印输出。
您可以直接打印 tpl 文件,或者生成字符串,或者缓冲 tpl 文件的输出并从 load_template 返回它

Something like this?

function load_template($name, $vars)
{
  include('template/'.$name.'.tpl'); //.tpl, .inc, .php, whatever floats your boat
}

and in template/whatever.tpl you'd have:

...
<title><?php echo $vars['title'] ?></title>
...

...
<?php if (!empty($vars['content'])): //template still needs to know if the content is empty to display the div ?>
    <div id="content">
<?php echo $vars['content']; ?>
    </div>
<?php endif; ?>
...

Of course, that assumes the output being printed directly.
You could have the tpl file print directly, or produce a string, or buffer the output from the tpl file and return it from load_template

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