PHP 模板循环结果

发布于 2024-07-22 05:47:24 字数 534 浏览 2 评论 0原文

因此,在工作中,我们有一个自行开发的模板系统,该系统重量轻,到目前为止对我们来说效果很好。

我们遇到的困难是我们希望能够循环遍历模板。 用例类似于搜索结果。 我们正在努力思考可以做到这一点的方法。 我们现在想出了三种方法。 首先是将 html 存储在循环中,然后对其进行循环并使用 concat 将变量放入 html 中。 我们想到的第二种方法是重复包含一个文件。 第三种方法是包含该文件一次,使用输出缓冲来捕获其输出,向其中添加文本 echo ",然后对其使用 eval。(我的老板希望我们想出创造性的方法来做到这一点)。

嗯,我更喜欢 include 重复方法,因为它允许我们将 html 与逻辑完全分离。 eval 方法也可以做到这一点,但它似乎有点更老套。我们对此做了一些时间测试,发现有。循环内的 html 方法(方法一)是最快的,其次是 eval 方法,并且包含多次实际上比 eval 方法慢 5-6 倍(我们包含了 file/.html)。 文件 1000 次,并进行 100 次取平均值以获得结果)。

评估该 )

或者有人有办法完成这类事情吗?

So here at work we have a home grown templating system, that is light weight and works great for us so far.

The wall we're hitting is that we want to be able to loop over a template. The use case is something like search results. We are trying to think of ways that we could do this. We've come up with three ways right now. First is the good old storing the html inside the loop and then looping over it and using concat to put the variables into the html. The second way we thought up was including a file repeatedly. And the third way was to include the file once, use output buffereing to capture its output, add the text echo " to it, and then using eval on it. (My boss wanted us to come up with creative ways of doing it).

Well, I prefer the include repeatedly method, as it allows us to separate the html from the logic completely. The eval method also does this, but it seems a little more hackish. Well we did some time tests on this and found that having the html right inside the loop (method one) was the quickest, and that was followed by the eval method, and including multiple times came in last. Including was actually about 5-6 times slower than the eval method. (We included the file/evaluated the file 1000 times, and did that 100 times and averaged them to get our results).

Is there any way to speed up the multiple include? (It would appear that each time we do the include php is hitting the file system again.)

Or does anyone have a way of accomplishing this type of thing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

余生共白头 2024-07-29 05:47:24

正如您的测试所示,优化脚本的最佳方法是将已包含的 HTML 代码循环到脚本中。

影响时间来自:

  • eval() 调用 PHP 解析器来验证代码
  • include() 使用磁盘访问

然后,每个 include() 都会降低读取未更改文件的磁盘的性能。 要优化此磁盘访问,您的 eval() 选项将是:

  • include(): intial-parser + [disk-access + parser]*N 循环;
  • eval():initial-parser + disk-access + parser*N 循环
  • 内联 HTML:initial-parser

为了保持代码简洁,您可以创建包含 HTML 的函数、使用全局变量或包含所有 HTML 的单个对象或数组变量需要将数据显示到 HTML 中,例如

function html_comment ($comment)
{
    //global $comment;

    ?>
    <div class="comment">
        <div class="author"><?php echo $comment['author'] ?></div>
        <div class="date"><?php echo $comment['date'] ?></div>
        <div class="date"><?php echo $comment['content'] ?></div>
    </div>
    <?php
}

...
foreach ($comments as $comment)
    html_comment($comment);

As your tests showed, the best way to optimize the script is to cycle over already included HTML code into your script.

The impact time comes from:

  • eval() calls the PHP parser to validate the code
  • include() uses disk access

Each include() then downgrades performance reading the disk of an unaltered file. To optimize this disk access will be then your eval() option:

  • include(): intial-parser + [disk-access + parser]*N loops;
  • eval(): initial-parser + disk-access + parser*N loops
  • inline HTML: initial-parser

To keep the code clean, you can create functions containing the HTML, use global variables, or a single Object or Array variable containing all the data needed to be displayed into your HTML, something like

function html_comment ($comment)
{
    //global $comment;

    ?>
    <div class="comment">
        <div class="author"><?php echo $comment['author'] ?></div>
        <div class="date"><?php echo $comment['date'] ?></div>
        <div class="date"><?php echo $comment['content'] ?></div>
    </div>
    <?php
}

...
foreach ($comments as $comment)
    html_comment($comment);
心凉 2024-07-29 05:47:24

缓存最后一页。 我的意思是把所有的“块”(循环、片段,无论你想怎么称呼它们)并将它们加入到缓存中的一个 PHP 文件中,然后从那时起就将其包含在内。 这样,每个请求只需访问文件系统一次,而不是 25 次。

Cache the final page. What I mean is take all your "blocks" (loops, pieces, whatever you want to call them) and join them into one PHP file in your cache, and just include that from then on. That way you're only hitting the filesystem once per request instead of, say, 25 times.

浮萍、无处依 2024-07-29 05:47:24

老问题,我知道。

无论如何,我建议在模板内有一个简单的 for 或 foreach 循环。 没有什么不妥。 首先,因为在我看来,循环某些表示代码仍然是表示逻辑(因为这也使得将输出从列输出更改为行输出变得更容易 - 愚蠢的例子)。 其次,PHP 仍然是一种模板语言。 在模板中使用一些简单的控制结构没有任何问题。 使用像 Smartys 这样的系统只是添加了另一层抽象(需要学习另一种语法),它具有 PHP 的大部分功能(因此会产生相当多的开销)。 在我看来这是无稽之谈(旁注:对我来说,这是

Old question, I know.

Anyways, I would suggest having a simple for or foreach loop inside the template. There is nothing wrong with that. First, since looping through some presentation code is still presentation logic in my eyes (as this also makes it easier to change the output from, say, output in columns to output in rows - stupid example). Second, PHP is still a templating language. There is nothing wrong with using some simple control structures in a template. Using systems like Smartys is just adding another layer of abstraction (with another syntax to learn), that has much of the power of PHP (and thus creates quite a lot of overhead). That's nonsense in my eyes (on a sidenote: for me that is the inner-platform effect, an anti-pattern).

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