[BLOSSOM-2] Expose spring macro helper and the model as request attributes in freemarker templates Created: 01/Jul/10  Updated: 06/Dec/14  Resolved: 15/Aug/13

Status: Closed
Project: Blossom
Component/s: None
Affects Version/s: None
Fix Version/s: 2.0.2, 3.0

Type: New Feature Priority: Minor
Reporter: Tobias Mattsson Assignee: Tobias Mattsson
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: Text File ExtendedFreemarkerParagraphViewRenderer.java     Text File ExtendedFreemarkerTemplateViewRenderer.java     Text File ExtendedFreemarkerTemplateViewRenderer20.java    
Template:
Acceptance criteria:
Empty
Date of First Response:

 Description   

Spring's freemarker support comes with a set of macros for form binding, these rely on an attribute begin present in the freemarker model 'springMacroRequestContext'. This needs to be set by the Blossom freemarker view renderer.

Also, in order to use the form binding tag library in freemarker views it needs to see the freemarker model as request attributes. The Blossom freemarker renderer needs to expose them that way.



 Comments   
Comment by Magnolia International [ 01/Jul/10 ]

can't you do this via a custom renderer ?

Comment by Tobias Mattsson [ 02/May/11 ]

In spring.ftl is this comment that explains the requirements:

To take advantage of these macros, the "exposeSpringMacroHelpers" property
of the FreeMarker class needs to be set to "true". This will expose a
RequestContext under the name "springMacroRequestContext", as needed by
the macros in this library.

This should be set in FreemarkerParagraphViewRenderer and FreemarkerTemplateViewRenderer.

Also, Spring by default sets up a TemplateLoader that makes it possible to include the macros by simply saying <#import "spring.ftl" as spring />. It's probably a good idea to install a TemplateLoader of our own that makes the same thing possible so existing templates work seamlessly in Magnolia.

Comment by Tobias Mattsson [ 14/Mar/12 ]

Attaching updated renderers for Blossom 1.2

They're configured as:

  <bean class="info.magnolia.module.blossom.view.TemplateViewResolver">
    <property name="order" value="2"/>
    <property name="prefix" value="/WEB-INF/templates/"/>
    <property name="viewNames" value="*.ftl"/>
    <property name="viewRenderer">
      <bean class="info.magnolia.module.blossom.view.ExtendedFreemarkerTemplateViewRenderer"/>
    </property>
  </bean>

  <bean class="info.magnolia.module.blossom.view.ParagraphViewResolver">
    <property name="order" value="4" />
    <property name="prefix" value="/WEB-INF/paragraphs/"/>
    <property name="viewNames" value="*.ftl"/>
    <property name="viewRenderer">
      <bean class="info.magnolia.module.blossom.view.ExtendedFreemarkerParagraphViewRenderer"/>
    </property>
  </bean>
Comment by Tobias Mattsson [ 14/Mar/12 ]

The macros in spring.ftl can be imported as:

[#import "/org/springframework/web/servlet/view/freemarker/spring.ftl" as spring /]
Comment by Tobias Mattsson [ 16/Mar/12 ]

Attaching a version for Blossom 2.0, this is how to configure it (the only changed thing compared to blossom-servlet.xml in the sample is the class attribute that now says ExtendedFreemarkerTemplateViewRenderer20):

    <!-- Renders all views that end with .ftl -->
    <bean class="info.magnolia.module.blossom.view.TemplateViewResolver">
        <property name="order" value="3"/>
        <property name="prefix" value="/WEB-INF/templates/"/>
        <property name="viewNames" value="*.ftl"/>
        <property name="viewRenderer">
            <bean class="info.magnolia.module.blossom.view.ExtendedFreemarkerTemplateViewRenderer20">
                <property name="contextAttributes">
                    <map>
                        <entry key="cms">
                            <bean class="info.magnolia.rendering.renderer.ContextAttributeConfiguration">
                                <property name="name" value="cms"/>
                                <property name="componentClass" value="info.magnolia.templating.freemarker.Directives"/>
                            </bean>
                        </entry>
                        <entry key="cmsfn">
                            <bean class="info.magnolia.rendering.renderer.ContextAttributeConfiguration">
                                <property name="name" value="cmsfn"/>
                                <property name="componentClass" value="info.magnolia.templating.functions.TemplatingFunctions"/>
                            </bean>
                        </entry>
                    </map>
                </property>
            </bean>
        </property>
    </bean>
Comment by Tobias Mattsson [ 16/Mar/12 ]

Note that the attached files are not compatible with Spring 2.5 due to a change of API in org.springframework.web.servlet.support.RequestContext.

Comment by Jan Haderka [ 06/Dec/14 ]

Bulk close of old resolved issues.

Generated at Sun Feb 11 23:29:08 CET 2024 using Jira 9.4.2#940002-sha1:46d1a51de284217efdcb32434eab47a99af2938b.