平面文件站点:没有数据库的 PHP5 主模板
亲爱的朋友们, 想象一个没有数据库的平面 PHP 网站,其中有数百个文件,所有文件中都定义了相同的变量,例如 $read
$look
和 $buys
。
page1.php
<?
$blue= ".....";
$bell= ".....";
$beam= ".....";
?>
page2.php
<?
$blue= ".....";
$bell= ".....";
$beam= ".....";
?>
etcettera.php
现在,一旦我发明了一个新变量,就说 $bike
或 $beaf
然后我必须遍历所有这些模板文件才能添加到它们 $beaf = ""
或那里未定义。我想念一个主模板,可以这么说......欢迎任何想法/提示/代码/建议。提前致谢。
有没有更智能的模板管理方法,无需使用数据库,使维护这些模板变得更容易?
Dear folks,
Imagine a flat php site without database with hundreds of files having the same variables defined in all of them eg $read
$look
and $buys
.
page1.php
<?
$blue= ".....";
$bell= ".....";
$beam= ".....";
?>
page2.php
<?
$blue= ".....";
$bell= ".....";
$beam= ".....";
?>
etcettera.php
Now, as soon as I invent a new variable, say $bike
or $beaf
then I have to go through all those template files in order to add to them $beaf = ""
or else there undefined there. I miss a master template so to say... Any ideas/hints/code/suggestions are welcome. Thanks in advance.
Is there any smarter way of template management without use of database, making it easer to maintain these templates?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
像 Twig 这样的模板引擎可能会帮助你。 Twig 支持模板继承,它允许您定义主模板所有子模板继承自:
master.html.twig:
child.html.twig:
PHP 代码:
A templating engine like Twig might help you. Twig supports template inheritance, which allows you to define a master template that all child templates inherit from:
master.html.twig:
child.html.twig:
PHP code:
我建议创建一个包含 __autoload 函数的文件,
include
它在您的page1.php
的顶部(等等),然后创建一些像这样的类:您可以像
MyVars::book 一样使用它们PHP 文件中的
和MyVars::apple
。I would suggest to create a file that has an __autoload function in it,
include
it at the top of yourpage1.php
(and so on), then create some class like this:and you can use them like
MyVars::book
andMyVars::apple
in your PHP files.您的系统中浮动的平面变量是要避免的事情之一。
使用可以帮助您避免犯此类严重错误的框架,或者仅使用 Smarty
Zend
Your system with flat variables floating around is one of the thing to avoid.
Use a framework that helps you not doing such bad errors or just use Smarty
Zend