模板处理语言。
templ的Python项目详细描述
内容
1: Introduction
tem p是tem板p处理l语言。
templ是一种(图灵完全)编程语言,用于生成内容 从基于文本的模板文件和一个简单而强大的处理语言 (templ本身)直接嵌入到模板文件中。
templ可用于:
- 代码生成。
- 形成字母。
- Web服务器的服务器端脚本。
- 具有不同输出格式的自定义标记。
- 任何其他类型的基于文本的动态内容生成。
1.1: Contact Information
此项目当前托管在bitbucket, 在https://bitbucket.org/bmearns/templ/。主要作者是Brian Mearns: 你可以通过Bitbucket在https://bitbucket.org/bmearns联系布赖恩。
1.2: Copyright and License
templisfree software:您可以重新分发和/或修改它 根据gnu affero通用公共许可证的条款 自由软件基金会,或者许可证的第3版,或者 (由您选择)任何更高版本。
templ被分发是希望它会有用, 但是没有任何保证;甚至没有 适销性或适合特定用途。见 GNU Affero通用公共许可证了解更多详细信息。
GNU Affero通用公共许可证副本可在模板中获得 在文件license.txt下分发。如果你没有收到 这个文件,请参见http://www.gnu.org/licenses/。
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中的高级数据处理可以帮助保持模板文件更简单。