cancel
Showing results for 
Search instead for 
Did you mean: 

reusing macros on webscripts

dloureiro
Champ in-the-making
Champ in-the-making
Hi,
I've lots of scripts that are using macros to output content metadata as xml.

The problem i'm facing is that, in every template i've to do the same output, i've in the .ftl file repeated (copy & paste) all the macros i need. When i need to change one , i've to change all the files where i'm using the 'same' macro

It's possible to have the macros on a separated file and import it in the templates where needed and from one template execute one macro from another?

something like

<#assign macro = search.findNode("workspace://SpacesStore/c189b089-be11-11dc-a16e-6fac0b341fb1")>
<@macro.printdocprops doc=document />

Thanks in advance
10 REPLIES 10

dloureiro
Champ in-the-making
Champ in-the-making
i found this from freemarker

http://fmpp.sourceforge.net/freemarker/ref_directive_import.html
Example:   
<#import "/libs/mylib.ftl" as my>
<@my.copyright date="1999-2002"/>

but can´t find a way to use it on alfresco

davidc
Star Contributor
Star Contributor
Have you tried 'include'…

http://fmpp.sourceforge.net/freemarker/ref_directive_include.html

I haven't tried it myself, but if your templates are part of a webscript, the include path should be relative to one of the web script search paths e.g. Data Dictionary/Web Scripts

Have a go, and let us know.

dloureiro
Champ in-the-making
Champ in-the-making
Thanks david for the quick reponse.

The include directive seems ok and do what i want

After trying the include directive, i've found that the relative paths doesn't seem to be working (maybe due to internationalization problems - my alfresco is in Portuguese).

The solution i will use is
<#include "*/macros.get.xml.ftl">
and to call the macro

<@printdocprops doc=document />

Keep the good work.
Regards

davidc
Star Contributor
Star Contributor
Ok, at some point, I'll try out relative paths and see what happens.

dloureiro
Champ in-the-making
Champ in-the-making
after some tests, (I've tested on a alfresco machine in english too)

here is my conclusion:
- the relative paths  aren't working
  if i start the path like Data Dictionary/Scripts the real path used by import is
<current space path>/Data Dictionary/Scripts

if i start the path like
/somespace/somespace/

if my script is in Data Dictionary/Scripts the real path used is
Data Dictionary/Scripts/somespace/somespace/

if my script is in Data Dictionary/Web Scripts the real path used is
Data Dictionary/Web Scripts/somespace/somespace/

i can't access a script from Data Dictionary/Scripts  from a script within
Data Dictionary/Web Scripts

the import directive woks fine too, the problem was the path

the // in the path, works too.


<#import "/logical/staging/macros.get.xml.ftl" as my>

Thanks a lot

kevinr
Star Contributor
Star Contributor
i found this from freemarker

http://fmpp.sourceforge.net/freemarker/ref_directive_import.html
Example:   
<#import "/libs/mylib.ftl" as my>
<@my.copyright date="1999-2002"/>

but can´t find a way to use it on alfresco

The full path to the ftl to include should work?

Kevin

dloureiro
Champ in-the-making
Champ in-the-making
hi kevin,
thanks for the replay.

i don't know if it should work or not.

Full paths like
/Company Home/Data Dictionary/Web Scripts/mylib.ftl

doesn't work.

It would be nice if we can reference a lib by the full path too…

Regards

mikef
Champ in-the-making
Champ in-the-making
Try this:

<#include companyhome.childByNamePath["Data Dictionary/Web Scripts/My Template.ftl"].nodeRef>

Regards

Mike.

mikef
Champ in-the-making
Champ in-the-making
Just to clarify this, Freemarker includes work differently depending on where you are using them.

Web Scripts
If you want it use includes in Web Scripts you should use paths relative to the Web Script classpath e.g.

<#include "/demo/include/include.ftl">

Where the class path is:
   1.  repository folder: /Company Home/Data Dictionary/Web Scripts Extensions
   2. repository folder: /Company Home/Data Dictionary/Web Scripts
   3. class path folder: /alfresco/extension/templates/webscripts
   4. class path folder: /alfresco/templates/webscripts
Please Note: Do not store web scripts in /org/alfresco or any of its subfolders. These folders are reserved for Alfresco use.

Custom Views/Direct Execution etc
If you want to use includes in Freemarker outside of a web script, such as a Custom View you should use something like this:
   
<#include companyhome.childByNamePath["Data Dictionary/Web Scripts/My Template.ftl"].nodeRef>