【问题标题】:Mustache PHP Put a template in a templateMustache PHP 将模板放入模板中
【发布时间】:2015-01-18 16:38:24
【问题描述】:

我正在尝试使用 Mustache 的 PHP 实现将一些 html 内容嵌入到这样的模板中:

[mytemplate begin]
   [html content]
[mytemplate end]

我的 html 内容在一个 html 文件中,当我尝试渲染它时出现错误:

echo $template->render(array('content' => file_get_contents('content.html')));

这是错误:

致命错误:已达到最大函数嵌套级别“100”,正在中止!在第 257 行的 C:\wamp\www\resume\application\mustache\src\Mustache\Engine.php 中

如何将其作为 html 内容插入?

section.mustache 文件(我删除了很大一部分 html 代码,这只是必不可少的)

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>            
    </head>
    <body>
        <!-- CONTENT -->
        <div class="container">
            {{> section}}
        </div>
    </body>
</html>

还有 content.html 文件(假设还有两个 &lt;section&gt; 代码量相同)

<div class="section">
    <!--   Icon Section   -->
    <div class="row">
        <div class="col s12 m8 offset-m2 l8 offset-l2">
            <section id="sports" class="section scrollspy">
                <h1>Sports</h1>
                <div class="card">
                    <div class="card-image waves-effect waves-block waves-light">
                        <img src="media/images/climbing.jpg">
                    </div>
                    <div class="card-content">
                        <span class="card-title activator grey-text text-darken-4">Escalade<i class="mdi-navigation-more-vert right"></i></span>
                    </div>
                    <div class="card-reveal z-depth-3">
                        <span class="card-title grey-text text-darken-4"><h4>Escalade</h4><i class="mdi-navigation-close right"></i></span>
                        <p class="flow-text">Here is some more information about this product that is only revealed once clicked on. </p>
                    </div>
                </div>
                <div class="card">
                    <div class="card-image waves-effect waves-block waves-light">
                        <img src="media/images/snowboard.jpg">
                    </div>
                    <div class="card-content">
                        <span class="card-title activator grey-text text-darken-4">Snowboard<i class="mdi-navigation-more-vert right"></i></span>
                    </div>
                    <div class="card-reveal">
                        <span class="card-title grey-text text-darken-4"><h4>Snowboard</h4><i class="mdi-navigation-close right"></i></span>
                        <p class="flow-text">Here is some more information about this product that is only revealed once clicked on.</p>
                    </div>
                </div>
            </section>
        </div>
        <div class="col hide-on-small-only s12 m2 l1">
            <div style="height: 1px;">
                <ul class="section table-of-contents pinned" style="top: 0px;">
                    <li><a href="#sports" class="">Sports</a></li>
                   </ul>
            </div>
        </div>
    </div>
</div>

【问题讨论】:

  • 你能分享一下小胡子模板和content.html吗?
  • 当然,刚刚做到了。这些是注释完整的代码,我只是把必要的

标签: php html mustache mustache.php


【解决方案1】:

你有点迷失在命名上。 Mustache 即将猜测它的名称将包含哪个模板。也就是说,

{{> section}}

包括section.mustache。由于它出现在您的section.mustache 中,因此引擎会尝试递归地将其插入自身。这显然会导致无限循环,该循环在嵌套级别 100 处终止,并收到您收到的错误。

您应该将您的 content.html 重命名为 content.mustache 并包含内容:

{{> content}}

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-06
    • 2014-07-09
    • 1970-01-01
    • 1970-01-01
    • 2011-11-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多