Re: A WM brain bender
Keats Kirsch <[email protected]>
| Newsgroups | gmane.comp.java.webmacro.user |
|---|---|
| Message-ID | <[email protected]> |
Looking at the test case in CVS (
http://cvs.sourceforge.net/viewcvs.py/webmacro/webmacro/test/unit/org/webmacro/template/TestEvalTemplet.java?rev=1.1&view=markup)
I see that I created a convenience class for doing this during testing:
//** Convenience class to evaluate a string template as a Macro *//
*public* *class* StringMacro *implements* org.webmacro.Macro
{
*private* String s = *""*;
*public* StringMacro(String text)
{
s = text;
}
*public* Object evaluate(Context context) *throws*
org.webmacro.PropertyException
{
org.webmacro.engine.StringTemplate t = *new*
org.webmacro.engine.StringTemplate(context.getBroker(), s); *return*
t.evaluateAsString(context);
}
*public* *void* write(org.webmacro.FastWriter fw, Context context) { }
}
This let's you use arbitrary strings as macros, e.g.,
StringMacro m = *new* StringMacro(*"$A $B $C"*);
I think the $Template tool can be used similarly.
Keats
Keats Kirsch wrote:
> It's been a long time, but I'm fairly certain you can do this with the
> #templet/#eval mechanism. I'll try to dredge up my memories of how it
> works. There is also a $template tool that can do this:
> org.webmacro.servlet.TemplateTool.
>
> Keats
>
> Marc Palmer wrote:
>
>> Alex Twisleton-Wykeham-Fiennes wrote:
>>
>>> On Friday 11 March 2005 15:57, Marc Palmer wrote:
>>>
>>>>> Two questions:-
>>>>>
>>>>> 1. what package did you put your StringTemplateLoader class into?
>>>>
>>>>
>>>>
>>>> package org.webmacro.resource;
>>>
>>>
>>>
>>>
>>> OK. maybe put the following into your StringTemplateLoader:-
>>
>>
>>
>> Yep it is loading, and setConfig gets called.
>>
>> However load() is never called.
>>
>> More info - my content in the previous example was empty. I changed
>> the template to do this:
>>
>>
>> #include as template "string:testing"
>>
>> And the error is:
>>
>> 16:15:38 resource WARNING BrokerTemplateProvider: Template
>> not found: string:testing
>> org.webmacro.ignition.cms.ViewEngineException:
>> org.webmacro.PropertyException: #include string:testing: Not found by
>> template provider
>>
>> So for some reason the string: handler is being overlooked and its
>> just using the Broker which of course fails.
>>
>> DelegatingTemplateProvider.load is not being called either. Odd.
>>
>> Src:
>>
>> public class StringTemplateLoader extends AbstractTemplateLoader
>> {
>> public final static String PROTOCOL = "string:";
>>
>> public void setConfig(String config)
>> {
>> }
>>
>> public Template load(String query,
>> CacheElement ce)
>> throws ResourceException
>> {
>> if (!query.startsWith(PROTOCOL))
>> {
>> return null;
>> }
>> return new StringTemplate(broker,
>> query.substring(PROTOCOL.length()));
>> }
>> }
>>
>> Cheers
>>
>>
>
>
>
>
> -------------------------------------------------------
> 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
> _______________________________________________
> Webmacro-user mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/webmacro-user
>
-------------------------------------------------------
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