124124
125125```
126126
127- The ` compile ` will compile the _ source_ into a code object. We can execute it
128- later with an ` exec ` statement. Now let's build the ` parse_template ` function,
129- firstly we need to parse our template string into a list of nodes that knows
130- how to generate Python code, we need a function called ` _parse ` , we will see
131- the function later, we need some helpers now, to help with reading through the
132- template file, we have the ` _TemplateReader ` class, which handles the reading
133- for us as we consume the template file. We need to start from the begining and
134- keep going ahead to find some special notations, the ` _TemplateReader ` will
135- keep the current position and give us ways to do it:
127+ ` compile ` 会将_source_编译成一个code对象。稍后,我们可以使用一个` exec ` 语句来执行它。现在,构建` parse_template ` 函数,首先,我们需要将模板字符串解析成节点(node)列表,它清楚如何生成Python代码,我们需要一个名为` _parse ` 的函数,稍后我们将看到这个函数,我们现在需要一些辅助器,用以读取整个模板文件,我们有` _TemplateReader ` 类,在我们处理模板文件的时候,它为我们处理读取。我们需要从头开始,找到一些特殊的标记,` _TemplateReader ` 将会记录当前位置,并为我们提供执行方法:
136128
137129``` python
138130
@@ -178,9 +170,7 @@ keep the current position and give us ways to do it:
178170
179171```
180172
181- To help with generating the Python code, we need the ` _CodeWriter ` class, this
182- class writes lines of codes and manages indentation, also it is one Python
183- context manager:
173+ 为了生成Python代码,我们需要` _CodeWriter ` 类,这个类编写代码行,并管理缩进,另外,它还是一个Python上下文管理器:
184174
185175``` python
186176
@@ -227,12 +217,7 @@ context manager:
227217
228218```
229219
230- Then we pass the reader to the ` _parse ` function and produces a list of nodes.
231- All of there nodes are the child nodes of the template file node. We create
232- one CodeWriter object, the file node writes Python code into the CodeWriter,
233- and we return the generated Python code. The ` _Node ` class would handle the
234- Python code generation for a specific case, we will see it later. Now let's go
235- back to our ` _parse ` function:
220+ 然后,我们将读取器传递给` _parse ` 函数,并生成节点列表。所有这些节点都是模板文件节点的子节点。我们创建一个CodeWriter对象,文件节点将Python代码写入到CodeWriter中,然后返回生成的Python代码。` _Node ` 类将会处理特殊情况下的Python代码生成,稍后我们会看到。现在,回到` _parse ` 函数:
236221
237222``` python
238223
@@ -266,9 +251,7 @@ back to our `_parse` function:
266251
267252```
268253
269- We loop forever to find a template directive in the remaining file, if we
270- reach the end of the file, we append the text node and exit, otherwise, we
271- have found one template directive.
254+ 进入无限循环以查找剩余文件中的模板指令,如果抵达文件末端,则附加文本节点并退出,否则,说明找到了一个模板指令。
272255
273256``` python
274257
@@ -334,11 +317,9 @@ have found one template directive.
334317
335318```
336319
337- We have a block here, normally we would get the block body recursively and
338- append a ` _ControlBlock ` node, the block body should be a list of nodes. If we
339- encounter an ` {% end %} ` , the block ends and we exit the function.
320+ 这里,有一个块,通常,我们会递归获得块体,并附加一个` _ControlBlock ` 节点,而块体应该是一个节点列表。如果遇到一个` {% end %} ` ,则说明块结束了,退出该函数。
340321
341- It is time to find out the secrets of ` _Node ` class, it is quite simple:
322+ 是时候找出 ` _Node ` 类的秘密了,它非常简单:
342323
343324``` python
344325
@@ -401,8 +382,7 @@ It is time to find out the secrets of `_Node` class, it is quite simple:
401382
402383```
403384
404- The ` _Text ` and ` _Expression ` node are also really simple, just append what
405- you get from the template source.
385+ ` _Text ` 和` _Expression ` 节点也相当简单,只是附加从模板源获得的东西。
406386
407387``` python
408388
@@ -418,12 +398,9 @@ you get from the template source.
418398
419399```
420400
421- For a ` _ControlBlock ` node, we need to indent and write our child node list
422- with the indentation.
401+ 对于一个` _ControlBlock ` 节点,我们需要缩进并带缩进编写子节点列表。
423402
424- Now let's get back to the rendering part, we render a context by using the
425- ` generate ` method of ` Template ` object, the ` generate ` function just call the
426- compiled Python code:
403+ 现在,让我们回到渲染部分,通过使用` Template ` 对象的` generate ` 方法,我们渲染一个上下文,` generate ` 方法仅仅是调用编译好了的Python代码:
427404
428405``` python
429406
0 commit comments