cancel
Showing results for 
Search instead for 
Did you mean: 

How to address an aspect from a template

rdifrango
Champ in-the-making
Champ in-the-making
I have created a custom aspect called department that has 1 attribute of departmentName, how do I get access to this value from a Freemarker template:

<#if c.hasAspect("demo:department")?exists>
<departmentName>${c.properties["demo:department"][0]}</departmentName>
<#else>
<departmentName/>
</#if>

Did not work and neither did:

<departmentName>${c.properties.department.departmentName}</departmentName>

Any help would be appreciated.

I saw this post but the answer was not posted:

http://forums.alfresco.com/en/viewtopic.php?f=12&t=6889&start=0&st=0&sk=t&sd=a&hilit=hasaspect
4 REPLIES 4

cleseach
Star Contributor
Star Contributor
You should try :
${c.properties["demo:departmentName"]}

Regards,
Charles Le Seac'h

rdifrango
Champ in-the-making
Champ in-the-making
Yeah, I tried that as well:


<#if c.hasAspect("demo:department")?exists>
   <departmentName>${c.properties["demo:departmentName"]?string?replace("&", "&amp;")}</departmentName>
   <#else>
    <departmentName/>
   </#if>

And here was the response:


Expression c.properties["demo:departmentName"] is undefined on line 58, column 27 in org/demo/dmbrowse.get.xml.ftl.
The problematic instruction:
———-
==> ${c.properties["demo:departmentName"]?string?replace("&", "&amp;")} [on line 58, column 25 in org/demo/dmbrowse.get.xml.ftl]
in user-directive nodeInfo [on line 91, column 9 in org/demo/dmbrowse.get.xml.ftl]
———-

Java backtrace for programmers:
———-
freemarker.core.InvalidReferenceException: Expression c.properties["demo:departmentName"] is undefined on line 58, column 27 in org/demo/dmbrowse.get.xml.ftl.
   at freemarker.core.TemplateObject.assertNonNull(TemplateObject.java:124)

The bizarre this is that if I dump all the properties by interation over them using the following as an example:

http://wiki.alfresco.com/wiki/FreeMarker_Template_Cookbook#Document_Properties_Example

The property does display ok, here is the dump of those:


<properties>
      <property>

             <name>{http://www.alfresco.org/model/content/1.0}description</name>
             <value></value>
       </property>
      <property>
             <name>{http://www.alfresco.org/model/content/1.0}content</name>
             <value>org.alfresco.repo.template.BaseContentNode$TemplateContentData@d72be</value>
       </property>

      <property>
             <name>{http://www.alfresco.org/model/system/1.0}store-protocol</name>
             <value>workspace</value>
       </property>
      <property>
             <name>{http://www.alfresco.org/model/system/1.0}node-uuid</name>
             <value>e2243099-90e0-4945-bc89-afd2cba3f052</value>

       </property>
      <property>
             <name>{http://www.alfresco.org/model/content/1.0}created</name>
             <value>Jan 7, 2009</value>
       </property>
      [color=#FF0000]<property>
             <name>{http://www.demo.org/model/content/1.0}departmentName</name>

             <value>IT &amp; Help Desk</value>
       </property>[/color]
      <property>
             <name>{http://www.alfresco.org/model/content/1.0}title</name>
             <value>DCP_4963.JPG</value>
       </property>

      <property>
             <name>{http://www.alfresco.org/model/system/1.0}node-dbid</name>
             <value>524</value>
       </property>
      <property>
             <name>{http://www.alfresco.org/model/content/1.0}author</name>
             <value></value>

       </property>
      <property>
             <name>{http://www.alfresco.org/model/content/1.0}modifier</name>
             <value>admin</value>
       </property>
      <property>
             <name>{http://www.alfresco.org/model/content/1.0}modified</name>

             <value>Jan 7, 2009</value>
       </property>
      <property>
             <name>{http://www.alfresco.org/model/system/1.0}store-identifier</name>
             <value>SpacesStore</value>
       </property>
      <property>

             <name>{http://www.alfresco.org/model/content/1.0}creator</name>
             <value>admin</value>
       </property>
      <property>
             <name>{http://www.alfresco.org/model/content/1.0}name</name>
             <value>DCP_4963.JPG</value>
       </property>

</properties>

So I did also try:


<#if c.hasAspect("demo:department")?exists>
   <departmentName>${c.properties["{http://www.demo.org/model/content/1.0}departmentName']?string?replace("&", "&amp;")}</departmentName>
   <#else>
       <departmentName/>
   </#if>

And that did not work either.

mikeh
Star Contributor
Star Contributor
I think the problem lies in your <#if c.hasAspect("demo:department")?exists> statement - it's not doing what you expect it to.

The ?exists directive (actually now deprecated to just ??) tests for null. The hasAspect() function will always return true or false, so your exists check will always return true. Therefore you'll get the error if you iterate over a node that doesn't actually have the aspect.

Mike

rdifrango
Champ in-the-making
Champ in-the-making
Mike,

That did the trick, I now have the following:


<#if c.properties["demo:departmentName"]?exists>
   <departmentName>${c.properties["demo:departmentName"]?string?replace("&", "&amp;")}</departmentName>
<#else>
   <departmentName/>
</#if>

And it works like a champ.

Ron