Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to inherit a template with no ID in Odoo?

Tags:

templates

odoo

I am trying to show the date a change was made in a task. To do this, I need to inherit the template of the widget "mail_thread". That template hasn't an id in its definition. This is it:

<?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 in order to show the date.

So, how to inherit that template and replace the tag?

like image 922
Breba Avatar asked Jan 12 '15 17:01

Breba


1 Answers

There are different inheritance mechanism for client side templates (web templates, defined inside a <templates> tag, "compiled" with javascript in the client when loading it) and server-side templates (usually views, must be included in the data list in the __openerp__.py file, 'compiled' when launching/upgrading the odoo server).

You extend web/widget templates templates using <t t-extend="template_name"> followed by one or more <t t-jquery="jquery_selector" t-operation="operation"> which acts kinda like xpath, but client side and more 'powerful'.
You don't need ids, inheritance is based on the template name. (t-name directive)

  • Server-Side view inheritance
  • Client-Side template inheritance:

Template inheritance is used to alter existing templates in-place, e.g. to add information to templates created by an other modules.

Template inheritance is performed via the t-extend directive which takes the name of the template to alter as parameter.

The alteration is then performed with any number of t-jquery sub-directives:

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

The t-jquery directives takes a CSS selector. This selector is used on the extended template to select context nodes to which the specified t-operation is applied:

  • append
    the node’s body is appended at the end of the context node (after the context node’s last child)
  • prepend
    the node’s body is prepended to the context node (inserted before the context node’s first child)
  • before
    the node’s body is inserted right before the context node
  • after
    the node’s body is inserted right after the context node
  • inner
    the node’s body replaces the context node’s children
  • replace
    the node’s body is used to replace the context node itsel
  • No operation
    if no t-operation is specified, the template body is interpreted as javascript code and executed with the context node as this
like image 94
LeartS Avatar answered Oct 06 '22 12:10

LeartS