替换 php 文件中的 {{string}}
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17869964/
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
Replacing {{string}} within php file
提问by jkushner
I'm including a file in one of my class methods, and in that file has html + php code. I return a string in that code. I explicitly wrote {{newsletter}}
and then in my method I did the following:
我在我的一个类方法中包含一个文件,并且在该文件中有 html + php 代码。我在该代码中返回一个字符串。我明确地写了{{newsletter}}
然后在我的方法中我做了以下事情:
$contactStr = include 'templates/contact.php';
$contactStr = str_replace("{{newsletter}}",$newsletterStr,$contactStr);
However, it's not replacing the string. The only reason I'm doing this is because when I try to pass the variable to the included file it doesn't seem to recognize it.
但是,它不会替换字符串。我这样做的唯一原因是当我尝试将变量传递给包含的文件时,它似乎无法识别它。
$newsletterStr = 'some value';
$contactStr = include 'templates/contact.php';
So, how do I implement the string replacement method?
那么,如何实现字符串替换方法呢?
回答by bitWorking
You can use PHP as template engine. No need for {{newsletter}}
constructs.
您可以使用 PHP 作为模板引擎。不需要{{newsletter}}
构造。
Say you output a variable $newsletter
in your template file.
假设您$newsletter
在模板文件中输出一个变量。
// templates/contact.php
<?php echo $newsletter; ?>
To replace the variables do the following:
要替换变量,请执行以下操作:
$newsletter = 'Your content to replace';
ob_start();
include('templates/contact.php');
$contactStr = ob_get_clean();
echo $contactStr;
// $newsletter should be replaces by `Your content to replace`
In this way you can build your own template engine.
通过这种方式,您可以构建自己的模板引擎。
class Template
{
protected $_file;
protected $_data = array();
public function __construct($file = null)
{
$this->_file = $file;
}
public function set($key, $value)
{
$this->_data[$key] = $value;
return $this;
}
public function render()
{
extract($this->_data);
ob_start();
include($this->_file);
return ob_get_clean();
}
}
// use it
$template = new Template('templates/contact.php');
$template->set('newsletter', 'Your content to replace');
echo $template->render();
The best thing about it: You can use conditional statements and loops (full PHP) in your template right away.
最好的事情是:您可以立即在模板中使用条件语句和循环(完整的 PHP)。
回答by da-hype
This is a code i'm using for templating, should do the trick
这是我用于模板的代码,应该可以解决问题
if (preg_match_all("/{{(.*?)}}/", $template, $m)) {
foreach ($m[1] as $i => $varname) {
$template = str_replace($m[0][$i], sprintf('%s', $varname), $template);
}
}
回答by Octal
maybe a bit late, but I was looking something like this.
也许有点晚了,但我看起来像这样。
The problem is that include does not return the file content, and easier solution could be to use file_get_contents function.
问题是 include 不返回文件内容,更简单的解决方案是使用 file_get_contents 函数。
$template = file_get_contents('test.html', FILE_USE_INCLUDE_PATH);
$page = str_replace("{{nombre}}","Alvaro",$template);
echo $page;
回答by Gustav
Use output_buffers together with PHP-variables. It's far more secure, compatible and reusable.
将 output_buffers 与 PHP 变量一起使用。它更加安全、兼容和可重用。
function template($file, $vars=array()) {
if(file_exists($file)){
// Make variables from the array easily accessible in the view
extract($vars);
// Start collecting output in a buffer
ob_start();
require($file);
// Get the contents of the buffer
$applied_template = ob_get_contents();
// Flush the buffer
ob_end_clean();
return $applied_template;
}
}
$final_newsletter = template('letter.php', array('newsletter'=>'The letter...'));
回答by Denis Dos Santos Silva
based on @da-hype
基于@da-hype
<?php
$template = "hello {{name}} world! {{abc}}\n";
$data = ['name' => 'php', 'abc' => 'asodhausdhasudh'];
if (preg_match_all("/{{(.*?)}}/", $template, $m)) {
foreach ($m[1] as $i => $varname) {
$template = str_replace($m[0][$i], sprintf('%s', $data[$varname]), $template);
}
}
echo $template;
?>
回答by bwoebi
no, don't include for this. include
is executing php code. and it's return value is the value the included file returns - or if there is no return: 1.
不,不要为此包括在内。include
正在执行php代码。它的返回值是包含文件返回的值 - 或者如果没有返回:1。
What you want is file_get_contents()
:
你想要的是file_get_contents()
:
// Here it is safe to use eval(), but it IS NOT a good practice.
$contactStr = file_get_contents('templates/contact.php');
eval(str_replace("{{newsletter}}", $newsletterStr, $contactStr));