Hi Alex,
Sorry to chime in so late, but I wanted to give you some pointers.
As Jan writes, it is actually easier to just write "helper classes". Such a
class just needs registering as a context attribute (for example globally under
/modules/rendering/renderers/freemarker/contextAttributes as Jan suggests, or
on a per template basis by putting it in the template definition parameters).
The class doesn't need to be part of an official module, just available
somewhere on the class path (like in a JAR in tomcat's lib folder).
You can then call the functions on that class from inside freemarker, something
like this:
global context object:
${myHelper.myCoolMethod()}
template definition object:
${def.parameters.myHelper.myCoolMethod()}
If you want to actually create a directive, I see mainly one advantage:
directives can have a body, so you can do cool recursive stuff, or wrap other
things with beginning and end parts.
I created a few directives, so it is possible. Look at the ones magnolia
created in the templating module, see
info.magnolia.templating.freemarker.AbstractDirective and
info.magnolia.templating.elements.AbstractContentTemplatingElement and their
subclasses.
When creating a directive, it isn't that complicated, the parameter handling is
a bit of a pain.
To get magnolia to recognize the directives, add them to a HashMap<String,
TemplateDirectiveModel> and put that in the global freemarker context. We do
this by creating a subclass:
public class ATKDirectives extends HashMap<String, TemplateDirectiveModel> {
@Inject
public ATKDirectives(ServerConfiguration server,
TemplateDefinitionAssignment templateDefinitionAssignment){
super();
put("status", new StatusDirective());
put("pagesExtension", new PagesExtensionDirective(server));
put("draggable", new
DraggableDirective(templateDefinitionAssignment));
put("droppable", new
DroppableDirective(templateDefinitionAssignment));
put("sortable", new SortableDirective());
put("uuid", new UuidDirective());
put("handle", new HandleDirective());
}
}
Which we add as a component in our module descriptor:
<components>
<id>main</id>
<component>
<type>xx.magnolia.atk.freemarker.ATKDirectives</type>
<implementation>xx.magnolia.atk.freemarker.ATKDirectives</implementation>
</component>
And then refer to in the freemarker context. Not sure if there isn't a simpler
way, but this way is working for us...
Regards from Vienna,
Richard
________________________________________
Von: [email protected] [[email protected]]"
im Auftrag von "Jan Haderka (via Magnolia Forums) [[email protected]]
Gesendet: Donnerstag, 08. September 2016 09:14
An: Magnolia User List
Betreff: [magnolia-user] Re: Struggling with first directive
Hi Alex,
I guess the lack of responses comes from the fact that no one is really trying
to write directives for Freemarker. Most of the time it is enough to write a
simple templating function class and register it under
/modules/rendering/renderers/freemarker/contextAttributes as you have tried.
Such class can be anything with either no-arg constructor or with one annotated
with @Inject and all params in the constructor being resolvable at runtime of
Magnolia. Also on top of that you need to write a Magnolia module and register
the templating class as component (see docu on modules for more details on
that).
As for STK dependencies, if you are building something new, i would not go for
STK as you will find it too big and intensive to learn and later limiting in
what you can express in your webapp. There's twitter bootstrap module on forge
that would give you much more freedom for your website so I'd suggest going w/
that. If however you need STK for whatever reason and you are building up your
webapp with maven, you can simply inherit dependency management section from
the webapp provided by magnolia that includes stk - see
https://documentation.magnolia-cms.com/display/DOCS/Bundles+and+webapps for
more details there.
HTH,
Jan
--
Context is everything:
http://forum.magnolia-cms.com/forum/thread.html?threadId=38c77b48-fb81-42de-b4ad-b37132abc3eb
----------------------------------------------------------------
For list details, see http://www.magnolia-cms.com/community/mailing-lists.html
Alternatively, use our forums: http://forum.magnolia-cms.com/
To unsubscribe, E-mail to: <[email protected]>
----------------------------------------------------------------
----------------------------------------------------------------
For list details, see http://www.magnolia-cms.com/community/mailing-lists.html
Alternatively, use our forums: http://forum.magnolia-cms.com/
To unsubscribe, E-mail to: <[email protected]>
----------------------------------------------------------------