安企CMS#其他辅助标签#

一些常用会用到的内置标签。


模板的嵌套引用 include

往往制作模板的时候,我们会将一些公共部分,比如header、footer、aside等部分,抽离出来独立存放,不需要在每一个页面都重复编写,只需要在每一个页面引入它们即可。这个时候,我们可以使用include标签。

{\(<a href="/product/15399.html" data-anchor="10088">7</a>}<br/></p><pre style="text-indent: 0px; text-align: left;"><code>{% include "partial/he<strong data-anchor="667">ad</strong>er.html" %} {% include "partial/footer.html" %} </code></pre><p style="text-indent: 0px; text-align: start;">include可以将一个拆分出来的代码片段(fragment)嵌入到完整的文档中。使用形式是<span style="color: rgb(0, 0, 0); background-color: rgb(240, 240, 240);"><code>{% include "模板文件" %}</code></span>。</p><p style="text-indent: 0px; text-align: start;">如果需要引入的模板不存在的话,它会报错,如我我们不知道引入的模板是否存在,则需要增加<span style="color: rgb(0, 0, 0); background-color: rgb(240, 240, 240);"><code>if_exists</code></span>判断。</p><p style="text-indent: 0px; text-align: start;"><br/></p><pre style="text-indent: 0px; text-align: left;"><code>{% include "partial/he<strong data-anchor="667">ad</strong>er.html" if_exists %} </code></pre><p style="text-indent: 0px; text-align: start;">这样如果he<strong data-anchor="667">ad</strong>er.html模板存在的话,则会引入,即使不存在,也不会报错,只是被忽略掉了。</p><p style="text-indent: 0px; text-align: start;">默认情况下,include引入的模板,它会继承当前模板的所有变量,如果想给include引入的模板增加另外的变量,可以使用{\)17}with来增加。如:


{% include “partial/header.html” with title=“这是声明给header使用的title” %}

这样就给include引入的模板定义了title变量,当前模板的其他变量它同样也可以继续使用了。

如果需要声明多个变量给include引入的模板使用,可以连续使用key=value的形式增加,它们之间使用空格隔开,如:


{% include “partial/header.html” with title=“这是声明给header使用的title” keywords=“这是声明给header使用的keywords” %}

如果只想让include引入的模板使用指定的几个变量,而不是当前模板的所有变量,可以使用only来做限制:


{$27}{% include “partial/header.html” with title=“这是声明给header使用的title” keywords=“这是声明给header使用的keywords” only %}

然后在header.html中使用:


&lt;!DOCTYPE html&gt;
&lt;html lang=“en”&gt;
&lt;head&gt;

&lt;<a href="/news/318.html" data-anchor="218">meta</a> charset="UTF-8"&gt;
&lt;title&gt;{{title}}&lt;/title&gt;
&lt;<strong data-anchor="218">meta</strong> name="keywords" content="{{keywords}}"&gt;

&lt;/head&gt;


模板代码片段宏函数macro

iris.Django模板引擎可以很简便的定义一些宏函数代码片段。宏代码片段相当于一个函数,它只能调用从参数传入的变量。类似于使用include。不过macro有限定的作用域。如文章我们给文章列表的文章item使用macro

{$37}

定义一个宏函数
{% macro archive_detail(archive) %}
&lt;li class=“item”&gt;

&lt;a href="/archive/{{archive.Id}}" class="link"&gt;
    &lt;h5 class="title"&gt;{{archive.Title}}&lt;/h5&gt;
&lt;/a&gt;

&lt;/li&gt; {% endmacro %} 使用定义的宏函数 {% for item in archives %}

{{ archive_detail(item) }}

{% endfor %}

同时宏函数还可以保存到独立的文件中,然后通过import来嵌套进来。当一个文件中包含多个宏函数,可以使用,将隔开连续引入多个宏函数。还可以使用as来设置别名:

保存宏函数到 archive.helper


{% macro archive_detail(archive) %}
&lt;li class=“item”&gt;

&lt;a href="/archive/{{archive.Id}}" class="link"&gt;
    &lt;h5 class="title"&gt;{{archive.Title}}&lt;/h5&gt;
&lt;/a&gt;

&lt;/li&gt; {% endmacro %} {% macro archive_detail2(archive) %} &lt;li class=“item”&gt;

&lt;a href="/archive/{{archive.Id}}" class="link"&gt;
    &lt;h5 class="title"&gt;{{archive.Title}}&lt;/h5&gt;
&lt;/a&gt;

&lt;/li&gt; {% endmacro %}

在index.html中引入:


用import引入:
{% import “archive.helper” archive_detail, archive_detail2 as archive_detail_new, archive_detail as new_item %}
调用:
{% for item in archives %}

{{ archive_detail(item) }}
{{ archive_detail_new(item) }}
{{ new_item(item) }}

{% endfor %}

{$47}

模板的继承 extends

模板的继承有点像ppt中的母版一样,我们定义好一个骨架,将一个页面都写好,大部分不用变动,需要变动的部分使用block标签包裹起来:


{% block title %}

&lt;title&gt;base&lt;/title&gt;  &lt;!-- 如果扩写了就是扩写的,不扩写就还是用base --&gt;

{% endblock %}

这样定义的好处是,可以在继承它的模板中,重写这个block,不重写就按母版来显示。
比如我们定义了一个base.html


&lt;!DOCTYPE html&gt;
&lt;html lang=“en”&gt;
&lt;head&gt;

&lt;<strong data-anchor="218">meta</strong> charset="UTF-8"&gt;
{% block title %}
    &lt;title&gt;base&lt;/title&gt;  &lt;!-- 如果扩写了就是扩写的,不扩写就还是用base --&gt;
{% endblock %}
&lt;!-- 最新版本的 Bootstrap 核心 <a href="/code/1046.html" data-anchor="660">CSS</a> 文件 --&gt;
&lt;link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.<strong data-anchor="10088">7</strong>/dist/<strong data-anchor="660">css</strong>/bootstrap.min.<strong data-anchor="660">css</strong>"
      integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww<strong data-anchor="10088">7</strong>on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"&gt;

&lt;style&gt;
    * {
        margin: 0;
        p<strong data-anchor="667">ad</strong>ding: 0;
    }

    .he<strong data-anchor="667">ad</strong>er {
        width: 100%;
        height: 50px;
        background-color: #369;
    }
&lt;/style&gt;

&lt;/head&gt; &lt;body&gt;

&lt;div class=“header”&gt;&lt;/div&gt;

&lt;div class=“container”&gt;

&lt;div class="row"&gt;
    &lt;div class="col-md-3"&gt;
        {% include 'aside.html' %}
    &lt;/div&gt;
    &lt;div class="col-md-9"&gt;
        {% block content %}
            &lt;h4&gt;content&lt;/h4&gt;
        {% endblock %}
    &lt;/div&gt;
&lt;/div&gt;

&lt;/div&gt; &lt;/body&gt; &lt;/html&gt;

然后在index.html中继承这个base.html

{$57}

{% extends ‘base.html’ %}

{% block title %}

&lt;title&gt;index&lt;/title&gt;

{% endblock %}

{% block content %}

&lt;div class="col-md-9"&gt;
    &lt;h3&gt;index&lt;/h3&gt;
    &lt;p&gt;index content&lt;/p&gt;
&lt;/div&gt;

{% endblock %}

这样就是使用base.html作为母版,并在index.html 中重写了title、content两个部分。

注意:如果你在模版中使用 {% extends %} 标签,它必须是模版中的第一个标签。其他的任何情况下,模版继承都将无法工作。

在使用继承的情况下,尽可能将可能会变动的数据,都包裹在block中,因为block即使在后续的页面不重写,也不影响模板的解析,而需要重写的时候就更方便。

同样地,如果后续写到某一块,发现多个页面都需要使用到,那么这时候,就把添加到base.html中去,让它成为母版的一部分。


变量的输出

Django 模板中遍历复杂数据结构的关键是句点字符.,变量输出边界定义是双大括号。有一个对象是people,它有Name、Gender、Level属性,在模板中输出就是:

{\(6<strong data-anchor="10088">7</strong>}<br/></p><pre style="text-indent: 0px; text-align: left;"><code>&lt;ul&gt; &lt;li&gt;<a href="/news/47.html" data-anchor="16">网站</a>:{{siteName}}&lt;/li&gt; &lt;li&gt;名字:{{people.Name}}&lt;/li&gt; &lt;li&gt;性别:{{people.Gender}}&lt;/li&gt; &lt;li&gt;等级:{{people.Level}}&lt;/li&gt; &lt;/ul&gt; </code></pre><p style="text-indent: 0px; text-align: start;"><br/></p>{\)70}在模板中使用struct结构体内置方法{\(<strong data-anchor="10088">7</strong>1}比如在archive列表中,archive的结构体中,定义了内置函数{\)72}func (archive *Archive) GetThumb(),那么在模板中,是可以直接调用的。如:

{\(<strong data-anchor="10088">7</strong>3}<br/></p>{\)74}{% for item in archives %} &lt;li class=“item”&gt;

&lt;a href="/archive/{{item.Id}}" class="link"&gt;
    &lt;img src="{{item.GetThumb()}}" alt="{{item.Title}}" /&gt;
    &lt;h5 class="title"&gt;{{item.Title}}&lt;/h5&gt;
&lt;/a&gt;

&lt;/li&gt; {% endfor %} {\(<strong data-anchor="10088">7</strong>5}模板可以直接使用{\)76}{{item.GetThumb()}}来调用内置的{\(<strong data-anchor="10088">7</strong><strong data-anchor="10088">7</strong>}<code>archive.GetThumb()</code></span>方法。</p>{\)78}

{\(<strong data-anchor="10088">7</strong>9}在模板中输出当前时间</h3><p style="text-indent: 0px; text-align: start;"><span style="color: rgb(0, 0, 0); background-color: rgb(240, 240, 240);"><code>now</code></span>标签提供在模板中输出当前时间。now格式化时间的参数遵循golang的时间格式化规则。如果增加fake 参数,则会输出一个特定的加时间代替当前时间。如:</p><p style="text-indent: 0px; text-align: start;"><br/></p><pre style="text-indent: 0px; text-align: left;"><code>{% now "Mon Jan 2 15:04:05 -0<strong data-anchor="10088">7</strong>00 MST 2006" fake %} {% now "2006-01-02 15:04" %} </code></pre><p style="text-indent: 0px; text-align: start;"><br/></p><h3 style="text-indent: 0px; text-align: start;" id="content-dir-6"><span style="color: rgb(0, 0, 0); background-color: rgb(240, 240, 240);"><code>lorem</code></span> 随机生成拉丁文样本数据</h3>{\)87}显示随机的“ lorem ipsum”拉丁文本。 这对于在模板中提供样本数据很有用。也就是占位内容。在开发模板没有真实数据的时候,使用这个标签可以快速填充足够多的随机数据。如:


—–

{% lorem %}

{% lorem 10 %}

{% lorem 3 p %}

{% lorem 100 w %}


模板的注释

iris.Django的注释我们使用大括号+#来实现注释:{# 注释内容 #}

单行注释使用 {# 这只能注释单行 #},多行注释使用 {\(9<strong data-anchor="10088">7</strong>}<code>{% comment %}这里注释很多行{% endcomment %}</code></span>。</p><p style="text-indent: 0px; text-align: start;">示例:</p><p style="text-indent: 0px; text-align: start;">空单行注释</p><p style="text-indent: 0px; text-align: start;"><br/></p><pre style="text-indent: 0px; text-align: left;"><code>{# #} </code></pre><p style="text-indent: 0px; text-align: start;">单行注释</p><p style="text-indent: 0px; text-align: start;"><br/></p><pre style="text-indent: 0px; text-align: left;"><code>{# testing single line comment #} </code></pre><p style="text-indent: 0px; text-align: start;">用有效标签填充单行注释</p><p style="text-indent: 0px; text-align: start;"><br/></p>{\)107}{# testing single line comment {% if thing %}{% endif %} #}

用无效标签填充单行注释


{# testing single line comment {% if thing %} #}

用无效语法填充单行注释


{# testing single line comment {% if thing(“) %}wow{% endif %} #}

空块注释


{% comment %}{% endcomment %}
{$117}填充文本单行块注释


{% comment %}filled block comment {% endcomment %}

空多行块注释


{% comment %}

{% endcomment %}

阻止带有其他标签的注释


{% comment %}
  {{ thing_goes_here }}
  {% if stuff %}do stuff{% endif %}
{% endcomment %}

阻止其中带有无效标签的注释

{$127}

{% comment %}
  {% if thing %}
{% endcomment %}

使用无效语法阻止注释


{% comment %}
  {% thing(”) %}
{% endcomment %}

注释之间的常规标签,以确保其在词法分析器中不会中断


{% if hello %}
{% endif %}
after if
{% comment %}All done{% endcomment %}


上一篇: 没有了

下一篇: 安企CMS#更多过滤器#