fmx:directive

fmx:directive allows to render macro calls or registered FTL directives. Therefore the usage actually depends on the concrete macro call/directive.

Dedicated XML element

<fmx:a-teasergenerator name="'ypsilon'" age="model.age">
   <div class="bobo">
       <p>Hello</p>
   </div>
</fmx:a-teasergenerator>
[@a-teasergenerator name='ypsilon' age=model.age]
     <div class="bobo">
         <p>Hello</p>
     </div>
[/@a-teasergenerator]

You will notice several things in here. First of all this example uses single quotes within an attribute value for name whereas the attribute age doesn't. The difference is that the content within the double-quotes is used as a FTL expression. Therefore it's necessary to use the single-quotes within to provide a literal.

However this behaviour can be altered when instantiating the FmxTemplateLoader.

 

Changing/mapping directive names

The constructor accepts a function which maps the directive name coming from the template to a name that should end up in FTL (Function<String, String>). Let's say you want to write Magnolias directives using a minus sign:

The function can be writter as $ -> $.replace( '-', '.' ) in order to achieve this. There are essentially no other limits than the necessity to match this signature.

 

Changing the attribute mapping

Let's assume you don't like the fact that you need to use single quotes for attributes where you want to supply a literal.  In that case you can provide an attribute mapper per directive. This is essentially a map with the following structure:

Map<String, BiFunction<String, String, String>>

The key is always the name of the current directive being processed. Let's stick with the aforementioned example which means, that we want to map attributes for the directive cms.area . Our intention is to enter attribute values without the single quotes, thus we need a function that generates the corresponding expression:

 

private String mapCmsArea( String attrLocalName, String value ) {
    return String.format( "\"%s\"", value );
}

Map<String, BiFunction<String, String, String>> map = ...;
map.put( "cms.area", this::mapCmsArea );

Whenever a cms.area directive is being processed, the attributes values are processed through the registered function here. Since our function mapCmsArea generates quotes around our value it's no longer necessary to supply single quotes within the attribute values.