zoukankan      html  css  js  c++  java
  • SpringBoot系列: Pebble模板引擎语法介绍

    本文基于Pebble官方文档, 对pebble的语法做一些介绍. 

    ===============================
    Pebble 官方资料
    ===============================
    主页: https://pebbletemplates.io/
    github wiki: https://github.com/PebbleTemplates/pebble/wiki

    ===============================
    Pebble 基本语法
    ===============================
    表达式使用 {{  }} 括起来
    {{expression}}

    注释使用 {#  #} 括起来
    {# This is a comment #}

    控制类语句使用 {%  %} 括起来

    循环控制, 和python类似, for循环中支持else.

    {% for article in articles %}
        <h3>{{ article.title }}</h3>
        <p>{{ article.content }}</p>
    {% else %}
       <p>no article yet</p>
    {% endfor %}

    If 控制

    {% if category == "news" %}
        {{ news }}
    {% elseif category == "sports" %}
        {{ sports }}
    {% else %}
        <p>Please select a category</p>
    {% endif %}

    变量赋值

    {% set header = "Test Page" %}
    {{ header }}

    ===============================
    表达式
    ===============================
    简单表达式
    {{variable}}
    {{obj.property}}

    简单函数表达式
    {{max(var1, var2) }}

    Literals 表达式包括:
        字符串: 使用单引号或双引号括起来.
        true/false: 布尔值
    算术表达式, 加减乘除等等
    null 或 none : 等同于 Java 的 null
    列表集合表达式: [1,2,3] 或 ["one","two"]
    map集合表达式: {"a":1,"b":2}
    两个集合包含操作符为:  contains
    元素是否集合用:  in
    逻辑操作符有:  and , or , not
    比较符号有:  > , < 等等
    相等符号为:  is


    ===============================
    function
    ===============================
    max()
    min()
    range(0, 3)
    range(0, 6, 2)
    0..3 相当于 range(0, 3)

    ===============================
    test 断言
    ===============================
    test 是 pebble 一个非常有特点的概念, 和 is 操作符配合使用, 用来判断表达式是否为真.
    is even , 判断是否为偶数
    is odd, 判断是否奇数
    is null, 判断是否为null
    is empty, 判断是否为 empty, 包括的情形有: 对象为null,或字符串变量为"", 或空列表, 或空map等.
    is map, 判断表达式是否为map
    is iterable, 判断表达式是否可iterable.

    ===============================
    filter
    ===============================
    相比于 function 和 test, filter更经常使用到, 内置的filter有很多, 比如 upper/lower/default 等等,
    {{ name || default('no value')}}

    slice(fromIndex, toIndex) 的取值效果是[fromIndex, toIndex)
    {{ 'Mitchell' | slice(1,3) }}

    数字格式化
    {{ 3.141592653 | numberformat("#.##") }}

    日期格式化
    {{ user.birthday | date("yyyy-MM-dd") }}

    ===============================
    表达式的大小写
    ===============================
    顶级variable变量, 其名称大小写敏感, 比如:
    {{namE}} 和 {{name}} 是不同的.

    属性级的名称, 大小写不敏感, 比如:
    {{book.author}} 和 {{book.authoR}} 是效果一样.

    对于 {{book.author}} 这样的占位符, Pebble 会智能地匹配下面的方案或屬性:
         book.get("author")
         book.getAuthor()
         book.isAuthor()
         book.hasAuthor()
         book.author()
         book.author


    ===============================
    一些特别的tag
    ===============================
    extends tag 用来扩展一个模板
    {% extends "base" %}

    include 用来将一个模板合并当当前文档中
    {% include "advertisement" %}

    import 用来将指定模板中的macro引入
    {% import "form_util" %}

    set 用来定义一个变量
    {% set header = "Test Page" %}
    {{ header }}

    autoescape 用来打开或关闭自动转移, 甚至还可以设定使用何种转义的引擎

    {% autoescape false %}
        {{ danger }} {# will not be escaped #}
    {% endautoescape %}

    verbatim tag 告诉 pebble 不要按照pebble的语法解析tag内的内容, 该功能非常适合和一些 js 前段框架搭配使用.

    {% verbatim %}
        {% for user in users %}
            {{ user.name }}
        {% endfor %}}
    {% endverbatim %}

    macro tag 可以定义一些代码片段, 然后复用这些macro:

    {% macro input(type="text", name, value) %}
        <input type="{{ type }}" name="{{ name }}" value="{{ value }}" />
    {% endmacro %
    
    {{ input(name="country") }}
    {# will output: <input type="text" name="country" value="" /> #}
    
    {% import "form_util" %}
    {{ input("text", "country", "Canada") }}

    ===============================
    pebble的扩展性
    ===============================
    pebble 不仅仅使用简单, 而且扩展性非常好, 可以使用java代码编写自己的 filter/function/test, 甚至可以扩展操作符.

    选Java后端模板引擎强烈推荐pebble. 

  • 相关阅读:
    工厂模式--工厂方法模式(Factory Method Pattern)
    工厂模式--简单工厂模式( Simple Factory Pattern )
    blog2.0--Springboot添加redis缓存(注解方式)
    blog2.0--JSR303参数校验+全局异常处理器
    高并发秒杀——SpringBoot集成redis
    SpringBoot报错HHH000206: hibernate.properties not found
    blog2.0--保存博客文章到本地磁盘
    Swagger注解 传参
    设计模式--单例模式
    跳表
  • 原文地址:https://www.cnblogs.com/harrychinese/p/SpringBoot_pebble_usage.html
Copyright © 2011-2022 走看看