3.5. How to use snippets
Syntax for calling a snippet from XML:
Freemarker |
${fmsnippet.path.to.snippet}
|
Velocity |
${snippet.jhipster.html.overview}
|
The result of the evaluated template is then inserted in the XML pattern at the place where it was called.
Example using Snippets shows how this works in practice.
This is the way to go in MetaFactory when you wish to evaluate a complex expression for which the MetaFactory expression language does not have the required tool. Just print the result of the expression in your Freemarker snippet. When it comes to generating plain text files, simply use a single snippet within the XML pattern, as the below example shows.
yy
<file xmlns="http://www.firstbase.nl/xsd/personaliom/pattern"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.firstbase.nl/xsd/personaliom/pattern http://www.firstbase.nl/xsd/personaliom/pattern.xsd"
name="somename.html"
path="..."
package="..."
foreach="object"
condition="..."
trimLeading="..."
trimTrailing="...">
<body>${fmsnippet.frontend.index.html}</body>
</file>
The third use case that we mentioned is to extract and manipulate model data. When doing so in a snippet you need access to the model (meta)data and likely to the pattern properties as well.
Access to the model is provided by the Model API. Access to pattern properties is provided through the context object that is made available along with many other variables in the Freemarker or Velocity context.
To be able to use IDE autocompletion on these objects please first follow the instructions at Configuring your IDE.
Through the context object you can then call a defined pattern property using the syntax:
context.getPatternPropertyValue(“full.property.name”).
Read more about template engines.