{%%}在html中是什么意思 [英] what does {% %} mean in html

查看:130
本文介绍了{%%}在html中是什么意思的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为它用来引用php内容,但是我不确定.我在html文件中看到了一些类似这样的文字:

I think it's used to reference php stuff, but I'm not sure. I see some of it written like this in the html file:

{% if ban.reason %}
    <p class="reason">
        {{ ban.reason }}
    </p>
{% endif %}

推荐答案

这是一个模板引擎系统,其语法基于

It's a Template Engine system, and its syntax is based on jinja. Another code example:

{% extends "layout.html" %}

{% block body %}
    <ul>
        {% for user in users %}
            <li><a href="{{ user.url }}">{{ user.username }}</a></li>
        {% endfor %}
    </ul>
{% endblock %}

来自维基百科:

  • 鼓励将源代码组织到可区分操作的层中(例如,参见MVC)
  • 通过减少不必要的努力重复来提高生产力
  • 通过允许基于技能组合(例如艺术与技术)的工作分离来增强团队合作精神

模板引擎通常包括大多数高级编程语言所共有的功能,重点是用于处理纯文本的功能.这些功能包括:

Template engines typically include features common to most high-level programming languages, with an emphasis on features for processing plain text. Such features include:

  • 变量和函数
  • 文本替换
  • 包含(或包含)文件
  • 条件评估和循环

(来自维基百科)


有几个用于PHP的模板引擎.其中之一是
嫩枝 .

例如,不要这样写:

<?php echo $var ?>
<?php echo htmlspecialchars($var, ENT_QUOTES, 'UTF-8') ?>

您可以使用Twig进行此操作:

You can do this with Twig:

{{ var }}
{{ var|escape }}

另一个例子:

<ul id="navigation">
    <?php if (navigation) { ?>
        <?php foreach ($navigation as $item) { ?>
            <li><a href="<?php echo $item->href; ?>"><?php echo $item->caption; ?></a></li>
        <?php } ?>
    <?php } ?>
</ul>

在模板引擎中:

<ul id="navigation">
    {% for item in navigation %}
        <li><a href="{{ item.href }}">{{ item.caption }}</a></li>
    {% endfor %}
</ul>

这篇关于{%%}在html中是什么意思的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆