Re: A WM brain bender
Alex Twisleton-Wykeham-Fiennes <[email protected]>
| Newsgroups | gmane.comp.java.webmacro.user |
|---|---|
| Message-ID | <[email protected]> |
On Friday 11 March 2005 13:41, Alex Twisleton-Wykeham-Fiennes wrote:
<Snip>
> > It's a bit confusing. I'd like to not have to write a helper/directive
> > to load a Macro tempalte from a String unless I have to...
>
> Would it not be possible to have an implementation of TemplateProvider that
> used the name of the template as the contents of the template itself?
>
> This would let you say:-
>
> #include as template "<p>Hello, $name</p>"
>
> or
>
> #include as template "$templateSource"
>
> Of course, this would rather cripple your normal template loading, so there
> could be a switch which looked for a given header such as:-
>
> #include as template "evaluate:$templateSource"
>
> and if it didn't start with "evaluate:" then it would just delegate the
> request to the default template loader.
Actually I was a bit wrong. What you want is a TemplateLoader. Here is proof
of concept:-
package org.cord.node;
import org.webmacro.*;
import org.webmacro.engine.*;
import org.webmacro.resource.*;
public class EvaluatingTemplateLoader
extends AbstractTemplateLoader
{
public void setConfig(String config)
{
}
public Template load(String query,
CacheElement ce)
throws ResourceException
{
return new StringTemplate(broker,
query);
}
}
Then put this in your template evaluater:-
TemplateLoaderPath.n=evaluate:
TemplateLoader.evaluate=org.cord.node.EvaluatingTemplateLoader
where n is higher than any other TemplateLoaderPath to ensure that it doesn't
jump on other templates.
and then you can do:-
#include as template "<p>Hello, \$name</p>"
note that the escape of the $ in the passed template is necessary to prevent
it getting prematurely evaluated...
You can now do the same thing off variables like so:-
#set $templateSource = "<p>Hello, \$name</p>"
#include as template "$templateSource"
and this all works fine...
Alex
ps thought that it would be funky to be able to chain these like so:-
#include as template "#include as text "<normalTemplatePath>""
but WebMacro obviously thinks that this is on crack...
-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users.
Discover which products truly live up to the hype. Start reading now.
http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click