Re: Custom directive in template not identified by freemarker

Daniel Dekany <[email protected]> Fri, 24 May 2013 16:09:45 +0200
Newsgroups gmane.comp.web.freemarker.user
Message-ID <[email protected]>
Directives are just plain values in FreeMarker (), and there's no "magic"
that binds a name to a directive implementation. So to be able to do
<@file />, you simply need a variable called "file", whose value is
the instance of the desired TemplateDirectiveModel class. It doesn't
mater where it comes from. Like you could put "file" into the
data-model, or into a shared variable inside the Configuration.
Personally, I prefer the approach where you make an ftl file that you
will #import or #include, and in that template you issue

  <#assign file = 'com.example.whatever.FileDirective'?new()>

The advantage of this approach is that you can also put #macro-s and
#function into the same file, and more directives of course. So it's
somewhat transparent for the template author if something is
implemented as a #macro or a directive (its only "somewhat
transparent", as a directive can't have parameters passed by position
right now, only by name).

You are right in that this is poorly documented. I will look into this
as time permits.

-- 
Thanks,
 Daniel Dekany


Friday, May 24, 2013, 3:08:04 PM, srihari_ravi wrote:

> I have read the current documentation on custom directives.
>
> My basic doubt is: how does freemarker link between a custom directive class
> (that implements the TemplateDirectiveModel interface) and the custom
> directive I want to use in my template? Lets say that I want to create a
> directive that creates a file with the custom directive's body. I want to
> call this directive @file and I create a corresponding FileDirective class
> implementing the interface. How does freemarker understand that this is the
> class implementation for @file?
>
> I currently get an error like below. Thanks in advance for helping!
> - Ravi.
>
>   [javadoc] May 24, 2013 2:53:20 PM
> freemarker.log.JDK14LoggerFactory$JDK14Logger error
>   [javadoc] SEVERE: Template processing error: "on line 14, column 1 in
> ErrorDoc_Modules.ftl file not found."
>   [javadoc]
>   [javadoc] on line 14, column 1 in ErrorDoc_Modules.ftl file not found.
>   [javadoc] The problematic instruction:
>   [javadoc] ----------
>   [javadoc] ==> *user-directive file* [on line 14, column 1 in
> ErrorDoc_Modules.ftl]
>   [javadoc] ----------
>   [javadoc]
>   [javadoc] Java backtrace for programmers:
>   [javadoc] ----------
>   [javadoc] freemarker.core.InvalidReferenceException: on line 14, column 1
> in ErrorDoc_Modules.ftl file not found.
>   [javadoc]     at
> freemarker.core.UnifiedCall.accept(UnifiedCall.java:134)
>   [javadoc]     at
> freemarker.core.Environment.visit(Environment.java:221)
>   [javadoc]     at
> freemarker.core.MixedContent.accept(MixedContent.java:92)
>   [javadoc]     at
> freemarker.core.Environment.visit(Environment.java:221)
>   [javadoc]     at
> freemarker.core.Environment.process(Environment.java:199)
>   [javadoc]     at
> freemarker.template.Template.process(Template.java:259)
>   [javadoc]     at
> com.splwg.oms.doc.ErrorDocumentDoclet2.freemarkerDo(ErrorDocumentDoclet2.java:267)
>   [javadoc]     at
> com.splwg.oms.doc.ErrorDocumentDoclet2.createOutputFiles(ErrorDocumentDoclet2.java:249)
>   [javadoc]     at
> com.splwg.oms.doc.ErrorDocumentDoclet2.createPortalFiles(ErrorDocumentDoclet2.java:119)
>   [javadoc]     at
> com.splwg.oms.doc.ErrorDocumentDoclet2.start(ErrorDocumentDoclet2.java:86)
>   [javadoc]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native
> Method)
>   [javadoc]     at
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
>   [javadoc]     at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
>   [javadoc]     at java.lang.reflect.Method.invoke(Method.java:601)
>   [javadoc]     at
> com.sun.tools.javadoc.DocletInvoker.invoke(DocletInvoker.java:280)
>   [javadoc]     at
> com.sun.tools.javadoc.DocletInvoker.start(DocletInvoker.java:160)
>   [javadoc]     at
> com.sun.tools.javadoc.Start.parseAndExecute(Start.java:397)
>   [javadoc]     at com.sun.tools.javadoc.Start.begin(Start.java:167)
>   [javadoc]     at com.sun.tools.javadoc.Main.execute(Main.java:59)
>   [javadoc]     at com.sun.tools.javadoc.Main.main(Main.java:49)
>
>
>
> --
> View this message in context:
> http://freemarker.624813.n4.nabble.com/Custom-directive-in-template-not-identified-by-freemarker-tp4654555.html
> Sent from the freemarker-user mailing list archive at Nabble.com.
>
> ------------------------------------------------------------------------------
> Try New Relic Now & We'll Send You this Cool Shirt
> New Relic is the only SaaS-based application performance monitoring service
> that delivers powerful full stack analytics. Optimize and monitor your
> browser, app, & servers with just a few lines of code. Try New Relic
> and get this awesome Nerd Life shirt!
> http://p.sf.net/sfu/newrelic_d2d_may
> _______________________________________________
> FreeMarker-user mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/freemarker-user
>


------------------------------------------------------------------------------
Try New Relic Now & We'll Send You this Cool Shirt
New Relic is the only SaaS-based application performance monitoring service 
that delivers powerful full stack analytics. Optimize and monitor your
browser, app, & servers with just a few lines of code. Try New Relic
and get this awesome Nerd Life shirt! http://p.sf.net/sfu/newrelic_d2d_may