如何将参数传递给使用“包含”呈现的 PHP 模板?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1312300/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 01:59:28  来源:igfitidea点击:

How to pass parameters to PHP template rendered with 'include'?

phptemplatesparametersinclude

提问by Tom Haigh

need your help with PHP templating. I'm new to PHP (I'm coming from Perl+Embperl). Anyway, my problem is simple:

在 PHP 模板方面需要您的帮助。我是 PHP 新手(我来自 Perl+Embperl)。无论如何,我的问题很简单:

  • I have a small template to render some item, let it be a blog post.
  • The only way i know to use this template is to use 'include' directive.
  • I want to call this template inside a loop going thru all the relevant blog posts.
  • Problem:I need to pass a parameter(s) to this template; in this case reference to array representing a blog post.
  • 我有一个小模板来呈现一些项目,让它成为一篇博客文章。
  • 我知道使用此模板的唯一方法是使用“包含”指令。
  • 我想在遍历所有相关博客文章的循环中调用此模板。
  • 问题:我需要向这个模板传递一个参数;在这种情况下,引用表示博客文章的数组。

Code looks something like this:

代码如下所示:

$rows = execute("select * from blogs where date='$date' order by date DESC");
foreach ($rows as $row){
  print render("/templates/blog_entry.php", $row);
}

function render($template, $param){
   ob_start();
   include($template);//How to pass $param to it? It needs that $row to render blog entry!
   $ret = ob_get_contents();
   ob_end_clean();
   return $ret;
}

Any ideas how to accomplish this? I'm really stumped :) Is there any other way to render a template?

任何想法如何实现这一点?我真的很难过:) 还有其他方法可以渲染模板吗?

回答by NSSec

Consider including a PHP file as if you were copy-pasting the code from the include into the position where the include-statement stands. This means that you inherit the current scope.

考虑包含一个 PHP 文件,就像将包含代码中的代码复制粘贴到包含语句所在的位置一样。这意味着您继承了当前范围

So, in your case, $param is already available in the given template.

所以,在你的情况下, $param 已经在给定的模板中可用。

回答by Tom Haigh

$param should be already available inside the template. When you include() a file it should have the same scope as where it was included.

$param 应该已经在模板中可用。当您 include() 一个文件时,它应该具有与包含它的位置相同的范围。

from http://php.net/manual/en/function.include.php

来自http://php.net/manual/en/function.include.php

When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward. However, all functions and classes defined in the included file have the global scope.

当一个文件被包含时,它包含的代码继承了包含发生所在行的变量范围。从那时起,调用文件中该行可用的任何变量都将在被调用文件中可用。但是,包含文件中定义的所有函数和类都具有全局作用域。

You could also do something like:

您还可以执行以下操作:

print render("/templates/blog_entry.php", array('row'=>$row));

function render($template, $param){
   ob_start();
   //extract everything in param into the current scope
   extract($param, EXTR_SKIP);
   include($template);
   //etc.

Then $row would be available, but still called $row.

然后 $row 将可用,但仍称为 $row。

回答by Venkat D.

I use the following helper functions when I work on simple websites:

当我在简单的网站上工作时,我使用以下帮助函数:

function function_get_output($fn)
{
  $args = func_get_args();unset($args[0]);
  ob_start();
  call_user_func_array($fn, $args);
  $output = ob_get_contents();
  ob_end_clean();
  return $output;
}

function display($template, $params = array())
{
  extract($params);
  include $template;
}

function render($template, $params = array())
{
  return function_get_output('display', $template, $params);
}

display will output the template to the screen directly. render will return it as a string. It makes use of ob_get_contents to return the printed output of a function.

display 会直接将模板输出到屏幕上。render 将其作为字符串返回。它使用 ob_get_contents 返回函数的打印输出。