Re: question about layouts
Takács János <[email protected]>
| Newsgroups | gmane.comp.web.freemarker.user |
|---|---|
| Message-ID | <[email protected]> |
2011/1/8 Daniel Dekany <[email protected]>: > Saturday, January 8, 2011, 7:00:16 AM, Takács János wrote: > >> Hi >> >> I created layouts based on this article: >> http://wendefer.blogspot.com/2007/09/handy-tip-for-layouts-in-freemarker.html >> >>>From my web application, I have to serve a basic layout (with menus, >> header, footer, etc.), and a plain layout (plain html, with some basic >> javascript includes). >> The user can visit the webpage, in this case he gets the basic layout, >> or he can access some pages trough a .NET application, in wich case >> the app just loads Internet Explorer in a frame, and passes a GET >> parameter to the page url. >> In the spring mvc controller class, I have to decide the layout, based >> on this GET param. >> So what I did is this: I created multiple templates, page1.ftl, >> page1_basic.ftl, and page1_plain.ftl >> >> page1_basic.ftl contains only this: >> <@layout.basic> >> <#include "page1.ftl" /> >> </@layout.basic> >> >> and page1_plain.ftl is this: >> <@layout.plain> >> <#include "page1.ftl" /> >> </@layout.plain> >> >> And in the spring class, I pass the name of the view according to the >> GET parameter. This way I have to create 3 files for every page. >> My question is, is there a simpler way to do this? > > For example, you could have a single template like this: > > <@layout[layoutName]> > <#include pageName + ".ftl"> > </@> > > then just call this templates with layoutName and pageName in the > data-model set based on that HTTP request parameter. > > -- > Best regards, > Daniel Dekany > > > ------------------------------------------------------------------------------ > Gaining the trust of online customers is vital for the success of any company > that requires sensitive data to be transmitted over the Web. Learn how to > best implement a security strategy that keeps consumers' information secure > and instills the confidence they need to proceed with transactions. > http://p.sf.net/sfu/oracle-sfdevnl > _______________________________________________ > FreeMarker-user mailing list > [email protected] > https://lists.sourceforge.net/lists/listinfo/freemarker-user > Thanks for the answer. For the list archive, the working solution is this: layouts.ftl: <#import "includes.ftl" as layout/> <#if (! layoutName??) || (! layout[layoutName]??)> <#assign layoutName="basic"/> </#if> <#if ! pageName??> <#assign pageName="page1"/> </#if> <@layout[layoutName]> <#include pageName+".ftl"/> </@> This has to be in a separate file, so it can be included as a namespace called "layout". includes.ftl: <#include "basic.ftl" /> <#include "plain.ftl" /> plain.ftl: <#macro plain> <html><head></head><body> <#nested/> </body> </#macro> basic.ftl has the same structure with some fancy html additions. I also found a spring specific issue. I had to comment out this line in mvc-config.xml: <mvc:view-controller path="/" view-name="page1"/> I was getting freemarker errors, because my model was empty. As it turns out, this thing does no redirection, it just renders the view named "page1" with an empty model. Thanks again. -- Janos Takacs [email protected] ------------------------------------------------------------------------------ Gaining the trust of online customers is vital for the success of any company that requires sensitive data to be transmitted over the Web. Learn how to best implement a security strategy that keeps consumers' information secure and instills the confidence they need to proceed with transactions. http://p.sf.net/sfu/oracle-sfdevnl _______________________________________________ FreeMarker-user mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/freemarker-user