跳转到主要内容

一套django模板工具。

项目描述

一套简单工具,可以让您在django模板中使用更轻松。基于优秀的django-classytags库。

安装

  1. 安装包

$> pip install django-template-goodies
  1. 将“template_goodies”添加到INSTALLED_APPS。

用法:render_with & def_block 和 use_block

这是一个类似宏的解决方案,允许您定义一个始终看起来相同的基模板,并在其中仅填充不同的“块”。

假设您有部分模板person_wrapper.html

{% load goodies %}
<div class="person-wrapper">
    <div class="first-name">{{ first_name }}</div>
    {% use_block "block" %}
</div>

您可以使用以下方式使用它

{% load goodies %}
{% render_with "person_wrapper.html" first_name="First" last_name="Last" %}
    {% def_block "block" %}
    <div class="last-name">{{ last_name }}</div>
    {% end %}
{% end %}

此调用将用“{% use_block “block” %}”替换person_wrapper.html中的内容,并在“render_with”内部使用“{% def_block …. %}”和“{% end %}”。所以上面的结果大致如下

<div class="person-wrapper">
    <div class="first-name">First</div>
    <div class="last-name">Last</div>
</div>

另一个表格的例子,table.html

{% load goodies %}
<table class="{{ opts.table_class }}">
    <thead>
        <tr>
            {% use_block "headers" %}
        </tr>
    </thead>
    <tbody>
        {% for row in object_list %}
        <tr>
            {% use_block "row" %}
        </tr>
        {% endfor %}
    </tbody>
</table>

可以以下方式使用

{% render_with "table.html" object_list=people %}{% def_block "headers" %}
    <td>ID</td>
    <td>First name</td>
    <td>Last Name</td>
    <td>Age</td>
{% end %}{% def_block "row" %}
    <td>{{ forloop.counter }}</td>
    <td>{{ row.first_name }}</td>
    <td>{{ row.last_name }}</td>
    <td>{{ row.age }}</td>
{% end %}{% end %}

用法:dict

更新或创建模板内的字典。如果需要,您可以在变量上使用所有类型的过滤器

{% load goodies %}

{% dict opts name="first"|title %}
{% dict opts last_name="last" value=opts.name %}

{{ opts.first }} - {{ opts.last_name}} || {{ opts.value }}

结果大致如下

First - last || first

要求

  • django-classytags

作者

项目详情


下载文件

下载适用于您平台的应用程序。如果您不确定选择哪个,请了解有关安装包的更多信息。

源分布

django_template_goodies-0.1.5.tar.gz (2.9 kB 查看哈希值)

上传时间 源代码