[MAGNOLIA-5824] Reenable defining freemarker shared variables by configuration Created: 02/Jul/14  Updated: 05/Aug/14  Resolved: 05/Aug/14

Status: Closed
Project: Magnolia
Component/s: templating
Affects Version/s: 5.0
Fix Version/s: 5.3.2

Type: Bug Priority: Major
Reporter: Zdenek Skodik Assignee: Christopher Zimmermann
Resolution: Fixed Votes: 0
Labels: support
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: Text File TestSharedVariables.java     Text File TestSharedVariables2.java     XML File config.server.rendering.freemarker.sharedVariables.xml     XML File templates.templating-kit.components.teasers.contact.xml    
Issue Links:
relation
is related to MAGNOLIA-3087 Allow configuration of shared variabl... 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
Release notes required:
Yes
Date of First Response:

 Description   

/server/rendering/freemarker/sharedVariables/test/class=info.magnolia.templating.functions.TemplatingFunctions is no longer accepted.

References: http://freemarker.org/docs/pgui_config_sharedvariables.html
Original implementation: http://jira.magnolia-cms.com/browse/MAGNOLIA-3087



 Comments   
Comment by Christopher Zimmermann [ 24/Jul/14 ]

Some first results:
In FreemarkerConfig, if you remove the 'new" setSharedVariables added with update to Node2Bean, then everything works fine as it did before.
So what is the issue with the node2Bean set* methods?

In Node2BeanTransformer #setProperty, dscr.getWriteMethod() is true, that is the setSharedVariables method.
The 'value' variable holds the configured sharedVariables until 'filterOrConvertCollectionsAndMaps(dscr, value);'
which then sets value to an empty map {}.

(Whereas when there is no set* method, then the code forks into the "dscr.getAddMethod() != null" section instead which does not run the filterOrConvertCollectionsAndMaps conversion.)

So in a nutshell, node2bean is stricter and the supplied classes must strictly match the configured description.

*

Given that, I tried making my test TestSharedVariables class implement the proper interface 'freemarker.template.TemplateModel'. After I did that, the node2Bean setSharedVariables method appeared to work properly and everything was great until I tried to actually render them in freemarker by calling the classes method as before:
${testShared.testMethod()}

But now I get this exception:
Expected hash. testShared evaluated instead to info.magnolia.templating.functions.TestSharedVariables on line 28, column 15 in templating-kit/components/teasers/contact.ftl.
The problematic instruction:
----------
==> ${testShared.testMethod()} [on line 28, column 13 in templating-kit/components/teasers/contact.ftl]
----------

Comment by Jan Haderka [ 25/Jul/14 ]

seems to me like this is due to difference in method signature:
public void addSharedVariable(String name, Object value) throws TemplateModelException {}
vs.
public void setSharedVariables(Map<String, TemplateModel> sharedVariables) {}

changing setter method to <String, Object> and adding transformation called in the add method should fix the problem as well.

Comment by Christopher Zimmermann [ 29/Jul/14 ]

Jan's suggestion above was the solution to the problem. So the fix is in the FreemarkerConfig#setSharedVariables method.
Added test.

To facilitate QA/Review Ive attached a few files:
Add the java files to your project (in magnolia-templating info/magnolia/templating/functions)
Import the sharedVariables.xml in config app
Import the contact.xml in the templates app.

Then browse to the /demo-project/news-and-events page.
Before the fix, you'll see a freemarker error, with the fix you should see some extra text in the contact teaser.

Comment by Christopher Zimmermann [ 05/Aug/14 ]

Removed accidentally added classes:
magnolia-templating/src/main/java/info/magnolia/templating/functions/TestSharedVariables.java
magnolia-templating/src/main/java/info/magnolia/templating/functions/TestSharedVariables2.java

Generated at Mon Feb 12 04:08:44 CET 2024 using Jira 9.4.2#940002-sha1:46d1a51de284217efdcb32434eab47a99af2938b.