Учитывая этот шаблон:
<template id="minimal_layout_inherit" inherit_id="report.minimal_layout">
<xpath expr="//head" position="inside">
<link rel='stylesheet' href="my_module/static/src/css/mycss.css"/>
</xpath>
</template>
Как я могу передать переменную с именем css_file из бэкэнда, чтобы шаблон можно было изменить на это:
<template id="minimal_layout_inherit" inherit_id="report.minimal_layout">
<xpath expr="//head" position="inside">
<link rel='stylesheet' t-att-href="css_file"/>
</xpath>
</template>
Я попробовал парсер KabyR, адаптированный к моему случаю. Но переменная css_file не передается в унаследованный Minimum_layout: class ParticularReport(models.AbstractModel): _name = 'report.my_module.report_saleorder'
@api.model
def render_html(self, docids, data=None):
report_obj = self.env['report']
report = report_obj._get_report_from_name('my_module.report_saleorder')
docs = self.env[report.model].browse(docids)
docargs = {
'doc_ids': docids,
'doc_model': report.model,
'docs': docs,
'css_file': "/my_module/static/css/sale_report.css",
'css_file2': "my_module/static/css/sale_report.css",
}
print '** DEBUG render_html docargs'
print docargs
for doc in docargs['docs']:
print 'doc.id'
print doc.id
return report_obj.render('my_module.report_saleorder', docargs)
Учитывая, что у меня есть в моем пользовательском модуле:
<template id="report_saleorder">
<t t-call="report.html_container">
<t t-foreach="docs" t-as="doc">
<t t-call="my_module.report_saleorder_document" t-lang="doc.partner_id.lang"/>
</t>
</t>
</template>
являющийся шаблоном report.html_container, который использует минимальный_шаблон (тот, который унаследован от того места, где нам нужна переменная).
Но css_file окончательно недоступен в унаследованном шаблоне
<template id="report_saleorder_css" inherit_id="report.minimal_layout">
<xpath expr="html/head/*[position() = last()]" position="after">
<link rel="stylesheet" t-attf-href="my_module/static/css/sale_report.css"/>
<link rel="stylesheet" t-att-href="css_file"/>
<link t-att-href="'%s' % (css_file)" rel="stylesheet"/>
<link t-att-href="'%s' % (css_file2)" rel="stylesheet"/>
<span>AAA</span><span><t t-esc="css_file" /></span><span>BBB</span>
</xpath>
</template>
И вы получаете
2017-06-23 00:03:09,376 5131 INFO ? werkzeug: 127.0.0.1 - - [23/Jun/2017 00:03:09] "GET /my_module/static/css/sale_report.css HTTP/1.1" 200 -
2017-06-23 00:03:09,397 5131 INFO demo_v10 werkzeug: 127.0.0.1 - - [23/Jun/2017 00:03:09] "GET /None HTTP/1.1" 404 -
2017-06-23 00:03:09,963 5131 INFO demo_v10 werkzeug: 127.0.0.1 - - [23/Jun/2017 00:03:09] "GET /None HTTP/1.1" 404 -
Как видите, первый (жестко закодированный) работает, второй и третий (используют переменные css_file и css_file2) — нет. Также ничего не появляется между AAA и BBB в документе. Итак, css_file там нет.