Handlebars, avoid compiling (ignore) part of a template?

早过忘川 提交于 2020-12-01 10:16:26

问题


Is there a way to tell the Handlebar compiler to ignore a block of template.

I know there is the \ solution, like :

\{{ is.ignored}}

but is there something that would do the same, but for a complete block, like :

<script type="text/x-handlebars-template" id="my-template">
    <ul>
        {{#each items}}
            <li><a href="{{url}}" title="{{title}}">{{display}}</a></li>
        {{/each}}
    </ul>
</script>

I believe it would be better (and far more readable) to have something like {{#ignore}}{{/ignore}} instead of adding \ everywhere.

I tried to find something using block helpers, either building something myself, but I can't get my hand on the non-compiled version of what's inside the block.


回答1:


Unfortunately, Cyril's answer seems out of date? I found this alternative in the Handlebars documentation on raw blocks:

Raw Blocks

Raw blocks are available for templates needing to handle unprocessed mustache blocks.

{{{{raw-helper}}}}
    {{bar}}
{{{{/raw-helper}}}}

will execute the helper raw-helper without interpreting the content.

Handlebars.registerHelper('raw-helper', function(options) {
    return options.fn();
});

will render

{{bar}}



回答2:


Yes I finally found it, it's called ... raw ! :

{% raw %}
<script type="text/x-handlebars-template" id="my-template">
    <ul>
        {{#each items}}
            <li><a href="{{url}}" title="{{title}}">{{display}}</a></li>
        {{/each}}
    </ul>
</script>
{% endraw %}

Update : After an update of Handlebars, this snipped seems to not work now. I opened a ticket to see how to make it works.



来源:https://stackoverflow.com/questions/23605254/handlebars-avoid-compiling-ignore-part-of-a-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!