hogan html模板,javascript - Can Mustache Templates do template extension? - Stack Overflow

I'm playing around with this right now in Python (note I'm the creator of Mako), adding in a dynamic context that captures sections seems to be doing the right thing, though I'd need to test this a lot more.

Basically we are using lambdas, where a "

import pystache

class NameSpace(object):

def __init__(self, renderer, vars_={}):

self.renderer = renderer

self._content = {}

self.vars = vars_

def add_content(self, name, value):

self._content[name] = value

def __getattr__(self, key):

if key in self.vars:

# regular symbol in the vars dictionary

return self.vars[key]

elif key.startswith("

# an "inherit from this template" directive

name = key[1:]

return inheritor(self, name)

elif key.startswith("$"):

# a "here's a replaceable section" directive

name = key[1:]

if name in self._content:

# if we have this section collected, return the rendered

# version

return sub_renderer(self, name)

else:

# else render it here and collect it

return collector(self, name)

else:

# unknown key.

raise AttributeError(key)

def sub_renderer(namespace, key):

def go():

def render(nested):

return namespace._content[key]

return render

return go

def collector(namespace, key):

def go():

def render(nested):

content = namespace.renderer.render(nested, namespace)

namespace.add_content(key, content)

return content

return render

return go

def inheritor(namespace, name):

def go():

def render(nested):

namespace.renderer.render(nested, namespace)

return namespace.renderer.render_name(name, namespace)

return render

return go

So here's some templates. base.mustache:

{{#$header}}

default header

{{/$header}}

{{#$body}}

default body

{{/$body}}

{{#$footer}}

default footer, using {{local key}}

{{/$footer}}

hello.mustache:

{{#

{{#$header}}

new header

{{/$header}}

{{#$body}}

new body, with {{local key}}

{{/$body}}

{{/

and then to play with three levels deep, subhello.mustache:

{{#

{{#$footer}}

im some new footer

{{/$footer}}

{{/

Rendering hello.mustache like this:

renderer = pystache.Renderer(search_dirs=["./templates/"])

print renderer.render_name("hello",

NameSpace(renderer, {"local key": "some local key"}))

output:

new header

new body, with some local key

default footer, using some local key

Rendering subhello.mustache:

print renderer.render_name("subhello",

NameSpace(renderer, {"local key": "some local key"}))

output:

new header

new body, with some local key

im some new footer

I just wrote this in twenty minutes, and I've only used handlebars.js a little bit in the past and pystache for the first time just now so the whole "mustache" idea is not deep for me yet. But this seems to work ?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值