Community mailing list archives

community@mail.odoo.com

Re: Render a html field as qweb

by
Paul Catinean
- 01/24/2016 05:59:11
Hello Yannik,

Not sure if I understood correctly but in the mako syntax you can run python methods and python code

Also if you want your variables rendered as html use t-raw

If that's not the problem I'm not sure how else could your variables be rendered if not as text?

Regards,
Paul

On Sat, Jan 23, 2016 at 10:07 PM, Yannik Lieblinger <yannik@lieblinger.de> wrote:

Hi Paul,

Hi all,

sorry, I've made an failure. The get_text() function only returns false.

    def get_text(self):
        # For testing
        env = api.Environment(self.cr, self.uid, self.localcontext)
        template = env['clb_presuffix_text.config'].search([('textType', '=', 'pretext')]).text
        user = env['res.users'].browse(env.uid)
        # try to load the template
        model = env['sale.order']
        ids = env['res.partner'].search([('id', '=', 379)])
        mail = env['email.template']
        try:
            template = mako_template_env.from_string(tools.ustr(template))
        except Exception:
            _logger.exception("Failed to load template %r", template)
            return results
        variables = {
            'user': user,
            'ctx': env['res.partner'].search([('id', '=', 379)]),
            'testVar': 'Test text. Is not shown : (.',
        }
        try:
            render_result = template.render(variables)
        except Exception:
            _logger.exception("Failed to render template %r using values %r" % (template, variables))
            render_result = u""

        return render_result

I will take an deeper look into the mail template module, maybe I find an solution to fix it.

But I would be really thankful if somone had an idea or an helpful tip.

Thanks for all!

Regards Yannik


Paul Catinean <paulcatinean@gmail.com> hat am 22. Januar 2016 um 13:12 geschrieben:

Hello Yannik,

Glad to have you among us

I'm not sure if this is the best way to do it but it works for emails that need parsing variables and translation before sending

Using QWeb alone should be possible too but for messages like you mentioned I think the mako syntax is much easier

1. Firstly import the mako template environment and openerp tools

from openerp.addons.email_template.email_template import mako_template_env
from openerp import tools

2. Then you use the mako template environment to render and translate your content using mako syntax:

mako_template_env.from_string(tools.ustr(your_mako_template_here)).render(dict_of_variables_used_in_mako)

You can place the final result in a variable or a method and call from qweb using t-esc (which is safer and does not need to do anything except output the result)

You can also find examples of mako templates in the standard Email templates

Hope this helps

Regards,
Paul

On Fri, Jan 22, 2016 at 11:36 AM, Yannik Lieblinger <yannik@lieblinger.de> wrote:

Hi all,

I try to build an new module for our sales order and invoice reports.


The Module should add custom text with variables like o.partner_id.phone to the end of the report. I have create an Class with an text field which should hold the custom qweb subtemplates.


If I call the field via t-raw all looks great but no variables getting rendered and they are printed as text only. I'm new to odoo, so I don't really understand the python print workflow. But is there a possibility to render my templates from the text field with variables which are available in the sale order document?

Regards Yannik

 


_______________________________________________
Mailing-List: https://www.odoo.com/groups/community-59
Post to: mailto:community@mail.odoo.com
Unsubscribe: https://www.odoo.com/groups?unsubscribe


_______________________________________________
Mailing-List: https://www.odoo.com/groups/community-59
Post to: mailto:community@mail.odoo.com
Unsubscribe: https://www.odoo.com/groups?unsubscribe




_______________________________________________
Mailing-List: https://www.odoo.com/groups/community-59
Post to: mailto:community@mail.odoo.com
Unsubscribe: https://www.odoo.com/groups?unsubscribe