Community: Framework mailing list archives

expert-framework@mail.odoo.com

t-esc in website

by
Pedro Cachaca
- 10/15/2014 09:33:24
Hi

<t t-esc="res_company.name"/> works fine in any page of the website.

How to render something like <t t-esc="Courses[0].name"/> in any page ?

models.py :
class Courses(models.Model):
    _name = 'academy.courses'
    name = fields.Char()
    description = fields.Html()

I tested below solutions. The third one is working fine but is it a good way to do this ? or another alternative ?


1/ with controllers : seems limited to a page
I can do this with a controller but seems not compatible to show in any page, this will works only in a generated page /academy/courses. Do I miss something ?

controllers.py:
    @http.route('/academy/courses', auth='public', website=True)
    def courses_list(self):
        Courses = http.request.env['academy.courses']
        return http.request.render('academy.courses_list', {'courses': Courses.search([]),})


2/ with controllers+javascript : a little complex
I teste with the use of a javascript (declared as website.snippet.animationRegistry.xxx), in order to replace a <div class="courseblock/> with the content, but seems a little complex, to just show a simple t-esc.....
.js :
           $.post( "/academy/courses", function( data ) {
               if(data){
                   $( ".courseblock" ).replaceWith( data );
               }
           });


3/ linked with a res_company : OK, is it a good solution ? 

<div t-esc="res_company.course_ids[0].description"/>

works fine ;), needs to link Courses with Res_company in the models. Is it a good solution ?

models.py:
class res_company(models.Model):
    _inherit ='res.company'
    course_ids = fields.One2many('weebnb.bnbs', 'company_id', string="bnb")
class Courses(models.Model):
    _name = 'academy.courses'
    name = fields.Char()
    description = fields.Html()
    company_id = fields.Many2one('res.company', string="Company")

4/ Note: Res_company

I saw in  /addons/website/models/ir_ui_view.py that res_company is defined into the def render.
Do we have to do something similar ?

def render(self, cr, uid, id_or_xml_id, values=None, engine='ir.qweb', context=None):
...
company = self.pool['res.company'].browse(cr, SUPERUSER_ID, request.website.company_id.id, context=context)
...
res_company=company
...

Thanks your help ;)