Mustache PHP将模板放入模板中

时间:2015-01-18 16:38:24

标签: php html mustache mustache.php

我正在尝试使用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文件(考虑到另外两个<section>只有相同数量的代码)

<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>

1 个答案:

答案 0 :(得分:2)

你在namings中有点迷失。 Mustache即将猜测它的名字包含哪个模板。那就是说,

{{> section}}

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

您应该将content.html重命名为content.mustache并添加内容:

{{> content}}

希望它有所帮助。