平面文件站点:没有数据库的 PHP5 主模板

发布于 2024-10-26 11:09:46 字数 611 浏览 1 评论 0原文

亲爱的朋友们, 想象一个没有数据库的平面 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 技术交流群。

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

发布评论

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

评论(3

浅暮の光 2024-11-02 11:09:46

Twig 这样的模板引擎可能会帮助你。 Twig 支持模板继承,它允许您定义主模板所有子模板继承自:

ma​​ster.html.twig:

<html>
  <head><title>{% block title %}Default Title{% endblock %}</title></head>
  <body>
    <h1>{% block pageHeading}{% endblock %}</h1>
    {% block body}{% endblock %}
  </body>
</html>

child.html.twig:

{% extends master.html.twig %}
{% block title}Child Page{% endblock %}
{% block pageHeading}Weclome!{% endblock %}
{% block body}
<p>My name is {{ name }}.  Today's date is {{ today|date('n/j/Y') }}.</p>
{% endblock %}

PHP 代码:

$loader = new Twig_Loader_Filesystem('/path/to/templates');
$twig = new Twig_Environment($loader, array(
  'cache' => '/path/to/compilation_cache',
));
$template = $twig->loadTemplate('child.html.twig');
echo $templater->render(array("name"=>"Mike", "today"=>new DateTime()));

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:

<html>
  <head><title>{% block title %}Default Title{% endblock %}</title></head>
  <body>
    <h1>{% block pageHeading}{% endblock %}</h1>
    {% block body}{% endblock %}
  </body>
</html>

child.html.twig:

{% extends master.html.twig %}
{% block title}Child Page{% endblock %}
{% block pageHeading}Weclome!{% endblock %}
{% block body}
<p>My name is {{ name }}.  Today's date is {{ today|date('n/j/Y') }}.</p>
{% endblock %}

PHP code:

$loader = new Twig_Loader_Filesystem('/path/to/templates');
$twig = new Twig_Environment($loader, array(
  'cache' => '/path/to/compilation_cache',
));
$template = $twig->loadTemplate('child.html.twig');
echo $templater->render(array("name"=>"Mike", "today"=>new DateTime()));
晨曦慕雪 2024-11-02 11:09:46

我建议创建一个包含 __autoload 函数的文件, include 它在您的 page1.php 的顶部(等等),然后创建一些像这样的类:

class MyVars {
  const book = "Book";
  const apple = "Apple";
}

您可以像 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 your page1.php (and so on), then create some class like this:

class MyVars {
  const book = "Book";
  const apple = "Apple";
}

and you can use them like MyVars::book and MyVars::apple in your PHP files.

稍尽春風 2024-11-02 11:09:46

您的系统中浮动的平面变量是要避免的事情之一。

使用可以帮助您避免犯此类严重错误的框架,或者仅使用 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

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