Atomic Design in Odoo with QWeb templates - atomic

I am designing a website in odoo 11.0 and would like to create a file for some elements that will be used in several views, for example, the calendar, I need to create a file that contains all the html elements for the calendar and then in each view in which I need to show the calendar simply imported the file that contains it, that is, apply Atomic Design, but I searched and could not find it.

To create a file for each element that you need to reuse at another time, you have to create a file with the <odoo>, <data> and <template> tags as if it were a common view, then to import it you use <t t-call = "module.template_id"> </ t>, for example, in my case I need to create a calendar with all the html elements and then import it into other views, for that:
I create the calendar.xml file in my_module/views/share folder with the following code:
<?xml version="1.0" encoding="UTF-8"?>
<odoo>
<data>
<template id="calendar">
<!--HTML code-->
</template>
</data>
</odoo>
I import this file into the module __manifest__.py file
to import it in another view use t-call, suppose that my module is called my_module, to import it we write:
<odoo>
<data>
<template id="index_template">
<t t-call="website.layout">
<div id="wrap">
<div class="container">
<!--=============importing calendar============-->
<t t-call="my_module.calendar">
</t>
<!--=============other HTML code============-->
</div>
</div>
</t>
</template>
</data>
</odoo>
In case the view that I am importing shows dynamic data, for example, I need to show a poster like: choose the date for the: 'event_type' where the possible values ​​are: event, party, reservation, etc. and that it will take value at the moment of showing the calendar, in the calendar file we use a variable that will be set from the view that is importing it, the calendar.xml file would look like this:
<?xml version="1.0" encoding="UTF-8"?>
<odoo>
<data>
<template id="calendar">
<h1>Choose the date for the <t t-esc="event_type"/></h1>
<!--HTML code-->
</template>
</data>
</odoo>
In the file that I'm importing the calendar I have to set the value for the variable 'event_type', In this case, will show: 'Choose the date for the party', the file would look like this:
<odoo>
<data>
<template id="index_template">
<t t-call="website.layout">
<div id="wrap">
<div class="container">
<!--=============importing calendar============-->
<t t-call="my_module.calendar">
<t t-set="event_type">party</t>
</t>
<!--=============other HTML code============-->
</div>
</div>
</t>
</template>
</data>
</odoo>
In the same way we can call the nested views, to reuse everything we want, for example, we can create another template that is imported in calendar.xml so that to display the index page, we import calendar and in turn calendar import another template

Related

Odoo 9 qweb inheritance

In opportunity kanban view I want to remove certain element. It is a span in web_kanban template with t-name="Kanban.Group". I followed this thread How to inherit a template with no ID in Odoo? and related documentation.
I put this
<t t-extend="KanbanView.Group">
<t t-jquery="span.o_kanban_config" t-operation="replace"></t>
</t>
in opportunities kanban template and I also made it a separate xml:
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<template>
<t t-extend="KanbanView.Group">
<t t-jquery="span.o_kanban_config" t-operation="replace"></t>
</t>
</template>
</odoo>
(in this case I appended its name to module's manifest - in 'qweb' list). None of these approaches worked.
Contrary to what I have read about template inheritance, I also tried to use <t t-extend="web_kanban.template">, just in case Odoo needs the modules name just like when inheriting classic views...
Did I do something wrong or miss something? Is there a better/more suitable way to update template?
SOLVED
Finally it works.
I created a separate xml. As I decided to have folding arrows I put this code in it:
<template>
<t t-extend="KanbanView.Group">
<t t-jquery=".o_kanban_config.dropdown" t-operation="inner">
<a class="o_kanban_toggle_fold" href="#"><i class="fa fa-arrows-h"/></a>
</t>
</t>
</template>
I added a declaration to openerp.py manifest:
'qweb':[
'views/updated_kanban.xml',
],
Now only a folding arrow is displayed, no other options (Edit, duplicate etc.).
OInherting Kanban is regular view and has to be inherited like regular views inheritance and this link is the example of the Kanban view inheritance. Kanban view contain qweb but its not actual qWeb template so follow view inheritance document here

Cleanup Document in Eclipse not working in specific case

I have a problem using Eclipse Cleanup Document option in case my XML looks like this.
<?xml version="1.0" encoding="UTF-8"?>
<div>
<span style='color:#0000C8'>>
</span>
<h3 style='margin-left:0cm;text-indent:0cm'><a name="_Toc419970681">2.7.2<span
style='font:7.0pt "Times New Roman"'> </span>Helpers</a></h3>
</div>
Clean document works only if I remove <div> tags or if I move </span> after >
After successful [Source - Cleanup Document] XML should look like this.
<?xml version="1.0" encoding="UTF-8"?>
<div>
<span style='color:#0000C8'>></span>
<h3 style='margin-left:0cm;text-indent:0cm'>
<a name="_Toc419970681">
2.7.2
<span style='font:7.0pt "Times New Roman"'>
</span>
Helpers
</a>
</h3>
</div>
May be due to > (>) eclipse consider it as closing tag of span. Hence it can not validate other content after > as this is not proper closing tag of span/div. If you remove DIV tag then it will not find any open tag except span to treats as > however it did not removes you tag at the end.

AEM 6 sightly: How to read variable from language file?

I have the following html:
<div >${'foo' # i18n}</div>
In my i18n file I have the following:
<foo
jcr:mixinTypes="[sling:Message]"
jcr:primaryType="sling:MessageEntry"
sling:key="foo"
sling:message="This is dummy text"/>
This is dummy text is displayed in the page. well so far. The problem is that foo is a variable which comes form other template and I read as follow:
${fooValue} //this returns foo
Now to read message from the i18n I tried the following:
<div>${'${fooValue}' # i18n} </div>
but This displays ${fooValue} in the page. How to read message from the i18n, if I have variable key?
You could use a local template to which you pass the variable identifying your key for the i18n dictionary:
<template data-sly-template.locale="${# key}">
${key # i18n, locale='de'}
</template>
<div data-sly-call="${locale # key='world'}"></div>
Assuming your i18n dictionary has the translation, the output would be:
<div>
Welt
</div>
You can also call the locale template from another template in your page:
<template data-sly-template.locale="${# key}">
${key # i18n, locale='de'}
</template>
<template data-sly-template.translate="${# string}">
<div data-sly-call="${locale # key=string}" data-sly-unwrap></div>
</template>
<div data-sly-call="${translate # string='world'}"></div>
The output would be the same as above.
The solution by Radu works wonderfully. I have also tried this and this works too, if anyone is looking for a solution without involving templates.
<div>${fooValue # i18n}</div>
I personally would go with templates though. Makes it a little easier to read.
<div>${fooValue # i18n}</div> vs <div data-sly-call="${translate # string=fooValue}"></div>.

How to extract div tag

I'm trying to parse a html file and I want to extract everything inside a outer div tag with a unique id. Sample:
<body>
...
<div id="1">
<div id="2">
...
</div>
<div id="3">
...
</div>
</div>
...
</body>
Here I want to extract every thing in between <div id="1"> and its corresponding </tag> NOT the first </div> tag.
I've gone through many older posts but they don't work because they stop when they see the first </div> tag which is not what I'm looking for.
Any pointer would be appreciated.
It sounds like your problem is that you are trying to parse HTML using regular expressions.
Don't. Use an HTML parser. There are plenty on CPAN. I'm fond of HTML::TreeBuilder::XPath.
Quentin has rightly mentioned using an HTML parser to extract div content. Here's one option using Mojo::DOM:
use strict;
use warnings;
use Mojo::DOM;
my $text = <<END;
<body>
...
<div id="1">
Under div id 1
<div id="2">
Under div id 2
</div>
<div id="3">
Under div id 3
</div>
</div>
Outside the divs
</body>
END
my $dom = Mojo::DOM->new($text);
print $dom->find('div[id=1]')->pluck('text');
Output:
Under div id 1

Overriding a label in Zend Fom config XML

Hi I am using an xml file of the following structure:
<?xml version="1.0"?>
<configdata>
<page>
<form action="" method="post">
<elements>
<page_title type="text" name="page_title" >
<options label="Page Title" required="true" />
</page_title>
<page_content type="textarea" name="page_content">
<options label="Page Content" />
</page_content>
</elements>
</form>
</page>
I have two forms which take exactly the same data, but I need to change the labels on the form. I would rather not just cut and paste the code contained within <page></page> and adjust the labels. Is there a way I can 'extend' page and set the labels that way?
you should be able to do it right from the controller (i know it works with normal Zend_form objects)
$form = new Your_Form_Here();
$form->Elementname->setLabel('new label');
That's all there is to it. You may have to make some adjustments because you're using a config file but it should'nt be to hard.