模板处理语言。

templ的Python项目详细描述


1: Introduction

tem ptemp处理l语言。

templ是一种(图灵完全)编程语言,用于生成内容 从基于文本的模板文件和一个简单而强大的处理语言 (templ本身)直接嵌入到模板文件中。

templ可用于:

  • 代码生成。
  • 形成字母。
  • Web服务器的服务器端脚本。
  • 具有不同输出格式的自定义标记。
  • 任何其他类型的基于文本的动态内容生成。

1.1: Contact Information

此项目当前托管在bitbucket, 在https://bitbucket.org/bmearns/templ/。主要作者是Brian Mearns: 你可以通过Bitbucket在https://bitbucket.org/bmearns联系布赖恩。

2: Examples

下面显示一些简单的示例模板,它们是输出的 一个能用坦普尔做什么的想法。

2.1: Templ Basics

以下模板显示templ模板的一些基本元素:

Hello, World!
My Name is {set :NAME "templ"}. I am the TEMplate Processing Language.
Sometimes, {$ :NAME} likes to speak in the third person.

{$ :NAME} can do math:
    1 + 1 = {+ 1 1}
    1 + 2 = {+ 1 2}
    2 + 3 = {+ 2 3}
    3 + 5 = {+ 3 5}
    etc...

{$ :NAME} can operate on strings and lists:
    {implode "a" {' b n n s}}
    {str {cat {' a b c } {' d e f } }}

{$ :NAME} can do conditional processing:
    {if
        {== {+ 2 2} 5}
        "Oh No!"

        {== {+ 2 2} 4}
        "Phew!"

        "How did I get here?"
    }

{$ :NAME} can loop (and do trig):
{for :THETA {range 0 40 10} {
    echo "    sin(" {$ :THETA} ") = " {sin {rad {$ :THETA}}} {eol}}
}

{$ :NAME} can even do list comprehensions and user defined functions:
{v {set
    :MY-FUNC
    {lambda
        {' :THETA }
        {:
            {let :RADS}
            {$ :RADS {rad {$ :THETA}}}

            {echo "Processing theta=" {$ :THETA} "..." {eol}}

            %return value
            {+ {cos {$ :RADS}} {sin {$ :RADS}} }
        }
    }
}}{wrap "{" "}" {implode {glue "," {eol} "    "} {gen
    :T
    {range 40 80 10}
    {join ":" {$ :T} {:MY-FUNC {$ :T}}}
}}}

输出如下:

Hello, World!
My Name is templ. I am the TEMplate Processing Language.
Sometimes, templ likes to speak in the third person.

templ can do math:
    1 + 1 = 2
    1 + 2 = 3
    2 + 3 = 5
    3 + 5 = 8
    etc...

templ can operate on strings and lists:
    bananas
    [a, b, c, d, e, f]

templ can do conditional processing:
    Phew!

templ can loop (and do trig):
    sin(0) = 0.0
    sin(10) = 0.173648177667
    sin(20) = 0.342020143326
    sin(30) = 0.5


templ can even do list comprehensions and user defined functions:
Processing theta=40...
Processing theta=50...
Processing theta=60...
Processing theta=70...
{40:1.40883205281,
    50:1.40883205281,
    60:1.36602540378,
    70:1.28171276411}

2.2: Code Generation - A Sine Lookup Table

下面的模板演示了如何使用templ生成C代码的示例,在本例中是一个正弦查找表。

{v
    {set :SIZE 10}
}const double sine_lut[{get :SIZE}] =
\{
{for i {range {get :SIZE}} {::
    {let :THETA}
    {$ :THETA {mult
        {$ i}
        {div 360 {$ :SIZE}}
    }}
    {spit {'
        "    "
        {round
            {sin {rad {$ :THETA}}}
            4
        }
        ,
        {\t}
        "// i = "
        {get i}
        ", theta = "
        {$ :THETA}
        " deg"
        {eol}
    }}
}}\};

输出如下:

const double sine_lut[10] =
{
    0.0,    // i = 0, theta = 0 deg
    0.5878, // i = 1, theta = 36 deg
    0.9511, // i = 2, theta = 72 deg
    0.9511, // i = 3, theta = 108 deg
    0.5878, // i = 4, theta = 144 deg
    -0.0,   // i = 5, theta = 180 deg
    -0.5878,        // i = 6, theta = 216 deg
    -0.9511,        // i = 7, theta = 252 deg
    -0.9511,        // i = 8, theta = 288 deg
    -0.5878,        // i = 9, theta = 324 deg
};

2.3: Embedded Data

下一个示例演示templ如何使您轻松地嵌入数据 直接在使用它的模板文件中,允许您 例如,一个受版本控制的文件。

{v
    %Embedded data
    {$ :DATA {'
        %   Name            Year    Month (-1)      Date
        {'  "Alan T."     1912    05              23 }
        {'  "John V."     1903    11              28 }
        {'  "Claude S."   1916    03              30 }
        {'  "George B."   1815    10              2  }
        {'  "George B."   1815    10              2  }
        {'  "Ada L."      1815    11              15 }
        {'  "Charles B."  1791    11              26 }
        {'  "Donald K."   1938    0               10 }
        {'  "Dennis R."   1941    8               9  }
    }}
}{for :ROW {$ :DATA} {:
    {$ :STAMP {stamp {slice 1 {$ :ROW}}}}
    {$ :NOW {stamp}}
    {$ :AGE {floor {div {- {$ :NOW} {$ :STAMP}} {* 60 60 24 365.25}}}}
    {echo {@ 0 {$ :ROW}} ", age " {$ :AGE} " years." {eol} }
}}

它产生这个:

Alan T., age 100 years.
John V., age 109 years.
Claude S., age 96 years.
George B., age 197 years.
George B., age 197 years.
Ada L., age 197 years.
Charles B., age 221 years.
Donald K., age 75 years.
Dennis R., age 71 years.

2.4: Programmatic Invocation

templ的真正威力来自编程接口, 它允许您预定义符号,甚至可执行文件(函数、宏 ,然后可以从模板访问。 因为,尽管templ图灵完成的,而您可以执行所有操作 直接在模板(或单独包含的模板)中执行 python中的高级数据处理可以帮助保持模板文件更简单。

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

推荐PyPI第三方库


热门话题
java Android同步不同页面上的按钮   java评测每个类收集的垃圾对象实例数   java(Spring MVC+Hibernate 4+Test 4)自动连线DAO返回NULL   java Android编辑文本和虚拟键盘   java Selenium与BrowserMobProxy   JAVAlang.NoClassDefFoundError:com/sun/jersey/spi/inject/Errors$关闭原因?   java为什么在我成功登录后仍然会出现“不正确的帐户或密码或用户类型”   安卓应用程序在重新启动java时崩溃。网UnknownHostException:无法解析主机   多线程在Java中同步共享静态对象的正确方法是什么?   未调用自定义注释的java类验证(约束类)   java如何将指定目录的存档文件放入所需位置?   java如何识别Selenium中的每个编辑文本字段,如果它们的Xpath都相同   使用gwtmockito/mockito的java简单单选按钮单元测试?