document-templates
document-templates copied to clipboard
Can we provide the model relations data to the template?
Can we use placeholders with relations, eg model1.model2.field?
Hi @danielsuguimoto,
This is a bit tricky, and there is no complete support for it currently, but I can suggest some workarounds:
- The simplest would be to add the related model data as separate template data
$documentTemplate->addTemplateData($model1, 'model1');
$documentTemplate->addTemplateData($model2, 'model2');
- or, for a first level relation we can add the relation to the
getTemplateFieldsmethod of themodel1:
protected function getTemplateFields()
{
return [
'any_other_field',
'model2'
];
}
and use it in the template like {{model1.model2.field}}. It should work as expected. Unfortunately this workaround is not universal, deeper level relations won't work. Also it would show up in the ckeditor dropdown as model1.model2 without the actual fields from the model2.
Obviously these are not ideal solutions, and it makes sense to add support for the model relations in general. (including the admin part and the ckeditor).