问题描述
我认为 PHP 模板系统的基本原理是字符串替换,对吗?那么我可以使用一个字符串来保存我的 html 模板代码,比如
I think the basic principle of a PHP templating system is string replacing, right?So can I just use a string to hold my html template code like
$str_template = "<html><head><title>{the_title}</title><body>{the_content}</body></html>"
并且在下面的代码中简单地做一个 str_replace 将数据推送到我的模板变量中,如
and in the following code simply do a str_replace to push the data into my template variable like
str_replace( $str_template, '{the_title}', $some_runtime_generated_title );
str_replace( $str_template, '{the_content}', $some_runtime_generated_content );
最后
echo $str_template;
这是否有望使整个变量传递过程更快一点?我知道这可能是一个奇怪的问题,但有人试过吗?
Will this hopefully make the whole variable passing process a bit faster? I know this could be a weird question but has anybody tried it?
推荐答案
这通常是模板系统的基本思想.真正的模板系统具有更多功能,但它们的核心功能就是这些.
That is generally the basic idea for a templating system. Real templating systems have many more capabilities, but at the core this is what they do.
您问这是否会使整个变量传递过程更快一点".比什么更快?您现在正在做的事情是否遇到了性能问题?
You ask whether this will "make the whole variable passing process a bit faster". Faster than what? Are you running into performance problems with something you're doing right now?
这篇关于使用 str_replace 进行 PHP 模板化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!