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

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

翻译进度 100%


record is a shortcut for widget.record and provides access to the fields available, using dot notation.
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.
A lot of books!books.
Notice the lt symbol used in the JavaScript expression.It's an escape expression for the < sign, not allowed in XML.
Dynamic attributes by expressions – t-att The t-att- QWeb directive dynamically generates an attribute value by evaluating an expression.
Many of the QWeb directives use expressions that are evaluated to produce some result.
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.
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.
instance is a reference to the full web client instance.
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 is for less than or equal to.
gt is for greater than.
gte is for greater than or equal to.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
In Javascript expressions, the AND and OR operators are && and
.But the ampersand symbol is not allowed in XML.We can work around this using the and and or operators.
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.
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.
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.
Instead of repeating the same HTML blocks over and over again, we can design building blocks to compose more complex user interface views.
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.
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.
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.
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