简体   繁体   English

是否有可以注入方法调用的 Python 模板系统?

[英]Is there a Python templating system that can inject method calls?

Need a Python templating engine that can make method calls from a module, rather than get static dict values.需要一个可以从模块进行方法调用的 Python 模板引擎,而不是获取静态 dict 值。

I'm porting some code from Perl over to Python.我正在将一些代码从 Perl 移植到 Python。 In the original code,在原始代码中,
I passed a blessed hash object to Template Toolkit to format output.我将一个有福的哈希对象传递给模板工具包来格式化输出。
When you invoke a value like this in the template...当您在模板中调用这样的值时...

[% name %]                                                                  

...it would call the dynamic object method... ...它会调用动态对象方法...

$feature->name()                                                            

Can someone recommend a robust templating system that follows the same有人可以推荐一个强大的模板系统,它遵循相同
dev pattern?开发模式? Most appear to expect a dict with a pre-populated set of大多数人似乎期待一个带有预先填充的集合的字典
static values.静态值。 Instead, I want to pass the template an instantiated相反,我想将模板传递给实例化
module, using a pattern like this:模块,使用这样的模式:

ft = Feature()                                                              
...                                                                         
output = runSomeTemplatingEngine(templateString, ft)

在上述字典中,您可以轻松地传递一个对象并访问它的方法。

Try Qree (read "Curie").尝试Qree (阅读“居里”)。 It relies on exec() & eval() , and hence supports all language features.它依赖于exec()eval() ,因此支持所有语言功能。

In a Qree template, you can import modules, assign variables, call functions, etc. You can do everything.在 Qree 模板中,您可以导入模块、分配变量、调用函数等。您可以做任何事情。

Caution: As Qree uses eval() , it should NOT be used to render untrusted templates.注意:由于Qree使用eval() ,它应该被用来渲染不可信模板。

Note: I'm Qree's author.注意:我是 Qree 的作者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM