jinja2模板的slim模板语法

slimish-jinja的Python项目详细描述


Slimjinja2的模板语法。

这个版本破坏了兼容性。解析=for jinja输出很复杂。我放弃了它,转而直接使用{{ jinja_output }}

此版本(1.0)以后,将保持向后语法兼容性。

安装
pip install slimish_jinja

使用它作为Jinja2 extension(jinja_demo.py)的示例, 与Flask(flask_demo.py)和独立的(convert.py and demo.py)捆绑在一起。

如果您想将它用于任何其他用途,那么lexer - lexer.pyparser - parser.py就足够简单了。 lexer按行读取输入并生成标记。parser实现一个手动滚动递归下降解析器。

为了快速参考,此修身版:

!5
html
head
    / Inline static content.
    title
    - block title
        |Slimish-Jinja Example
    / Self closing tag with attributes.
    meta name="keywords" content="template language"
    %script type="text/javascript" src="{{ ASSET_URL }}"
    script
    / Empty jinja tag.
    @block script

/ 'id' and 'class' shortcut.
body#home.fluid.liquid
    / Nested static content.
    h1
    |This is my header.
    / 'div' with 'id' and 'class' shortcut.
    #contents.main
    / Empty html tag.
    %div
    p Dynamic {{ content }}
    p
        |Nested dyanmic  {{ content }}
        Left indent is preserved in text blocks.
    p
        |<a href="http://www.google.com">Google</a>
    / Dynamic attributes.
    ul class="{{ user_class }}"
    / Jinja tag.
    - for user in users
        li {{ user.name }}
        - if user.last_name
        li {{ user.last_name }}
        - elif user.middle_name
        li {{ user.middle_name }}
    - else
        li No user found.

转换为::

<!doctype html>
<html>
<head>
    <title>
    {% block title %}
        Slimish-Jinja Example
    {% endblock %}
    </title>
    <meta  content="template language" name="keywords"/>
    <script>
    {% block script %}{% endblock %}
    </script>
</head>
<body id="home" class="fluid liquid">
    <h1>
    This is my header.
    </h1>
    <div id="contents" class="main">
    <div></div>
    <p>Dynamic {{ content }}</p>
    <p>
        Nested dyanmic {{ content }}        Left indent is preserved in text blocks.
    </p>
    <p>
        <a href="http://www.thoughtnirvana.com">ThoughtNirvana</a>
    </p>
    </div>
    <ul  class="{{ user_class }}">
    {% for user in users %}
        <li>{{ user.name }}</li>
        {% if user.last_name %}
        <li>{{ user.last_name }}</li>
        {% elif user.middle_name %}
        <li>{{ user.middle_name }}</li>
        {% endif %}
    {% else %}
        <li>No user found.</li>
    {% endfor %}
    </ul>
</body>
</html>

文档类型删除。

!html      
<!DOCTYPE html>

!5         
<!DOCTYPE html>

!1.1       
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

!strict
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

!frameset
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">

!mobile
<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.2//EN" "http://www.openmobilealliance.org/tech/DTD/xhtml-mobile12.dtd">

!basic
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.1//EN" "http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd">

!transitional
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


!strict
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

!frameset
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">

!transitional
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

静态内容

/ Inline static content.
h1 This is my header

/ Nested static content.
h1
  |This is my header

/ Multiline static content.
p
  |Span mutliple lines.
     Left indent preserved in the output.

动态内容

/ Inline
h1 {{ user.title }}

/ Nested
h1
  {{ user.title }}

/ Mixed with text.
h1
  |The user name is {{ user.name }}

Jinja标签

- for user in users
  li {{ user.name }}
  - if {{ user.lastname }}
    li {{ user.lastname }}
- else
  li No users found

属性中的动态内容
  a href="{{ user.url  }}" {{ user.name }}

idclassname快捷键
#contents.main.liquid => <div id="contents" class="main liquid">

body#home.left => <body id="home" class="left">

代码注释。

/ Single slash comments.
/ Not part of the output.
p This is content.

空HTML标记

%div => <div></div>

清空Jinja标签。

@block title => {%block title %}{% endblock %}

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java应该考虑使用DTO来代替Spring控制器层吗?   java为什么要将Maven与Git结合起来?   java试图将CSV转换为XLSX,但使用了错误的逗号拆分列   mysql Spring 3+Hibernate:java。sql。BatchUpdateException:无法添加或更新子行(ManyToMany)   java基本字符串反转器   java无法使用RestControllerAdvice为身份验证失败生成自定义错误消息   java当只允许SQLException时,如何抛出EOFEException?   java如何创建播放模块?   Android中匿名类的java实例化异常问题   java两个停靠组件,其中第二个组件填充剩余空间   java如何在按钮延迟时启用它   Java中正在运行的应用程序中的后台进程   java我正试图从一个字符串打印出这个字符输出   如何使用java socket通过两个不同的wifi连接两台电脑?   javaapachecamel:如何将分层数据从数据库转换为pojo   java Webrtc:OniconConnectionChange和onConnectionChange之间有什么区别   java如何重写已经创建的JTable方法   爪哇扫雷机堆垛机   雅加达ee Java ee EJB 3.0 Glassfish