Lift 中的 Django 标签和过滤器?
这是我的上一个关于复数过滤器的问题的概括:
电梯是否有相当于 Django 的标签和过滤器?
标签是一小段预定义的代码,可以直接在html模板中使用,例如:
{% now "jS F Y H:i" %}
以给定的格式呈现现在的时间。
过滤器
过滤器(在 html 模板中)对模板中的上下文变量进行操作,例如:
{{ value|capfirst }}
如果调用值“john”将得到“John”。或者:
{{ value|length }}
将字符串的长度渲染到模板中。
正如您所看到的,过滤器对传递给模板的上下文变量进行操作。
This is a generalization of my previous question about pluralize filter:
Does lift have an equivalent of Django's tags and filters?
Tags are small piece of predefined code that can be used directly in html template, for example:
{% now "jS F Y H:i" %}
renders the time right now in the given format.
Filters
Filters operate (in html template) on the context variables in the template, for example:
{{ value|capfirst }}
if called on a value "john" will result in "John". Or:
{{ value|length }}
will render the length of the string into the template.
As you can see the filters operate on the context variables that are passed to the template.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
考虑到标签,您可以使用片段自己定义它们。
因为代码片段基本上是一个回调,就像 Django 标签一样。不过,您不会获得任何更简单的语法,因为 Lift 的模板是纯 XML/Html。
在过滤中定义的逻辑
通常无法在 Lift 模板中执行,因为 Lift 不允许模板中存在任何变量。因此整个概念是不适用的。 (您可以进行 XML 转换或绑定魔法,但这对于简单的
value.length
来说有点太多了。)不,如果您需要某个值的长度在你的 Html 中,你必须在代码片段中定义它并公开它。
如果您确实无法在模板中没有过滤器(尽管我可以向您保证,将所有 HTML 模板和代码分开是一件好事,并且一旦您习惯了它就可以工作),请查看 Scalate 也可以与 Lift 一起使用。
Considering tags, you could define those yourself with snippets.
As snippet is basically a callback much as a Django tag is. You don’t get any easier syntax, though, because Lift’s templates are pure XML/Html.
And the logic would be defined in
Filtering is something you generally can’t do in a Lift template because Lift doesn’t allow any variables in a template. The whole concept is thus inapplicable. (You could do XML transforms or or bind magic but that would be a bit too much for a simple
value.length
.)No, if you need the length of some value in your Html, you will have to define that inside the snippet and expose it.
If you really can’t live without filters in your template (though I can assure you, it is a good thing to separate all HTML template and code and it works once you are used to it), have a look at Scalate which may be used together with Lift as well.
这种逻辑应该位于代码片段的 render 方法中。在测试、IDE 和重构工具方面,以纯 Scala(而不是模板语言混合)显示代码是一等公民。
这些类型的转换不是内置的,但您可以使用隐式添加它们:
然后在绑定您生成的任何值之前在
render
方法中调用这些转换。This kind of logic should be in the
render
method of a snippet. Display code in pure Scala (rather than in a template language hybrid) is a first-class citizen with respect to testing, IDE's and refactoring tools.These kinds of transforms don't come built-in, but you can add them with implicits:
Then call these in your
render
method before binding whatever value it is you're generating.