QWebJavaScript评估上下文

阅读(7) 最后更新: 2019年1月27日 02:38

上一页 QWeb模板语言 | 字符串替换的动态属性-t-ATTF 下一页

翻译进度 100%

自助翻译

许多QWeb指令使用求值的表达式来产生某些结果。
Many of the QWeb directives use expressions that are evaluated to produce some result.

当从客户端使用时,就像看板视图一样,这些表达式是用JavaScript编写的,它们是在一个有几个有用变量可用的上下文中计算的。
When used from the client side, as is the case for Kanban views, these expressions are written in JavaScript.They're evaluated in a context that has a few useful variables available.

使用从服务器请求的字段,可以使用记录对象(表示当前记录),可以使用RAW_VALUE或VALUE属性访问字段值:RAW_VALUE是read()服务器方法返回的值,因此更适合在条件表达式中使用。
A record object is available, representing the current record, with the fields requested from the server.The field values can be accessed using either the raw_value or value attributes: raw_value is the value returned by the read() server method, so it's more suitable for use in condition expressions.

值是根据用户设置格式化的,用于在用户界面中显示。这通常与日期/日期时间、浮点/货币和关系字段相关。
value is formatted according to the user settings and is meant to be used for display in the user interface.This is typically relevant for date/datetime, float/monetary, and relational fields.

QWeb评估上下文也有用于JavaScript Web客户端实例的引用。要使用这些引用,需要对Web客户端架构有很好的理解,但我们不能详细介绍。出于参考目的,QWeb表达式评估中可以使用以下标识符:小部件是对当前KanbanRecord()小部件对象的引用,负责将当前记录呈现为看板卡,它公开了一些我们可以使用的辅助函数。
The QWeb evaluation context also has references available for the JavaScript web client instance.To make use of them, a good understanding of the web client architecture is needed, but we won't be able to go into that in detail.For reference purposes, the following identifiers are available in QWeb expression evaluation: widget is a reference to the current KanbanRecord() widget object, responsible for the rendering of the current record into a Kanban card.It exposes some helper functions we can use.

Record是Widget.Record的快捷方式,并使用点符号提供对可用字段的访问。
record is a shortcut for widget.record and provides access to the fields available, using dot notation.

READ_ONY_MODE表示当前视图是否处于读模式(而不是编辑模式),它是Widget.view.options.read_ONY_MODE的快捷方式。
read_only_mode indicates whether the current view is in read mode (and not in edit mode).It's a shortcut for widget.view.options.read_only_mode.

实例是对整个Web客户端实例的引用。
instance is a reference to the full web client instance.

同样值得注意的是,一些字符在表达式中是不允许的。小写符号(有特殊的意义,不应该在XML内容上使用)。否定的>=操作符是一个有效的选择,但通常的做法是使用以下可用于不平等操作的替代符号:它用于小于。
It's also noteworthy that some characters are not allowed inside expressions.The lower than sign ( have special meaning and shouldn't be used on the XML content.A negated >= operator is a valid alternative, but the common practice is to use the following alternative symbols that are available for inequality operations: lt is for less than.

LTE小于或等于。
lte is for less than or equal to.

GT代表大于。
gt is for greater than.

GTE大于或等于。
gte is for greater than or equal to.

前面的比较符号是Odoo特有的,是为了克服XML格式的限制而引入的,它们不是XML标准的一部分。
The preceding comparison symbols are specific to Odoo and were introduced to overcome limitations in the XML format.They are not part of the XML standard.

动态属性通过字符串替换-t-ATTF我们的看板卡使用t-ATTF QWeb指令在顶部元素上动态设置一个类,以便根据颜色字段值对卡片进行着色。为此,使用了t-ATTF-QWeb指令。
Dynamic attributes by string substitution – t-attf Our Kanban card is using the t-attf QWeb directive to dynamically set a class on the top element so that the card is colored depending on the color field value.For this, the t-attf- QWeb directive was used.

t-ATTF指令使用字符串替换动态生成标记属性,这允许动态生成较大字符串的部分,例如URL地址或CSS类名。
The t-attf- directive dynamically generates tag attributes using string substitution.This allows for parts of larger strings generated dynamically, such as a URL address or CSS class names.

该指令查找将由结果计算和替换的表达式块。
The directive looks for expression blocks that will be evaluated and replaced by the results.

它们由{和}或#{和}分隔。块的内容可以是任何有效的JavaScript表达式,并且可以使用QWeb表达式可用的任何变量,例如记录和小部件。
These are delimited either by {{ and }} or by #{ and }.The content of the blocks can be any valid JavaScript expression and can use any of the variables available for QWeb expressions, such as record and widget.

在我们的示例中,我们还使用了kanban_color()JavaScript函数,该函数是专门为将颜色索引号映射到CSS类颜色名称而提供的。
In our case, we also used the kanban_color() JavaScript function, specially provided to map color index numbers into the CSS class color names.

作为一个详细的例子,我们将使用此指令动态地更改用户的颜色,如果优先级较高,则为红色字体。为此,请将看板卡中的颜色替换为以下内容:这将导致类=“OE_kanban_text_red”或“class=”OE_kanban_text_Black“,请注意,虽然看板视图中有OE_kanban_text_red CSS类,但OE_kanban_text_Black CSS类不存在,并用于解释这一点。
As an elaborate example, we'll use this directive to dynamically change the color of the user, to be in red font if the priority is high.For this, replace in our Kanban card with the following: This results in either class="oe_kanban_text_red" or class="oe_kanban_text_black", depending on the checkout's priority value.Please note that, while the oe_kanban_text_red CSS class is available in Kanban views, the oe_kanban_text_black CSS class does not exist and was used to explain the point.

注意JavaScript表达式中使用的lt符号,它是<符号的一个转义表达式,在XML中是不允许的。
Notice the lt symbol used in the JavaScript expression.It's an escape expression for the < sign, not allowed in XML.

通过表达式动态属性-t-att-qweb指令通过计算表达式动态生成属性值。
Dynamic attributes by expressions – t-att The t-att- QWeb directive dynamically generates an attribute value by evaluating an expression.

我们的看板卡使用它来动态地设置标签上的一些属性;标题属性是使用以下内容动态呈现的:t-att-title=“记录器.成员_id.value”.value字段返回其值表示形式,因为它应该显示在屏幕上。对于多对一字段,这通常是相关记录的名称值。对于用户来说,这是username.so,当鼠标指针悬停在图像上时,您将看到相应的用户名。
Our Kanban card uses it to dynamically set some attributes on the tag; the title attribute is dynamically rendered using the following: t-att-title="record.member_id.value" The .value field returns its value representation as it should be shown on the screen.For many-to-one fields, this is usually the related record's name value.For users, this is the username.As a result, when hovering the mouse pointer over the image, you will see the corresponding username.

当表达式的计算值为假等效值时,属性根本不会呈现,这对于特殊的HTML属性(例如选中的输入字段)很重要,即使没有属性值,也可能产生效果。
When the expression evaluates to a false equivalent value, the attribute is not rendered at all.This is important for special HTML attributes such as the checked input field, which can have an effect even without an attribute value.

循环-t-foreach一个HTML块可以通过迭代循环来重复,我们可以使用它来添加记录追随者的化身。
Loops – t-foreach A block of HTML can be repeated by iterating through a loop.We can use it to add the avatars of the record followers.

让我们从只呈现记录的合作伙伴ID开始,如下所示:t-foreach指令接受对集合进行迭代的JavaScript表达式,在大多数情况下,这只是一个to-多关系字段的名称,它与t-as指令一起使用,用于设置用于引用迭代中的每个项的名称。
Let's start by rendering just the partner IDs of the record, as follows: ; The t-foreach directive accepts a JavaScript expression evaluating to a collection to iterate.In most cases, this will be just the name of a to-many relation field.It's used with a t-as directive to set the name to be used to refer to each item in the iteration.

接下来使用的t-ESC指令计算提供的表达式,在本例中只计算rec变量名,并将其呈现为安全转义的HTML。
The t-esc directive used next evaluates the provided expression, just the rec variable name in this case, and renders it as safely escaped HTML.

在前面的示例中,我们循环遍历存储在Message_Parter_ID字段中的关注者,由于看板卡上的空间有限,我们可以使用Slice()JavaScript函数来限制显示的关注者数量,如下所示:3)“rec变量保存每个迭代值,在这种情况下保存一个合作伙伴ID,这样,我们可以重写跟随者循环如下:t-as=”rec“>class=”OE_avatar“宽度=”24“高度=”24“/>例如,这可以添加到负责的用户图像旁边,在右手页脚中。
In the previous example, we loop through the followers stored in the message_parter_ids field.Since there is limited space on the Kanban card, we could have used the slice() JavaScript function to limit the number of followers to display, as shown in the following: t-foreach="record.message_partner_ids.raw_value.slice(0, 3)" The rec variable holds each iteration value, a partner ID in this case.With this, we can rewrite the follower loop as follows: t-as="rec"> class="oe_avatar" width="24" height="24" /> For example, this could be added next to the responsible user image, in the right-hand footer.

一些辅助变量也是可用的,它们的名称以t-定义为前缀,我们使用rec,所以可用的辅助变量如下:rec_index是迭代索引,从零开始,rec_size是集合的元素数,在迭代的第一个元素上,rec_first是真的,在迭代的最后一个元素上,rec_偶数是真,在奇数索引上,rec_odd是真的,根据当前索引rec_all表示在rec_value上迭代的对象,当遍历{key:value}字典时,保存值(rec持有键名),例如,我们可以利用以下方法避免ID列表中的后缀逗号:t-as=“rec”>;条件-t-如果我们的看板视图使用卡片选项菜单中的t-if指令使某些选项可用,则取决于某些条件。t-if指令期望在客户端呈现看板视图时用JavaScript计算表达式。标记及其内容只有在条件计算为真时才会呈现。
A few helper variables are also available.Their name has the variable name defined in t- as as a prefix.In our example, we used rec, so the helper variables available are as follows: rec_index is the iteration index, starting from zero rec_size is the number of elements of the collection rec_first is true on the first element of the iteration rec_last is true on the last element of the iteration rec_even is true on even indexes rec_odd is true on odd indexes rec_parity is either odd or even, depending on the current index rec_all represents the object being iterated over rec_value, when iterating through a {key:value} dictionary, holds the value (rec holds the key name) For example, we could make use of the following to avoid a trailing comma on our ID list: t-as="rec"> ; Conditionals – t-if Our Kanban view used the t-if directive in the card option menu to make some options available depending on some conditions.The t-if directive expects an expression to be evaluated in JavaScript when rendering Kanban views on the client side.The tag and its content will be rendered only if the condition evaluates to true.

例如,如果签出有一个值,则只显示它借来的图书数,在Request_Date字段之后添加以下内容:BooksWe使用了一个元素,这样如果条件为false,则元素不会产生输出。如果为true,则只将包含的元素呈现到输出中。
As an example, to only display the checkout's number of books borrowed if it has a value, add the following after the request_date field: books We used a element so that if the condition is false, the element produces no output.If it's true, only the contained element is rendered to the output.

注意,条件表达式使用GT符号而不是>来表示大于运算符。
Notice that the condition expression used the gt symbol instead of > to represent the greater than operator.

还有一个例子说明了它们的用法:没有书。
The else if and else conditions are also supported with the t-elif and t-else directives.Here is an example of their usage: No books.

很多书!书。
A lot of books!books.

在Javascript表达式中,and和OR运算符是&and
In Javascript expressions, the AND and OR operators are && and

但是在XML中不允许符号,我们可以使用AND或操作符来解决这个问题。
.But the ampersand symbol is not allowed in XML.We can work around this using the and and or operators.

呈现值-t-ESC和t-RAW我们使用元素来呈现字段内容,但是字段值也可以直接显示,而不需要标记。
Rendering values – t-esc and t-raw We used the element to render the field content.But field values can also be presented directly without a tag.

t-ESC指令计算一个表达式并将其呈现为HTML转义值,如下所示:在某些情况下,如果源数据被保证是安全的,则可以使用t-RAW来呈现字段原始值而不进行任何转义,如以下示例所示:避免尽可能多地使用t-RAW是很重要的,它的使用应该严格保留在输出HTML数据时,这些HTML数据是在没有任何用户数据的情况下专门准备的,或者任何用户数据都是为HTML特殊字符显式转义的。
The t-esc directive evaluates an expression and renders it as an HTML-escaped value, as shown in the following: In some cases, and if the source data is guaranteed to be safe, t-raw can be used to render the field raw value without any escaping, as shown in the following example: For security reasons, it's important to avoid using t-raw as much as possible.Its usage should be strictly reserved for outputting HTML data that was specifically prepared without any user data in it or where any user data was escaped explicitly for HTML special characters.

为更复杂的逻辑设置变量-t-set的值,我们可以将表达式的结果存储到一个变量中,以便稍后在模板中使用它,这是使用t-set指令完成的,将变量命名为SET,然后是t-value指令,表达式计算要赋值的值。
Set values on variables – t-set For more complex logic, we can store the result of an expression into a variable to use it later in the template.This is to be done using the t-set directive, naming the variable to set followed by the t-value directive, with the expression calculating the value to assign.

例如,以下代码以红色呈现错过的截止日期,与上一节一样,但使用red_or_Black变量供CSS类使用,如下所示:t-value=“Recor.Priority.RAW_VALUE GTE‘2”?’OE_kanban_text_red‘:’“/>变量也可以分配给HTML内容,如下面的示例所示:调用和重用其他模板-t调用QWeb模板可以是可重用的HTML片段,可以插入到其他模板中。
As an example, the following code renders missed deadlines in red, just as in the previous section, but uses a red_or_black variable for the CSS class to use, as shown in the following: t-value=" record.priority.raw_value gte '2' ?'oe_kanban_text_red' : ''" /> Variables can also be assigned HTML content, as in the following example: Call and reuse other templates – t-call QWeb templates can be reusable HTML snippets that can be inserted into other templates.

与其一次又一次地重复相同的HTML块,我们可以设计构建块来组成更复杂的用户界面视图。
Instead of repeating the same HTML blocks over and over again, we can design building blocks to compose more complex user interface views.

可重用模板在标签中定义,并由顶部元素标识,该元素的t名不是kanban-box,然后可以使用t-调用方向包括这些其他模板,这对于在相同的看板视图中声明的模板、在相同的外接模块中的其他地方或在不同的加载项中的模板都是如此。
Reusable templates are defined inside the tag and identified by a top element with a t-name other than kanban-box.These other templates can then be included using the t-call directive.This is true for the templates declared in the same Kanban view, somewhere else in the same add-on module or in a different add-on.

跟随者化身列表可以在一个可重用的片段中被隔离。让我们将它重新工作以使用一个子模板。我们应该首先在元素中的节点之后,添加另一个模板到我们的XML文件中,如下所示:t-as=“rec”>class=“OE_avatar”宽度=“24”高度=“24”/>直接从kanban-box主模板调用它。我们需要像对其他视图一样使用module.name完整标识符,例如,可以使用Library_checkout.关注者_avatars完整标识符来引用这个片段。
The follower avatar list is something that could be isolated in a reusable snippet.Let's rework it to use a sub-template.We should start by adding another template to our XML file, inside the element, after the node, as shown in the following: t-as="rec"> class="oe_avatar" width="24" height="24" /> Calling it from the kanban-box main template is quite straightforward.Instead of the element containing the for each directive, we should use the following: To call templates defined in other add-on modules, we need to use the module.name full identifier, as we do with the other views.For instance, this snippet can be referred using the library_checkout.follower_avatars full identifier.

被调用的模板在与调用方相同的上下文中运行,因此在处理被调用的模板时,调用方中可用的任何变量名称也是可用的。
The called template runs in the same context as the caller, so any variable names available in the caller are also available when processing the called template.

一个更优雅的选择是将参数传递给被调用的模板,这是通过在t-调用标签中设置变量来实现的。这些变量将只在子模板上下文中被评估和可用,而在调用者上下文中不存在。
A more elegant alternative is to pass arguments to the called template.This is done by setting variables inside the t-call tag.These will be evaluated and made available in the sub-template context only and won't exist in the caller context.

首先,我们需要将固定值3替换为变量arg_max,例如:t-as=“rec”>class=“oe_avatar”宽度=“24”高度=“24”/>,在执行子模板调用时定义该变量的值如下:t调用元素中的整个内容也可通过0魔术变量对子模板可用。我们可以定义一个HTML代码片段,它可以在子模板中使用,这对于以模块化的方式构建布局和组合/嵌套QWeb模板特别有用。
We could use this to have the maximum number of follower avatars set by the caller instead of being hardcoded in the sub-template.First, we need to replace the fixed value, 3, with a variable, arg_max, example: t-as="rec"> class="oe_avatar" width="24" height="24" /> Then, define that variable's value when performing the sub-template call as follows: The entire content inside the t-call element is also available to the sub-template through the 0 magic variable.Instead of argument variables, we can define an HTML code fragment that can be used in the sub-template with .This is especially useful for building layouts and combining/nesting QWeb templates in a modular way.

odoo技术交流2 群号 625337975

http://www.odoogo.com/post/58/Odoo开发视频教程

文档不断更新中,添加微信odoogo,获取最新视频课程和模块

留言提问