How to use and import an Alfresco NodeRef in Freemarker template?

Viewed 173

In Alfresco, I have multiple workflows in which i send an email based on certain events. Each e-mail template uses the same layout and design. Therefore, I want to import a "shell" template and pass in different variables based on the type of workflow and event.

// lib/utils.html.ftl
<#macro shell greeting>
   ${greeting}
</#macro>

So as example, I have a task-assignment.ftl template, where I want to import my base template.

// task-assignment.html.ftl
<#import "./lib/utils.html.ftl" as utils>
<#assign greeting="${greeting!'Hello User'}" />
<@utils.shell greeting />

My problem is the value of the path to the file in the Freemarker <#import >.

I can't figure out how to get the path in a format that Freemarker understands and Alfresco can provide.

var file = companyhome.childrenByXPath("/app:company_home/app:dictionary/app:email_templates/cm:test/cm:utils.html.ftl");


logger.log(file[0].url) => /d/d/workspace/SpacesStore/08234083-23948234-2349834/utils.html.ftl

<#import file as utils>

Caused by: freemarker.core._MiscTemplateException: Error reading imported template string://d/d/workspace/SpacesStore/08234083-23948234-2349834/utils.html.ftl

Does anyone know, how to get the path to the file in a format that Freemarker is happy with and Alfresco is able to provide?

I'm using Freemarker version 2.3.20 and Alfresco Community version 6.1.2.

1 Answers

You can use the import your base template if that's also in your classpath. See the below code snippet.

<#import "../refresh_script.html.ftl" as refresh>

<div id="Rec1" class="info">
    <div id="Ctrl_top_read" class="titremontre hd" ><img src="${url.context}/images/deployment_report.gif" alt="icon"><span class="titlebox">${msg("mostread")}</span></div>
    <@refresh.displayRefreshButton />
    <div onClick="Display(this)" id="bt_top_read" class="button buttonUp"></div>
    <div id="top_read" class="montre bd">
    <#if topread?string?trim=="nodata" || topread?string?trim=="error" || topread?string?trim=="invalid">
        <div class="${topread?string?trim}">${msg("${topread?string?trim}")}</div>
    <#else>
        ${topread}
    </#if>
    </div>
</div>

To get the Noderef in ftl file.

Define <param name="destination">{node.nodeRef}</param> in your action alfresco includes the noderef as a hidden input field.

Then youy can do document.getElementById("{}").value or YAHOO.util.Dom.get("{}").value to get the nodeRef.

Related