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-L_LENzX5WGS8VAo=e2JmZA8xVuBmn-nRvT5cQWFGn59w@mail.gmail.com> |
On Fri, May 11, 2012 at 5:56 AM, S Ahmed <[email protected]> wrote: > But do I have to pass in the variable somehow? > > <@p.master title=${title}> > > Or does the macro just know that that variable exists? Everything you set up in your ModelAndView will be globally available in your freemarker template, and in every other template it includes. You don't have to pass them to macros as parameters. > > Also, how do I check for not null and not empty? > > <#if title??> > </#if> > > that checks for null only, what if I want to know if it is an empty string? > ie.. "" > That's easy: <#if (title??) || (title=="") > This will first check for null, then for an empty string. > > Is it possible to have an object as a parameter in a macro? like a > something like user? > > <@macro page user> > > where user has: > > user.name > user.age > > ? The short answer is yes. For the longer explanation, i'd like to direct your attention to this page: http://freemarker.sourceforge.net/docs/pgui_datamodel_objectWrapper.html You can add a simple HashMap to your ModelAndView: HashMap<String,String> asdf=new HashMap<String,String>(); asdf.put( "name","Joe"); asdf.put( "age","Twenty"); mav.addObject( "user",asdf); After this, in your template ${user} will be an object, you can access it's properties like this: ${user.name}, ${user.age} > > On Thu, May 10, 2012 at 11:54 PM, S Ahmed <[email protected]> wrote: >> >> But do I have to pass in the variable somehow? >> >> <@p.master title=${title}> >> >> Or does the macro just know that that variable exists? >> >> >> On Thu, May 10, 2012 at 2:32 AM, Takács János <[email protected]> wrote: >>> >>> 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 >> >> > > > ------------------------------------------------------------------------------ > 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 [email protected] +36705065248 ------------------------------------------------------------------------------ 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