How to inherit a template without identifier in Odoo?

I am trying to show the change date in a task. To do this, I need to inherit the widget template "mail_thread". This template does not have an identifier in its definition. It's him:

<?xml version="1.0" encoding="UTF-8"?> <template> <!-- mail.Widget template used to namespace the css --> <t t-name="mail.Root"> <div class="oe_mail"> </div> </t> ... <span t-att-title="widget.date"> <t t-if="widget.timerelative" t-esc="widget.timerelative"/> <t t-if="!widget.timerelative" t-raw="widget.display_date"/> </span> ... </template> 

In my module, I need to replace the <span> tag to show the date.

So how to inherit this template and replace the tag?

+5
source share
2 answers

There are different inheritance mechanisms for client templates (web templates defined inside the <templates> , "compiled" with javascript in the client upon loading), and server templates (usually should be included in the data list in the __openerp__.py file, " compiled "when starting / updating odoo server).

You extend web widget template templates using <t t-extend="template_name"> followed by one or more <t t-jquery="jquery_selector" t-operation="operation"> , which acts like xpath, but client-side and more "powerful."
You do not need identifiers; inheritance is based on the name of the template. ( t-name )

Template inheritance is used to modify existing templates in place, for example, to add information to templates created by other modules.

Template inheritance is performed using the t-extend directive, which takes the name of the template to change as a parameter.

Then the change is performed with any number of t-jquery subdirectives:

<t t-extend="base.template"> <t t-jquery="ul" t-operation="append"> <li>new element</li> </t> </t>

T-jquery directives use a CSS selector. This selector uses an extended template to select the context nodes to which the t operation is indicated:

  • Append
    the node body is added at the end of the node context (after the context of node s last child)
  • before the name
    the node body is added to the node context (inserted before the first node s)
  • Before
    the node body is inserted right in front of the node context
  • after
    the node body is inserted immediately after the node context
  • interior
    node body replaces node s context contents
  • replace
    node body is used to replace node itsel context
  • No operation
    if no t-operation is specified, the body of the template is interpreted as javascript code and executed with a node context like this
+10
source

I also wanted to change the date display format in this XML file. So I copied the entire template for this default layout to my new module and only changed the date in the span tag.

 <?xml version="1.0" encoding="UTF-8"?> <template> <!-- default layout --> <t t-name="mail.thread.message"> .... <span t-att-title="widget.date"> <!--<t t-if="widget.timerelative" t-esc="widget.timerelative"/>--> <!--<t t-if="!widget.timerelative" t-raw="widget.display_date"/>--> <t t-raw="widget.display_date"/> </span> .... </t> </template> 

need to declare this xml file in __ openerp __. py

It worked for me.

0
source

Source: https://habr.com/ru/post/1210926/


All Articles