[MAGNOLIA-2945] FreeMarker ?ancestors and ?root built-ins don't work Created: 17/Nov/09  Updated: 19/Dec/16  Resolved: 04/Nov/15

Status: Closed
Project: Magnolia
Component/s: templating
Affects Version/s: 4.1.1
Fix Version/s: None

Type: Bug Priority: Minor
Reporter: Nils Breunese Assignee: Unassigned
Resolution: Won't Do Votes: 3
Labels: freemarker, vpro
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
duplicate
is duplicated by MAGNOLIA-3164 Freemarker: content?ancestors results... Closed
Template:
Acceptance criteria:
Empty
Task DoD:
[ ]* Doc/release notes changes? Comment present?
[ ]* Downstream builds green?
[ ]* Solution information and context easily available?
[ ]* Tests
[ ]* FixVersion filled and not yet released
[ ]  Architecture Decision Record (ADR)
Bug DoR:
[ ]* Steps to reproduce, expected, and actual results filled
[ ]* Affected version filled
Date of First Response:

 Description   

I wanted to use the FreeMarker ?ancestors built-in (http://freemarker.sourceforge.net/docs/ref_builtins_node.html) in a FreeMarker template. However, it doesn't seem to work. I added this:

[#assign ancs = content?ancestors]

But just this statement results in the following error:

Can't get parent of I18nContentWrapper for website:/[rep:root]:root node doesn't have a parent

I found the same goes for the ?root built-in:

[#assign r = content?root]

Just this assignment results in the same error message:

Can't get parent of I18nContentWrapper for website:/[rep:root]:root node doesn't have a parent

This bug was previously discussed on the Magnolia User-List: http://old.nabble.com/Problem-with-FreeMarker-built-in-for-ancestors-ts26334726.html



 Comments   
Comment by Richard Unger [ 02/Dec/10 ]

Ok, the cause is easy to find - in freemarker, the ancestors built-in steps through the node-model calling getParent() until getParent() returns null, at which point it assumes to have reached the root.

The code looks like this (freemarker 2.3.16):

 static class ancestorsBI extends NodeBuiltIn {
       TemplateModel calculateResult(TemplateNodeModel nodeModel, Environment env) throws TemplateModelException {
           AncestorSequence result = new AncestorSequence(env);
           TemplateNodeModel parent = nodeModel.getParentNode();
           while (parent != null) {
               result.add(parent);
               parent = parent.getParentNode();
           }
           return result;
       }
    }

On the magnolia side, info.magnolia.freemarker.models.ContentModel implements getParentNode() incorrectly for freemarker, throwing an exception if you ask for the root's parent, instead of returning null...

Below is a simple fix to ContentModel:

Index: magnolia-core/src/main/java/info/magnolia/freemarker/models/ContentModel.java
===================================================================
--- magnolia-core/src/main/java/info/magnolia/freemarker/models/ContentModel.java	(revision 35388)
+++ magnolia-core/src/main/java/info/magnolia/freemarker/models/ContentModel.java	(working copy)
@@ -138,8 +138,8 @@
 
     public TemplateNodeModel getParentNode() throws TemplateModelException {
         try {
-            // todo : check if this is the root?
-            // content.getLevel() == 0;
+        	if (content.getLevel()==0) 
+        		return null; // return null if we are at the root, freemarker expects this
             final Content parent = content.getParent();
             return (TemplateNodeModel) wrapper.wrap(parent);
         } catch (RepositoryException e) {

Comment by Jan Haderka [ 27/Feb/11 ]

since there seems to be enough of interest, let's try to squeeze this improvement in the next release.

Comment by Michael Mühlebach [ 04/Nov/15 ]

Given the thousands of other issues we have open that are more highly requested, we won't be able to address this issue in the foreseeable future. Instead we will focus on issues with a higher impact, and more votes.
Thanks for taking the time to raise this issue. As you are no doubt aware this issue has been on our backlog for some time now with very little movement.
I'm going to close this to set expectations so the issue doesn't stay open for years with few updates. If the issue is still relevant please feel free to reopen it or create a new issue.

Generated at Mon Feb 12 03:41:39 CET 2024 using Jira 9.4.2#940002-sha1:46d1a51de284217efdcb32434eab47a99af2938b.