Hi Ernst!

Also seems like a good solution!

IIRC the BeansWrapper does not handle the wrapping of Content objects.
You may want something like:
...
wrapper = Components.getSingleton(FreemarkerConfig.class).getObjectWrapper();   
wrapper.wrap(myobj);
...
If you have any methods/fields of type Content.

Regards from Vienna,

Richard

-----Ursprüngliche Nachricht-----
Von: [email protected] [mailto:[email protected]] 
Im Auftrag von Ernst Bunders
Gesendet: Mittwoch, 15. Juni 2011 16:55
An: Magnolia User-List
Betreff: Re: [magnolia-user] freemarker sharedVariables and static methods


hello Richard

I already have a class with tons of static methods that return strings, 
collections, content nodes, whatnot.
To wrap all that including the method result I find a bit daunting, especially 
because the previously described method will take care of it nicely. But I 
don't know how to use that in combination with the sharedVariables feature.

For now I just created an extension to FreemarkerHelper to fix it:


public class VtkFreemarkerHelper extends FreemarkerHelper {
    @Override
    protected void addDefaultData(Map<String, Object> data, Locale locale, 
String i18nBasename) {
        super.addDefaultData(data, locale, i18nBasename);
        BeansWrapper wrapper = BeansWrapper.getDefaultInstance();
        TemplateHashModel staticModels = wrapper.getStaticModels();
        try {
            TemplateHashModel vtkUtilStatics =
                (TemplateHashModel)
staticModels.get("nl.vpro.magnolia.module.vprotemplatingkit.util.VtkUtil");
            data.put("vtkUtil", vtkUtilStatics);
        } catch (TemplateModelException e) {
            e.printStackTrace();
        }
    }
}

2011/6/15 Unger, Richard <[email protected]>:
>
> Hi Ernst!
>
> I use the sharedVariables to access my own objects from freemarker templates.
>
> It is working well.
>
> All the objects I set as shared vars are subclasses of 
> TemplateDirectiveModel, since I want to use them as directives.
>
> If you have a class you want to use that does not implement one of the model 
> methods, why not create a delegate object to wrap the class you actually want 
> to share?
>
> Regards from Vienna,
>
> Richard
>
>
>
>
> -----Ursprüngliche Nachricht-----
> Von: [email protected] 
> [mailto:[email protected]] Im Auftrag von Ernst Bunders
> Gesendet: Mittwoch, 15. Juni 2011 15:06
> An: Magnolia User-List
> Betreff: [magnolia-user] freemarker sharedVariables and static methods
>
>
> hi
>
> I have a class with a bunch of static methods that I want to access from 
> freemarker templates.
> According to 
> http://freemarker.sourceforge.net/docs/pgui_misc_beanwrapper.html#auto
> id_55
> I can do something like
>
> BeansWrapper wrapper = BeansWrapper.getDefaultInstance();
> TemplateHashModel staticModels = wrapper.getStaticModels(); 
> TemplateHashModel fileStatics =
>    (TemplateHashModel) staticModels.get("java.io.File");
>
> for my class and that's it.
>
> But the shardVariables map simply maps names to classes that can be 
> instantiated as TemplateModel.
>
> It seems that what i actually need is a kind of factory or factory method 
> that will create the TemplateModel instance, in stead of straight 
> instantiation.
>
> Is there something I miss, or is it simply not possible with the current 
> setup?
>
> I also found  http://jira.magnolia-cms.com/browse/MAGNOLIA-3152
> I think this issue addresses the same problem. Can I assume that the fact 
> this issue is still open means that some work is still needed to make this 
> possible?
>
> If so, I can extend FreemarkerHelper, but I rather don't if not needed.
>
> regards,
>
>
> --
> Ernst Bunders
> Ontwikkelaar VPRO
>
>
> ----------------------------------------------------------------
> For list details see
> http://www.magnolia-cms.com/home/community/mailing-lists.html
> To unsubscribe, E-mail to: <[email protected]>
> ----------------------------------------------------------------
>
>
>
>
>
> ----------------------------------------------------------------
> For list details see
> http://www.magnolia-cms.com/home/community/mailing-lists.html
> To unsubscribe, E-mail to: <[email protected]>
> ----------------------------------------------------------------
>
>



--
Ernst Bunders
Ontwikkelaar VPRO


----------------------------------------------------------------
For list details see
http://www.magnolia-cms.com/home/community/mailing-lists.html
To unsubscribe, E-mail to: <[email protected]>
----------------------------------------------------------------





----------------------------------------------------------------
For list details see
http://www.magnolia-cms.com/home/community/mailing-lists.html
To unsubscribe, E-mail to: <[email protected]>
----------------------------------------------------------------

Reply via email to