为什么django模板只使用python代码?

时间:2022-09-20 13:45:16

I mean I understand that these templates are aimed at designers and other less code-savvy people, but for developers I feel the template language is just a hassle. I need to re-learn how to do very simple things like iterate through dictionaries or lists that I pass into the template, and it doesn't even seem to work very well. I'm still having trouble getting the whole "dot" notation working as I would expect (for example, {{mydict.dictkey}} inside a for loop doesn't work :S -- I might ask this as a separate question), and I don't see why it wouldn't be possible to just use python code in a template system. In particular, I feel that if templates are meant to be simple, then the level of python code that would need to be employed in these templates would be of a caliber not more complicated than the current templating language. So these designer peeps wouldn't have more trouble learning that much python than they would learning the Django template language (and there's more places you can go with this knowledge of basic python as opposed to DTL) And the added advantage would be that people who already know python would be in familiar territory with all the usual syntax and power available to them and can just get going.

我的意思是我理解这些模板是针对设计师和其他不那么精通代码的人,但对于开发人员,我觉得模板语言只是一个麻烦。我需要重新学习如何做非常简单的事情,比如迭代我传递给模板的字典或列表,它甚至看起来都不是很好。我仍然无法像我期望的那样使整个“点”符号工作(例如,for {循环中的{{mydict.dictkey}}不起作用:S - 我可能会将此作为一个单独的问题) ,我不明白为什么在模板系统中只使用python代码是不可能的。特别是,我觉得如果模板很简单,那么需要在这些模板中使用的python代码级别并不比当前的模板语言复杂。所以这些设计师偷看学习那么多python比学习Django模板语言要困难得多(并且你可以使用基本python而不是DTL的知识更多的地方)并且附加的优势将是那些人已经知道python将处于熟悉的领域,具有所有可用的语法和功能,并且可以开始。

Am I missing something? If so I plead django noob and would love for you to enlighten me on the many merits of the current system. But otherwise, any recommendations on other template systems that may be more what I'm looking for?

我错过了什么吗?如果是这样的话,我会恳求django noob,并希望你能够启发我当前系统的许多优点。但是否则,对其他模板系统的任何推荐可能更像我正在寻找的东西?

4 个解决方案

#1


10  

The reason that most people give for limited template languages is that they don't want to mix the business logic of their application with its presentation (that wouldn't work well with the MVC philosophy; using Django I'm sure you understand the benefits of this).

大多数人为有限的模板语言提供的原因是他们不希望将他们的应用程序的业务逻辑与其表示混合(这与MVC哲学不兼容;使用Django我确信你理解它的好处这个的)。

Daniel Greenfeld wrote an article a few days ago explaining why he likes "stupid template languages", and many people wrote responses (see the past few days on Planet Python). If you read what Daniel wrote and how others responded to it, you'll get an idea of some of the arguments for and against allowing template languages to use Python.

丹尼尔格林菲尔德几天前写了一篇文章,解释了为什么他喜欢“愚蠢的模板语言”,很多人写了回复(参见过去几天关于Planet Python的文章)。如果您阅读了Daniel写的内容以及其他人如何回应它,您将会了解允许模板语言使用Python的一些论据。

#2


2  

Don't forget that you aren't limited to Django's template language. You're free to use whatever templating system you like in your view functions. However you want to create the HTML to return from your view function is fine. There are many templating implementations in the Python world: choose one that suits you better, and use it.

不要忘记您不仅限于Django的模板语言。您可以在视图函数中*使用您喜欢的任何模板系统。但是,您想要创建从视图函数返回的HTML很好。 Python世界中有许多模板实现:选择一个更适合你的实现,并使用它。

#3


1  

Seperation of concerns.

分离关注点。

Designer does design. Developer does development. Templates are written by the designers.

设计师确实设计。开发人员做开发。模板由设计师编写。

Design and development are independent and different areas of work typically handled by different people.

设计和开发是独立的,不同的工作领域通常由不同的人处理。

I guess having template code in python would work very well if one is a developer and their spouse is a designer. Otherwise, let each do his job, with least interference.

我想如果一个开发人员和他们的配偶是设计师,在python中使用模板代码会很好。否则,让每个人尽职尽责,干扰最少。

#4


0  

Django templates don’t just use Python code for the same reason Django uses the MVC paradigm:
    No particular reason.
    (ie: The same reason anyone utilizes MVC at all, and that reason is just that certain people prefer this rigid philosophical pattern.)

Django模板不仅仅使用Python代码,原因与Django使用MVC范例相同:没有特别的原因。 (即:任何人都使用MVC的原因相同,原因只是某些人更喜欢这种严格的哲学模式。)

In general I’d suggest you avoid Django if you don’t like things like this, because the Django people won’t be changing this approach. You could also, however, do something silly (in that it’d be contradictory to the philosophy of the chosen software), like put all the markup and anything else you can into the "view" files, turning Django from its "MVC" (or "MTV" ) paradigm into roughly what everything else is (a boring but straightforward lump).

一般来说,如果你不喜欢这样的话,我建议你避免使用Django,因为Django人不会改变这种方法。然而,您也可以做一些愚蠢的事情(因为它与所选软件的哲学相矛盾),比如将所有标记和其他任何内容放入“视图”文件中,将Django从其“MVC”中转移出来(或“MTV”)范式大致是其他所有东西(一个无聊但直截了当的疙瘩)。

#1


10  

The reason that most people give for limited template languages is that they don't want to mix the business logic of their application with its presentation (that wouldn't work well with the MVC philosophy; using Django I'm sure you understand the benefits of this).

大多数人为有限的模板语言提供的原因是他们不希望将他们的应用程序的业务逻辑与其表示混合(这与MVC哲学不兼容;使用Django我确信你理解它的好处这个的)。

Daniel Greenfeld wrote an article a few days ago explaining why he likes "stupid template languages", and many people wrote responses (see the past few days on Planet Python). If you read what Daniel wrote and how others responded to it, you'll get an idea of some of the arguments for and against allowing template languages to use Python.

丹尼尔格林菲尔德几天前写了一篇文章,解释了为什么他喜欢“愚蠢的模板语言”,很多人写了回复(参见过去几天关于Planet Python的文章)。如果您阅读了Daniel写的内容以及其他人如何回应它,您将会了解允许模板语言使用Python的一些论据。

#2


2  

Don't forget that you aren't limited to Django's template language. You're free to use whatever templating system you like in your view functions. However you want to create the HTML to return from your view function is fine. There are many templating implementations in the Python world: choose one that suits you better, and use it.

不要忘记您不仅限于Django的模板语言。您可以在视图函数中*使用您喜欢的任何模板系统。但是,您想要创建从视图函数返回的HTML很好。 Python世界中有许多模板实现:选择一个更适合你的实现,并使用它。

#3


1  

Seperation of concerns.

分离关注点。

Designer does design. Developer does development. Templates are written by the designers.

设计师确实设计。开发人员做开发。模板由设计师编写。

Design and development are independent and different areas of work typically handled by different people.

设计和开发是独立的,不同的工作领域通常由不同的人处理。

I guess having template code in python would work very well if one is a developer and their spouse is a designer. Otherwise, let each do his job, with least interference.

我想如果一个开发人员和他们的配偶是设计师,在python中使用模板代码会很好。否则,让每个人尽职尽责,干扰最少。

#4


0  

Django templates don’t just use Python code for the same reason Django uses the MVC paradigm:
    No particular reason.
    (ie: The same reason anyone utilizes MVC at all, and that reason is just that certain people prefer this rigid philosophical pattern.)

Django模板不仅仅使用Python代码,原因与Django使用MVC范例相同:没有特别的原因。 (即:任何人都使用MVC的原因相同,原因只是某些人更喜欢这种严格的哲学模式。)

In general I’d suggest you avoid Django if you don’t like things like this, because the Django people won’t be changing this approach. You could also, however, do something silly (in that it’d be contradictory to the philosophy of the chosen software), like put all the markup and anything else you can into the "view" files, turning Django from its "MVC" (or "MTV" ) paradigm into roughly what everything else is (a boring but straightforward lump).

一般来说,如果你不喜欢这样的话,我建议你避免使用Django,因为Django人不会改变这种方法。然而,您也可以做一些愚蠢的事情(因为它与所选软件的哲学相矛盾),比如将所有标记和其他任何内容放入“视图”文件中,将Django从其“MVC”中转移出来(或“MTV”)范式大致是其他所有东西(一个无聊但直截了当的疙瘩)。