ngs-doo / TemplaterExamples

Creating reports in .NET and Java
https://templater.info
The Unlicense
48 stars 27 forks source link

Can aliases only be defined in the templater editor? #74

Closed asmaier closed 3 months ago

asmaier commented 3 months ago

This is something a bit unclear to me from the user manual. Can aliases only be defined using the templater editor? Because at one point in the documentation it says

It is more common to define such aliases in the template itself, but when appropriate this can also be done via API.

So I thought this means I could add an alias directly into the template e.g. by writing

{{atColor = colors:at(1)}}
My favorite color is {{atColor.name}}.

as it is also possible with plumsail: https://plumsail.com/docs/documents/v1.x/document-generation/common-docx-xlsx/aliases.html

But that doesn't seem to work with templater-json.jar alone. Is it correct that to create an alias one always needs the templater-editor? Or is it somehow possible to define aliases without using the editor simply by defining that alias in the template?

zapov commented 3 months ago

You can register aliases via code through configuration: https://templater.info/apidocs/hr/ngs/templater/EditorConfigurationBuilder.html#addAlias-java.lang.String-java.lang.String-

An approach to do it could be to have first pre-processing like that just to pick up aliases and then register them for actual processing, but with that approach there is no built-in support for alias recognition in Templater Editor.

Lots of people have been doing various pre-processing to convert initial document into something else which Templater can recognize and process, from some math formula evaluation to in-document definition of aliases like in your example.

asmaier commented 3 months ago

Thank you, that makes it clear to me. For in-document definition of aliases I would have to implement a pre-processing step to pick up all the aliases from the template. Then I could leverage the EditorConfiguration API to register these aliases and use them in the final step when inserting data into the template.