You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@click.apache.org by Prem Kurian Philip <pr...@songbirdtech.com> on 2009/08/11 08:23:43 UTC
templateExists function
Hi,
I am trying to use the #templateExists velocity function to determine if a
template exists before calling the #parse function. I am getting errors
when I tried this:
#if (#templateExists("$context/assets/organizations/$id/css/styles.css"))
#parse("$context/assets/organizations/$id/css/styles.css")
#end
The error message I am receiving is this:
"Was expecting one of: "[" , "{" , "(" , <WHITESPACE> ,
<STRING_LITERAL> , "true" , "false" , <INTEGER_LITERAL> ,
<FLOATING_POINT_LITERAL> , <IDENTIFIER> , "{" , <LOGICAL_NOT>"
I would appreciate some information on how to solve this. I also tried
resourceExists with similar results.
I am using Click 1.5.
Thank you,
Prem
Re: templateExists function
Posted by Frederic Daoud <fd...@proinbox.com>.
> Thanks a lot for your reply. I am using stuff like this in the styles.css
> file:
>
> .logo {background-image:
> url(${context}/assets/organizations/$organizationID/images/logo.gif);}
>
> I am using the $context and $organizationID variables in the file. This
> will need to be parsed and so I would need to use #parse and not
> #include.
> Am I right?
Yes, that is correct. So, you'll need to go through your styles.css file
and escape any non-Velocity code (probably mostly \#).
Cheers,
Freddy
Re: templateExists function
Posted by Prem Kurian Philip <pr...@songbirdtech.com>.
> What is in styles.css? Since you are parsing it as a Velocity template,
> you need to escape anything that's not meant to be Velocity code, for
> example:
>
> #header { color: #ffffff; }
>
> should be
>
> \#header { color: \#ffffff; }
>
> If nothing at all in styles.css is Velocity code, then you should use
> #include instead and you won't have to escape anything.
>
> #if
> (#templateExists("$context/assets/organizations/$id/css/styles.css"))
> #include("$context/assets/organizations/$id/css/styles.css")
> #end
>
Freddy,
Thanks a lot for your reply. I am using stuff like this in the styles.css
file:
.logo {background-image:
url(${context}/assets/organizations/$organizationID/images/logo.gif);}
I am using the $context and $organizationID variables in the file. This
will need to be parsed and so I would need to use #parse and not #include.
Am I right?
Thanks,
Prem
Re: templateExists function
Posted by Prem Kurian Philip <pr...@songbirdtech.com>.
> Had a chance to try it out. The way Velocity accesses static methods
> is described here:
>
> http://velocity.apache.org/engine/releases/velocity-1.6.2/developer-guide.html#supportforstaticclasses
>
> For Click you can do something like:
>
> public void onInit() {
> addModel("velocity", Velocity.class);
> }
>
> Then in your template you can access the resourceExists method as follows:
>
> #if ($velocity.resourceExists("/xyz.htm"))
> #parse(...)
> #end
>
> That said, the resourceExists method didn't work for me (not sure
> why), so I ended up writing my own method:
>
> public class Util {
> public static void resourceExists(String resourceName) {
> try {
> Context context = Context.getThreadLocalContext();
>
> // First check on the servlet context path
> boolean hasTemplate =
> context.getServletContext().getResource(resourceName) != null;
> if (!hasTemplate) {
>
> // Second check on the classpath
> hasTemplate = ClickUtils.getResource(resourceName,
> Util.class) != null;
> }
>
> } catch (MalformedURLException e) {
> throw new RuntimeException(e);
> }
>
> return hasTemplate;
> }
>
> You can use it as follows:
>
> public void onInit() {
> addModel("util", Util.class);
> }
>
> Hope this helps.
>
Bob, thanks a lot! I haven't tried this out yet though.
Regards,
Prem
Re: templateExists function
Posted by Bob Schellink <sa...@gmail.com>.
Had a chance to try it out. The way Velocity accesses static methods
is described here:
http://velocity.apache.org/engine/releases/velocity-1.6.2/developer-guide.html#supportforstaticclasses
For Click you can do something like:
public void onInit() {
addModel("velocity", Velocity.class);
}
Then in your template you can access the resourceExists method as follows:
#if ($velocity.resourceExists("/xyz.htm"))
#parse(...)
#end
That said, the resourceExists method didn't work for me (not sure
why), so I ended up writing my own method:
public class Util {
public static void resourceExists(String resourceName) {
try {
Context context = Context.getThreadLocalContext();
// First check on the servlet context path
boolean hasTemplate =
context.getServletContext().getResource(resourceName) != null;
if (!hasTemplate) {
// Second check on the classpath
hasTemplate = ClickUtils.getResource(resourceName,
Util.class) != null;
}
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
return hasTemplate;
}
You can use it as follows:
public void onInit() {
addModel("util", Util.class);
}
Hope this helps.
kind regards
bob
On Thu, Aug 13, 2009 at 2:24 PM, Prem Kurian
Philip<pr...@songbirdtech.com> wrote:
>> I haven't tested myself but isn't the templateExists function part of
>> the Velocity class?
>>
>> So you should do this instead:
>>
>> #if (Velocity.templateExists("empty.htm")
>> #end
>>
>> Also it seems as if templateExists has been deprecated in favor of
>> resourceExists.
>>
>
> Bob, thanks a lot for taking the time to reply.
>
> I did actually try this earlier (Velocity.templateExists as well as
> Velocity.resourceExists) and I was still getting the same parse error.
>
> I tried this:
> #if (Velocity.templateExists("template.htm") == true)
> #parse("template.htm")
> #end
>
> As well as this:
>
> #if (Velocity.resourceExists("template.htm") == true)
> #parse("template.htm")
> #end
>
> The error I am getting: Page Parsing Error
>
> Source: /border-template.htm
> Message: Was expecting one of: "[" , "{" , "(" , <WHITESPACE> ,
> <STRING_LITERAL> , "true" , "false" , <INTEGER_LITERAL> ,
> <FLOATING_POINT_LITERAL> , <IDENTIFIER> , "{" , <LOGICAL_NOT>
>
> Regards,
> Prem
>
>
--
http://incubator.apache.org/click/
Re: templateExists function
Posted by Prem Kurian Philip <pr...@songbirdtech.com>.
> I haven't tested myself but isn't the templateExists function part of
> the Velocity class?
>
> So you should do this instead:
>
> #if (Velocity.templateExists("empty.htm")
> #end
>
> Also it seems as if templateExists has been deprecated in favor of
> resourceExists.
>
Bob, thanks a lot for taking the time to reply.
I did actually try this earlier (Velocity.templateExists as well as
Velocity.resourceExists) and I was still getting the same parse error.
I tried this:
#if (Velocity.templateExists("template.htm") == true)
#parse("template.htm")
#end
As well as this:
#if (Velocity.resourceExists("template.htm") == true)
#parse("template.htm")
#end
The error I am getting: Page Parsing Error
Source: /border-template.htm
Message: Was expecting one of: "[" , "{" , "(" , <WHITESPACE> ,
<STRING_LITERAL> , "true" , "false" , <INTEGER_LITERAL> ,
<FLOATING_POINT_LITERAL> , <IDENTIFIER> , "{" , <LOGICAL_NOT>
Regards,
Prem
Re: templateExists function
Posted by Bob Schellink <sa...@gmail.com>.
Hi Prem,
I haven't tested myself but isn't the templateExists function part of
the Velocity class?
So you should do this instead:
#if (Velocity.templateExists("empty.htm")
#end
Also it seems as if templateExists has been deprecated in favor of
resourceExists.
Hope this helps.
kind regards
bob
On Tue, Aug 11, 2009 at 6:12 PM, Prem Kurian
Philip<pr...@songbirdtech.com> wrote:
> The problem is not with the #parse command but rather that I cannot get
> the template to render if I use the #templateExists function call within
> the template.
>
> Example: This generates a Page Parsing error:
>
> #if (#templateExists("empty.htm"))
> Empty template
> #end
>
> Here I am having a completely empty file (empty.htm) with nothing in it.
> So the problem is with the call to templateExists.
>
> Regards,
> Prem
>
>
--
http://incubator.apache.org/click/
Re: templateExists function
Posted by Prem Kurian Philip <pr...@songbirdtech.com>.
The problem is not with the #parse command but rather that I cannot get
the template to render if I use the #templateExists function call within
the template.
Example: This generates a Page Parsing error:
#if (#templateExists("empty.htm"))
Empty template
#end
Here I am having a completely empty file (empty.htm) with nothing in it.
So the problem is with the call to templateExists.
Regards,
Prem
Re: templateExists function
Posted by Frederic Daoud <fd...@proinbox.com>.
Hi Prem,
> #if (#templateExists("$context/assets/organizations/$id/css/styles.css"))
> #parse("$context/assets/organizations/$id/css/styles.css")
> #end
>
> The error message I am receiving is this:
>
> "Was expecting one of: "[" , "{" , "(" , <WHITESPACE> ,
> <STRING_LITERAL> , "true" , "false" , <INTEGER_LITERAL> ,
> <FLOATING_POINT_LITERAL> , <IDENTIFIER> , "{" , <LOGICAL_NOT>"
What is in styles.css? Since you are parsing it as a Velocity template,
you need to escape anything that's not meant to be Velocity code, for
example:
#header { color: #ffffff; }
should be
\#header { color: \#ffffff; }
If nothing at all in styles.css is Velocity code, then you should use
#include instead and you won't have to escape anything.
#if
(#templateExists("$context/assets/organizations/$id/css/styles.css"))
#include("$context/assets/organizations/$id/css/styles.css")
#end
You can also assign the path to a variable to avoid repeating it, if
you wish.
#set ($css = "$context/assets/organizations/$id/css/styles.css")
#if (#templateExists($css))
#include($css)
#end
Hope that helps.
Cheers,
Freddy