指甲是白色的是什么原因| 六畜兴旺是什么意思| 天秤座男生和什么星座最配| 什么是普惠性幼儿园| 看血管挂什么科| 右派是什么意思| 什么的衣裳| 建日是什么意思| 若无其事的若是什么意思| 李子什么时候吃最好| 白眼狼是什么意思| 麻醉对身体有什么伤害| rt是什么| 什么工作赚钱最快| 吃什么食物有助于睡眠| 牙龈肿痛是什么原因| 不宜是什么意思| 碎石后要注意些什么| 张韶涵什么星座| msms筛查是什么意思| 什么人容易得尿毒症| 羡慕是什么意思| 甲醛有什么危害| 精液为什么是苦的| 大姨妈来了能吃什么水果| e m s是什么快递| 小腿抽筋吃什么药| 外贸原单是什么意思| 做完雾化为什么要漱口| 一代明君功千秋是什么生肖| 防晒衣什么品牌好| 右手大拇指发麻是什么原因| 习字五行属什么| 孕吐什么时候出现| 脊柱侧弯拍什么片子| 膈应什么意思| 36年属什么生肖| 血管钙化是什么意思| 颈动脉硬化有什么症状| 怀孕养狗对胎儿有什么影响| 湿气重是什么原因引起的| 强调是什么意思| 外阴瘙痒用什么洗| 肉桂茶适合什么人喝| 阴道内壁是什么样的| 9月16日是什么星座| 倾向是什么意思| 真菌孢子阳性什么意思| 腌牛肉放什么调料| 结婚前一天晚上的宴会叫什么| 什么叫抑郁症| 来大姨妈前有什么症状| 鸡精是什么做的| 回是什么生肖| 吃什么对脾胃有好处| 绩效工资是什么意思| 发迹是什么意思| 梦见出国了是什么意思| 漫山遍野是什么生肖| 扁平疣用什么治疗| 什么的哭| 吃什么主食减肥最快| 骨质增生挂什么科| 癫疯病发作前兆有什么症状| 鼻炎是什么| 7月20号什么星座| 94年属什么今年多大| 吃什么可以降尿酸| 调停是什么意思| 户口所在地是什么意思| 小孩睡觉说梦话是什么原因| 减肥应该吃什么| 昭字五行属什么| 驾驶证b2能开什么车| 暗的反义词是什么| 宫颈多发囊肿是什么意思| 啤酒对身体有什么好处| 蛇标志的车是什么牌子| 失恋是什么意思| 心脏由什么组织构成| 皂矾是什么| 一直打嗝吃什么药| 晚上9点是什么时辰| 6月23号是什么日子| 摇头晃脑是什么生肖| 吃人参对身体有什么好处| 马头琴是什么族的乐器| 梦见买房子是什么预兆| 小孩经常发烧是什么原因| 刘强东开什么车| 音叉是什么| 右下腹有什么器官| 1957年属什么生肖| 脱脂牛奶适合什么人喝| 为什么不敢挖雍正陵墓| 早上7点多是什么时辰| 梦见家里好多蛇是什么预兆| 今年阴历是什么年| 榴莲苦是什么原因| 七七年属什么生肖| 皮肤过敏用什么药最好| 热狗为什么叫热狗| 红细胞阳性是什么意思| 慢性咽喉炎吃什么药| 净身出户是什么意思| 月经吃什么食物好| 肝脏不好吃什么食物才能养肝护肝| 西瓜像什么| 什么东西放进去是硬的拿出来是软的| 什么叫阴吹| 作是什么意思| 雉是什么动物| 丙型肝炎病毒抗体阴性什么意思| 女为读什么| 三甲医院是什么意思| 尿急尿频尿不尽吃什么药| 血糖高可以喝什么饮料| 冬瓜和什么不能一起吃| 痛风性关节炎吃什么药| 痔疮很痒是什么原因| 什么是黑色素瘤| 夜尿多是什么原因引起的| 属鸡今年要注意什么| 饭后腹胀是什么原因| 新陈代谢是什么| 正印代表什么意思| 移花接木什么意思| 竹笋不能和什么一起吃| 蓟什么意思| 六月下旬是什么时候| 豆汁是什么做的| 为什么会晒黑| 红色加绿色是什么颜色| 空调自动关机是什么原因| courvoisier是什么酒| 地道战在河北什么地方| 女人的逼是什么意思| 燃气灶什么品牌好| 晚上吃什么能减肥| 胎盘下缘达宫颈内口是什么意思| 6月份是什么季节| 牛仔是什么面料| 女人小腹坠痛是什么原因| 脱氧核糖是什么| 腹部超声检查什么| 西加一横读什么| 常喝红茶有什么好处| 提炼是什么意思| 梦见煎鱼是什么预兆| 患得患失什么意思| 肝不好吃什么药效果好| 夸父是一个什么样的人| 于无声处是什么意思| 什么是复句| 梦见老公怀孕什么预兆| 手足口病什么症状| 1949年是什么年| 喝啤酒头疼是什么原因| 一切就绪是什么意思| 半斤八两什么意思| 凉拌菜用什么醋最好| sjb什么意思| 下作是什么意思| 喝什么可以减肥瘦肚子| 1129是什么星座| 火龙果是什么颜色| 视黄醇结合蛋白是什么| 打喷嚏是什么原因| 白内障有什么症状表现| 黄鼠狼是什么科| 甲字五行属什么| 吃姜有什么好处| 阿耨多罗三藐三菩提是什么意思| 孤单的反义词是什么| 医院体检挂什么科| 东盟是什么意思| 老是头晕是什么原因| 酸是什么| foreplay是什么意思| 什么是脑中风| 丑人多作怪什么意思| 江西古代叫什么| 三朵玫瑰花代表什么意思| 什么叫重本大学| 宵字五行属什么| 入珠是什么意思| 刍狗是什么意思| 吃东西想吐是什么原因| 弟子规是什么意思| 交叉感染是什么意思| 授记是什么意思| 虾和什么相克| 黄茶属于什么茶| 耐药是什么意思| 上火了吃什么药好| 怀孕胎盘低有什么影响| 虚汗是什么症状| 蛀虫指什么生肖| 偷鸡不成蚀把米什么意思| 怀孕两个月有什么症状| 入职需要准备什么材料| 刚怀孕初期吃什么好呢| 肺癌靶向治疗是什么意思| 什么品牌的书包质量好| 托帕石是什么宝石| 为什么会长痔疮| 做肠镜有什么危害| 过敏用什么药| 手术后吃什么水果| 什么叫通分| 鸡眼膏为什么越贴越疼| 肝火旺盛吃什么| 93年属鸡是什么命| 蜈蚣进家有什么预兆| 心肌供血不足用什么药| 汗管瘤什么原因造成| 平安夜什么时候吃苹果| 真露酒属于什么酒| 微针有什么功效| 支气管炎是什么症状| 宋朝前面是什么朝代| 为什么总打喷嚏| 皱褶什么意思| 狗是什么时辰| 腊肉炒什么菜最好吃| 朗字五行属什么| 草字头加全念什么| iv医学上什么意思| 掉头发缺什么维生素| hcg值低是什么原因| 吃什么排铅| 屌丝男是什么意思| 东北方向五行属什么| 寒湿吃什么药| 胸部疼挂什么科| 野兽之王是什么动物| 鸡精和味精有什么区别| 什么动物有四个胃| 糖化血红蛋白是什么意思| 八月五号是什么星座| 技校是什么学历| 血沉偏高是什么原因| 吃二甲双胍为什么会瘦| 小孩发烧流鼻血是什么原因| 冰晶是什么东西| 老虎下山下一句是什么| 华五行属什么| 想长胖喝什么奶粉好| 手上的月牙代表什么意思| 净空是什么意思| 下午3点到5点是什么时辰| 蜈蚣长什么样| 什么样的风景| 阿莫西林治疗什么| 狗是什么时辰| 遗精是什么意思| 千什么百什么| 精工手表什么档次| 吃紫菜有什么好处和坏处| 甲低有什么危害| 师五行属什么| 梦到丢了一只鞋是什么意思| 帕金森病是什么病| 心脏支架和搭桥有什么区别| 百度
Skip to content

hyperturtle/Stache

Folders and files

NameName
Last commit message
Last commit date

Latest commit

?

History

23 Commits
?
?
?
?
?
?
?
?
?
?
?
?
?
?

Repository files navigation

Build Status

Stache

Trimmed mustache logic-less templates

Implements everything from Mustache.5 except for lambdas in < 200 lines of code. Plus four new things. Implied closing tags {{/}}, Self referencer {{.}}, Existence check {{?exists}}{{/exists}} and data pusher {{< blah}}{{/blah}}, {{:default}}

Also, the ability to compile to javascript code!

render_js(template_string)

Compiles an inline script to javascript code

Stache().render_js_template(template_name)

Compiles all the templates and sets the entry point to the template name

render_all_js()

Compiles all the templates and returns a template object.

stachio = Stache()
stachio.add_template('template_name', templatefile.read())
write('var t = ' + stachio.render_all_js() ';')
var content = t['template_name']([{myparams:3}])
$("#container").html(content)

Why?

Because the current Pystache implementation has holes. And because I wanted to learn about python generators. As a result my codebase is considerabley smaller and easier to grok too(at least for me). It consists of two main methods, _tokenize, and _parse, both python generators. _tokenize creates tokens and _parse consumes and renders them. Also benchmarking the two with my tests, mine was slightly faster, around 2x to 3x.

Existing Stuff

{{tag}}

Renders the value of tag, html escaped, within the current scope

{{{unescape}}} & {{&unescape}}

Don't html escape the value

{{#section}}{{/section}}

Section blocks. Renders the enclosed block if

  • section is true
  • section exists

If section exists and is a(n):

  • Array: It renders the enclosed block for each element in the array, placing the current element in scope
  • Dict: It renders the enclosed block once and places the Dict as the current scope

{{^invert}}{{/invert}}

Renders the enclosed block if invert is an empty string, empty array, false, or doesn't exist. The opposite the the section block.

{{! comments - ignore me }}

Ignores the text within the tag

{{>partial}}

Looks up the partial template and renders it with the current context

New Stuff

{{/}} Implied closing tag

Whenever you use {{/}} it implies the closing of the nearest block.

{{#open}}stuff goes here{{/}}

Is the same as:

{{#open}}stuff goes here{{/open}}

{{.}} Self Referencer

This renders the current "scope". This is useful if you want to iterate over an array and wrap them.

{{#array}}<li>{{.}}</li>\n{{/array}}

with array = [1,2,3,'yay'] will produce:

<li>1</li>
<li>2</li>
<li>3</li>
<li>yay</li>

Existence Check {{?exists}}{{/}}

Forces a check of the tag name, rather than imply that it is a section block. This is useful for check if an array has members rather than iterate over the members

{{?array}}
stuff\n
{{/}}

with {array: [1, 2, 3, 4]} results in:

stuff

as opposed to

{{#array}}
stuff\n
{{/}}

which would render

stuff
stuff
stuff
stuff

{{:default}}stuff{{/}}

This is equivalent to {{default}}{{^default}}stuff{{/}}

It renders the enclosed section if default doesn't exist, empty or false

{{<thing}} Pusher {{/thing}}

It renders the inner block and adds it to the global scope.

{{<thing}}
It takes this. You can put anything in here.
{{tags}}, {{#blocks}}{{/blocks}}, etc.
{{/thing}}

and it populates the global scope with a key of thing. Watch out, it can override existing vars. A convention such as

{{<namespace.thing}}{{/namespace.thing}}

or similiar will help with collisions. This is helpful if you want to use stache templates for masterpages/inheritance. Lets say you have these templates:

master =

<div id="header">
{{header}}
</div>

<div id="footer">
{{footer}}
</div>

page =

{{<header}}
{{name}}
{{/header}}

{{<footer}}
footer
{{/footer}}

{{>master}}

Rendering the page template with {'name': 'Stachio'} will produce

<div id="header">
Stachio
</div>

<div id="footer">
footer
</div>

You can also apply the inverted block or default block to supply default blocks

master =

<div id="header">
{{header}}
{{^header}}Default Header{{/header}}
</div>

<div id="footer">
{{:footer}}Default Footer{{/footer}}
</div>

Rendering {{<footer}}Custom Footer{{/footer}}{{>master}} with {} will produce

<div id="header">
Default Header
</div>

<div id="footer">
Custom Footer
</div>

Install

pip install stache

Test

You can run python test.py or if you have nosetests:

cd stache
nosetests

Benchmark

python test.py

Usage:

>>> from Stache import Stache
>>> Stache().render("Hello {{who}}!", dict(who="World"))
Hello World

or

>>> import Stache
>>> Stache.render("Hello {{world}}!", dict(world="Stache!"))
Hello Stache!

To populate partials:

>>> from Stache import Stache
>>> stachio = Stache()
>>> stachio.add_template('main', 'a = {{a}};')
>>> stachio.add_template('main1', 'b = [ {{#b}}{{.}} {{/b}}];')
>>> stachio.add_template('main2', 'stachio')
>>> stachio.add_template('woah', '{{>main}} {{>main1}} {{>main2}}')
>>> stachio.render_template('woah',dict(a=1, b=[1,2,3,4,5]))
a = 1; b = [ 1 2 3 4 5 ] stachio

If you want to put in dynamic file loading of partials you can override Stache().templates with a dict() like object and override the __get__ and load the template in __get__ if it doesn't exist. Once you load up the template, you'll need to call self.add_template(template_name, template) to tokenize the template.

I don't think this is ideal though... Ideas for populating partials are welcome.

Efficient use with async wsgi:

For wsgi apps that support async, you can yield parts of the rendered template as they render. render_iter and render_template_iter both produce iterators that are yield'ed as it is generated.

>>> for part in Stache.render_iter("Hello {{world}}!", dict(world="Stache!")):
>>>     yield part
Hello
Stache!

Timeline:

I'm wary of lambdas, because I want the templates to be language agnostic. The main reason I liked Mustache in the first place is because of possibility of template reuse.

Some future ideas I have is rendering to javascript templates to be used on browser frontend, bypassing the need for a client side script to compile it into javascript

About

Trimmed mustache logic-less templates for python

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

酿酒用什么菌 着痹是什么意思 杜甫被称为什么 经常低血糖是什么原因 耳廓上长痣代表什么
拉肚子喝什么水 女为读什么 腈纶是什么面料优缺点 一件代发是什么意思 红醋是什么醋
月经不能吃什么水果 什么叫散光 双侧卵巢多囊性改变是什么意思 四川地震前有什么预兆 女人梦见蛇缠身是什么预兆
什么情况下做冠脉ct 酸奶对人体有什么好处 儿童回春颗粒主要治什么 什么是虚荣心 什么叫做t
什么什么的玉米hcv9jop7ns1r.cn 人流需要准备什么东西hcv9jop4ns1r.cn 雷替斯是什么药gysmod.com 西洋参什么人不能吃hcv8jop6ns9r.cn 罗马布是什么面料hcv8jop1ns3r.cn
小葫芦项链是什么牌子hcv8jop0ns6r.cn 切除一侧输卵管对女性有什么影响fenrenren.com 农历七月是什么星座hcv8jop7ns3r.cn 什么东西越热越爱出来gysmod.com 憨厚是什么意思hcv7jop9ns8r.cn
萎缩性胃炎可以吃什么水果hcv9jop4ns8r.cn 计抛是什么意思hcv9jop3ns4r.cn 超敏c反应蛋白是什么xianpinbao.com 蹶是什么意思hcv9jop3ns7r.cn 杜鹃花什么颜色hlguo.com
肠易激综合症用什么药能治好wmyky.com 绿色加红色是什么颜色hcv7jop5ns2r.cn 燕窝是什么东西做成的hcv9jop5ns4r.cn 吃什么能消除脂肪瘤hcv8jop3ns3r.cn 疱疹吃什么药可以根治hcv9jop0ns7r.cn
百度