View Full Version : How to get meta data in Freemarker + Sitemesh?
hkadmin
Jan 30th, 2008, 10:16 PM
Hello,
I am using Sitemesh for my header decorator, with Freemarker template,
As I want to get the meta data from freemarker to sitemesh decorator...
I have follow the example base on sitemesh offical site, but it still throw exception...
My template .ftl like
<html>
<meta name="foo" content="bar">
<head>
<body>
I want to get the meta data from sitemesh decorator....
${page.properties.meta.foo}
Unfortunately, it throw following exception
Expression page.properties.meta is undefined on line 4, column 12 in ...
The problematic instruction:
----------
==> ${page.properties.meta.foo} [on line 4, column 10 in WEB
Anyone can help? I aplogy for my poor english, but it is quite important for me. Many thanks!
imilina
Jan 31st, 2008, 03:47 AM
I understand that you use freemarker for both your views and for sitemesh template, right?
First, I've used meta definitions only for applying special decorators for some pages - never tried to access meta data directly.
What are you trying to do:
1. Define meta data in your freemarker view, and access it in your sitemesh template?
or
2. Define meta data in your sitemesh template, and access it in your freemarker view?
I think #1 should work, but #2 will surely not work - sitemesh filter will first let your servlet to render response (view), and then insert this content into decorator. That means that inside your view you cannot access anything defined in decorator, as decorator is not yet processed. The other way around should work, though.
Regards,
Igor.
hkadmin
Jan 31st, 2008, 08:06 AM
I understand that you use freemarker for both your views and for sitemesh template, right?
First, I've used meta definitions only for applying special decorators for some pages - never tried to access meta data directly.
What are you trying to do:
1. Define meta data in your freemarker view, and access it in your sitemesh template?
or
2. Define meta data in your sitemesh template, and access it in your freemarker view?
I think #1 should work, but #2 will surely not work - sitemesh filter will first let your servlet to render response (view), and then insert this content into decorator. That means that inside your view you cannot access anything defined in decorator, as decorator is not yet processed. The other way around should work, though.
Regards,
Igor.
Thanks, Igor
I am using the first approach, define meta data in freemarker view, and access in sitemesh decorator
because i want to use a script in my sitemesh template, the meta data passed by the template view will control the on/off for the script.
For an example, if one of my freemarker view contain meta content=Yes, on the same time, my sitemesh template get the meta content, so a script will turn on in my sitemesh head.
${page.properties.meta.foo} <--- Is it the right syntax to display meta data in sitemesh template view?!
Many Thanks!
imilina
Jan 31st, 2008, 11:13 AM
${page.properties.meta.foo} <--- Is it the right syntax to display meta data in sitemesh template view?!
Not quite. Try this:
${page.properties["meta.foo"]}
Regards.
jandrla
Jan 31st, 2008, 01:01 PM
Hi,
I don't know if this could help, it works when it's used inside JSP, I presume it's similar with Freemarker:
<html>
<meta name="foo" content="bar">
<head>
<body>
In decorator:
<decorator:usePage id="currentPage" />
<% String fooMetaData = currentPage.getProperty("meta.foo");
pageContext.setAttribute("foo", fooMetaData); %>
So you can use it with:
${foo}
I know it's ugly scriptlet, but I didn't find better solution.
Hope this helps.
Regards
hkadmin
Jan 31st, 2008, 08:36 PM
Not quite. Try this:
${page.properties["meta.foo"]}
Regards.It works, Many Thanks!
hkadmin
Jan 31st, 2008, 08:58 PM
Hi,
I don't know if this could help, it works when it's used inside JSP, I presume it's similar with Freemarker:
<html>
<meta name="foo" content="bar">
<head>
<body>
In decorator:
<decorator:usePage id="currentPage" />
<% String fooMetaData = currentPage.getProperty("meta.foo");
pageContext.setAttribute("foo", fooMetaData); %>
So you can use it with:
${foo}
I know it's ugly scriptlet, but I didn't find better solution.
Hope this helps.
Regards
Thank you for your reply, your syntax is work in JSP, i have tried it before~
:rolleyes:
vBulletin® v3.7.3, Copyright ©2000-2008, Jelsoft Enterprises Ltd.