最好的Python模板库,方便代码生成

时间:2022-10-20 18:59:52

Instead of me spending the next day (or year) reading about them all, are there any suggestions for templating engines that I should look into in more detail?

而不是我花费第二天(或一年)阅读所有这些,是否有任何关于模板引擎的建议,我应该更详细地研究一下?

7 个解决方案

#1


28  

Best suggestion: try them all. It won't take long.

最佳建议:全部尝试。不会花很长时间。

My favourite: Jinja2 (by a mile)

我最喜欢的:Jinja2(一英里)

It has decent syntax, can trace errors through it, and is sandboxable.

它具有良好的语法,可以通过它跟踪错误,并且是沙箱。

#2


14  

If you're doing code generation, you might find Cog useful - it's specifically for code generation, rather than being a generally applicable templating language.

如果您正在进行代码生成,您可能会发现Cog很有用 - 它专门用于代码生成,而不是通常适用的模板语言。

#3


9  

The most important concern is whether you can live with the syntax the templates require. Second and third (depending on your application needs) would be speed and ease of distribution.

最重要的问题是您是否可以使用模板所需的语法。第二和第三(取决于您的应用需求)将是速度和易于分发。

I looked at all of them, but the only syntax I could stand was Jinja. Jinja has the advantage of supporting a lot of Python constructs, so it's very easy to add snippets of functionality to the templates as needed, without coding special tags. Most of what requires tags in other template systems is handled by macros in Jinja.

我看着他们所有人,但我能忍受的唯一语法是Jinja。 Jinja具有支持许多Python构造的优势,因此可以根据需要向模板添加功能片段,而无需编写特殊标记。大多数需要其他模板系统中的标签的内容由Jinja中的宏处理。

Of course, if you're looking for something easy and quick, it's hard to beat the Python templating API in the core language.

当然,如果你正在寻找一些简单快捷的东西,那么在核心语言中很难击败Python模板API。

#4


2  

Update: Kid appears to have been succeeded by Genshi.

更新:Kid似乎已被Genshi取代。

I've used Kid, which is I think one of the older systems. I've found it to be extremely solid, stable and reliable. It's tag-based, so it's nice for working with XML/HTML. It's kind of interesting in that template functions are done as HTML attributes, not special blocks, i.e. {% ... %}. However, some aspects of that (especially the way it does 'includes') can get pretty irksome. It also doesn't seem to be developed actively or at all anymore.

我用过Kid,这是我认为较老的系统之一。我发现它非常坚固,稳定可靠。它是基于标记的,因此使用XML / HTML很好。有趣的是模板函数是作为HTML属性完成的,而不是特殊的块,即{%...%}。然而,这方面的某些方面(特别是它'包括'的方式)可能会变得非常令人厌烦。它似乎也没有积极发展或根本不发展。

It's worth taking a look at if you want something that has been around for a while and has become quite stable. If you want something more recent, I've heard good things about both Genshi and Jinja.

值得一看,如果你想要的东西已经存在了一段时间并且变得非常稳定。如果你想要更新的东西,我听说过关于Genshi和Jinja的好东西。

#5


1  

I like Clearsilver because it works with several different languages and it strictly enforces the separation between data and presentation. I previously used Cheetah and while it's pretty nice, I didn't like working with what sometimes seemed like a limited form of Python.

我喜欢Clearsilver,因为它适用于几种不同的语言,它严格执行数据和表示之间的分离。我之前使用的是Cheetah,虽然它非常好,但我不喜欢使用有时看似有限形式的Python的东西。

#6


1  

If you're working with X[HT]ML, one of the tag-based templating systems that can leave you with well-formed templates is a good move. I use PXTL, FWIW. (It can produce other formats, but if your emphasis isn't XML or HTML it'd not be a sensible choice.)

如果您正在使用X [HT] ML,那么基于标签的模板系统之一可以为您提供格式良好的模板,这是一个很好的举措。我使用PXTL,FWIW。 (它可以产生其他格式,但如果你的重点不是XML或HTML,它不是一个明智的选择。)

I have an intense dislike for templating systems that claim to “help you separate business logic and presentation” by limiting expressions to their own Little Language. They don't seem to understand that there is such as thing as “presentation logic”, and it can get sometimes complicated enough to need a Real Language like Python to run it. Having you kick out your presentation logic into the app with the business logic is so not a win. Avoid!

我对模板系统非常不喜欢,他们声称“通过将表达限制在他们自己的小语言中来帮助你分离业务逻辑和演示”。他们似乎并不理解存在诸如“表示逻辑”这样的东西,并且它有时会变得足够复杂,需要像Python这样的真实语言来运行它。让您使用业务逻辑将演示逻辑踢出到应用程序中并非如此。避免!

(The limited expression separate mini-language approach made some sense in JSP's ‘EL’, as Java is too annoyingly verbose to use in a templating library. But we've got Python! It's perfect for writing expressions in templates as it is; chopping functionality out and making the user learn another new language gains you nothing.)

(有限的表达式单独的迷你语言方法在JSP的'EL'中有一定的意义,因为Java在模板库中使用起来太烦人了。但是我们有Python!它非常适合在模板中编写表达式;切割功能出来并让用户学习另一种新语言不会让你获益。)

#7


1  

If you want a very lightweight option, try templete. It's only like 80 lines of code in single module. Have a look here and here (it was published in a blog). I think it is a clever and very focused solution, if the features are sufficient for you.

如果你想要一个非常轻量级的选项,请尝试使用模板。它只有单个模块中的80行代码。看看这里和这里(它发表在博客上)。如果功能足够你,我认为这是一个聪明且非常集中的解决方案。

#1


28  

Best suggestion: try them all. It won't take long.

最佳建议:全部尝试。不会花很长时间。

My favourite: Jinja2 (by a mile)

我最喜欢的:Jinja2(一英里)

It has decent syntax, can trace errors through it, and is sandboxable.

它具有良好的语法,可以通过它跟踪错误,并且是沙箱。

#2


14  

If you're doing code generation, you might find Cog useful - it's specifically for code generation, rather than being a generally applicable templating language.

如果您正在进行代码生成,您可能会发现Cog很有用 - 它专门用于代码生成,而不是通常适用的模板语言。

#3


9  

The most important concern is whether you can live with the syntax the templates require. Second and third (depending on your application needs) would be speed and ease of distribution.

最重要的问题是您是否可以使用模板所需的语法。第二和第三(取决于您的应用需求)将是速度和易于分发。

I looked at all of them, but the only syntax I could stand was Jinja. Jinja has the advantage of supporting a lot of Python constructs, so it's very easy to add snippets of functionality to the templates as needed, without coding special tags. Most of what requires tags in other template systems is handled by macros in Jinja.

我看着他们所有人,但我能忍受的唯一语法是Jinja。 Jinja具有支持许多Python构造的优势,因此可以根据需要向模板添加功能片段,而无需编写特殊标记。大多数需要其他模板系统中的标签的内容由Jinja中的宏处理。

Of course, if you're looking for something easy and quick, it's hard to beat the Python templating API in the core language.

当然,如果你正在寻找一些简单快捷的东西,那么在核心语言中很难击败Python模板API。

#4


2  

Update: Kid appears to have been succeeded by Genshi.

更新:Kid似乎已被Genshi取代。

I've used Kid, which is I think one of the older systems. I've found it to be extremely solid, stable and reliable. It's tag-based, so it's nice for working with XML/HTML. It's kind of interesting in that template functions are done as HTML attributes, not special blocks, i.e. {% ... %}. However, some aspects of that (especially the way it does 'includes') can get pretty irksome. It also doesn't seem to be developed actively or at all anymore.

我用过Kid,这是我认为较老的系统之一。我发现它非常坚固,稳定可靠。它是基于标记的,因此使用XML / HTML很好。有趣的是模板函数是作为HTML属性完成的,而不是特殊的块,即{%...%}。然而,这方面的某些方面(特别是它'包括'的方式)可能会变得非常令人厌烦。它似乎也没有积极发展或根本不发展。

It's worth taking a look at if you want something that has been around for a while and has become quite stable. If you want something more recent, I've heard good things about both Genshi and Jinja.

值得一看,如果你想要的东西已经存在了一段时间并且变得非常稳定。如果你想要更新的东西,我听说过关于Genshi和Jinja的好东西。

#5


1  

I like Clearsilver because it works with several different languages and it strictly enforces the separation between data and presentation. I previously used Cheetah and while it's pretty nice, I didn't like working with what sometimes seemed like a limited form of Python.

我喜欢Clearsilver,因为它适用于几种不同的语言,它严格执行数据和表示之间的分离。我之前使用的是Cheetah,虽然它非常好,但我不喜欢使用有时看似有限形式的Python的东西。

#6


1  

If you're working with X[HT]ML, one of the tag-based templating systems that can leave you with well-formed templates is a good move. I use PXTL, FWIW. (It can produce other formats, but if your emphasis isn't XML or HTML it'd not be a sensible choice.)

如果您正在使用X [HT] ML,那么基于标签的模板系统之一可以为您提供格式良好的模板,这是一个很好的举措。我使用PXTL,FWIW。 (它可以产生其他格式,但如果你的重点不是XML或HTML,它不是一个明智的选择。)

I have an intense dislike for templating systems that claim to “help you separate business logic and presentation” by limiting expressions to their own Little Language. They don't seem to understand that there is such as thing as “presentation logic”, and it can get sometimes complicated enough to need a Real Language like Python to run it. Having you kick out your presentation logic into the app with the business logic is so not a win. Avoid!

我对模板系统非常不喜欢,他们声称“通过将表达限制在他们自己的小语言中来帮助你分离业务逻辑和演示”。他们似乎并不理解存在诸如“表示逻辑”这样的东西,并且它有时会变得足够复杂,需要像Python这样的真实语言来运行它。让您使用业务逻辑将演示逻辑踢出到应用程序中并非如此。避免!

(The limited expression separate mini-language approach made some sense in JSP's ‘EL’, as Java is too annoyingly verbose to use in a templating library. But we've got Python! It's perfect for writing expressions in templates as it is; chopping functionality out and making the user learn another new language gains you nothing.)

(有限的表达式单独的迷你语言方法在JSP的'EL'中有一定的意义,因为Java在模板库中使用起来太烦人了。但是我们有Python!它非常适合在模板中编写表达式;切割功能出来并让用户学习另一种新语言不会让你获益。)

#7


1  

If you want a very lightweight option, try templete. It's only like 80 lines of code in single module. Have a look here and here (it was published in a blog). I think it is a clever and very focused solution, if the features are sufficient for you.

如果你想要一个非常轻量级的选项,请尝试使用模板。它只有单个模块中的80行代码。看看这里和这里(它发表在博客上)。如果功能足够你,我认为这是一个聪明且非常集中的解决方案。