Re: how to pass value from code to page template

Takács János <[email protected]>
Newsgroups gmane.comp.web.freemarker.user
Message-ID <CAFt-u-+WFUT6_GaaQmafA13gM-gB6RjF5vbBukrRe1yF45OqcQ@mail.gmail.com>
your controller should look something like this:

@Controller
@Scope("request")
public class IndexController {
        @RequestMapping(value="/index",method=RequestMethod.GET)
        public ModelAndView html(){
                ModelAndView mav = new ModelAndView();
                mav.setViewName( "index.ftl");
                // this is how you pass a value from java to freemarker:
                mav.addObject( "title","Index Page");
                return mav;
        }
}

In your template, ${title} will have the value "Index Page", you can
use it included templates as well as in index.ftl.

page.ftl:

<#macro page>
<#if ! title??>
        <#assign title="Default Title"/>
</#if>
 <html>
   <title>${title}</title>
 </html>
 <body>
 ..
 <#nested>
 </body>
 </#macro>


On Thu, May 10, 2012 at 6:20 AM, S Ahmed <[email protected]> wrote:
> I have a page template that I use for all my pages like:
>
> <#macro page title="soem title">
> <html>
>   <title>${title}</title>
> </html>
> <body>
> ..
> <#nested>
>
> </body>
>
> </#macro>
>
> Then in my page I am doing:
>
> <#import "/page.ftl" as p>
>
> <@p.page title="some title">
>
> ...
>
>
> What if I wanted to get the title from my java code, and pass it down, is
> that possible?
>
> I'm using spring, and my view gets the ModelAndView values, can I set it
> from the server side and pass it as a parameter for title?
>
> ------------------------------------------------------------------------------
> Live Security Virtual Conference
> Exclusive live event will cover all the ways today's security and
> threat landscape has changed and how IT managers can respond. Discussions
> will include endpoint security, mobile security and the latest in malware
> threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
> _______________________________________________
> FreeMarker-user mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/freemarker-user
>



-- 

Takács János

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
FreeMarker-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.