M2Doc - Use of Template Blocks to implement queries / functions

Hello,

I just discovered recently that it is possible to use template block in order to implement queries or functions.
In this example ( M2Doc/tests//template/nominal/nominal-template.docx · GitHub), the template block called myTemplate allows to perform a mathematical operation on a Integer.

Up to know, I was using the Template blocks to directly inject content in the documentation generation (like write a Title, create a bullet list…).

Know I wanted to use this functionality to implement a complex query on Capella elements. Like for example, compute from a “Component” the list of external exchanges (exchanges which goes through the boundaries of the component).

The problem I have, is that I want to generate a list of Component Exchanges for example, but I want to be able to manipulate this list of Component Exchanges afterward.
And if I compute this list of Component Exchanges as part of the template block, M2Doc does not recognize the type of the result returned by the Template block, which makes it quite difficult to manipulate (it does not allow me to display the name of the Component Exchanges because it does not know that those elements have a property called name)…

Is it possible to somehow declare the type that will be returned by a Template block ?

Or is there some simple way to declare outside the Template block that the result is of a specific type to allow further manipulation?

Thank you

Hello,

The result of a template construct is a fragment of Word document. You have two ways to factories a query at the moment:

  • using a let where you can reuse the result of a complex expression via a variable. This can be handy to optimize the generation time.
  • write a Java service that will hide the complex logic.

One way other way could be query construct (see this issue).

Thanks Yvan for your answer.

I am aware of Java service and I have already used it. But I wanted to have something more flexible (not requiring the development environment and build / deploy aspect).

I was already using the let blocks, but as I had different alternatives, I was defining different let blocks, which was not really convenient.

But I found that it was possible to manage alternatives inside of a single let block.

For example:

{m:let myVariable = if … then … else … endif}
code
{endlet}

This allows me to implement what I need to do :slight_smile:

1 Like