Re: Possible room for performance improvement when using TemplateDirectiveModel and TemplateMethodModelEx

Daniel Dekany <[email protected]>
Newsgroups gmane.comp.web.freemarker.user
Message-ID <[email protected]>
Saturday, January 15, 2011, 1:06:26 AM, Daniel Henrique Alves Lima wrote:

>         Hi, Daniel. Thanks for your answers.
>
>
> On Fri, 2011-01-14 at 11:29 +0100, Daniel Dekany wrote:
>> You could avoid all the re-instantiations of the
>> DynamicTagLibDirective if you just put them into the Configuration as
>> a shared hash variable "g" (see setSharedVariable), which stores the
>> DynamicTagLibDirective objects. Thus these objects will be created
>> only once during the whole application life-cycle (when you set up the
>> Configuration object). 
>
>         Will this also work for functions/methods (besides directives)?

TemplateMethodModelEx pretty much corresponds to FTL functions, so
yes, it will. (Isn't that how you create them now?) (Note that you can
also have objects that implement both TemplateMethodModelEx and
TemplateDirectiveModel on the same time.)

(But, of course, if any of these Grails-specific objects are bound to
a HTTP Request or HTTP Session, then you can't just create them on the
application level.)

>> Also then auto-importing is not needed. OTOH
>> then a "g" in the data-model will shadow the "g" shared variable. (To
>> work that around, you can also add "g" with an additional name, like
>> "grails_tags", so you can say <#assign g = grails_tags> in the
>> template, hence shadowing the clashing "g" in the data-model. More
>> tricky than an import, but then, this accident will also occur
>> rarely.)
>> 
>> 
>
>         About this, even when i use
>
> [#assign message
> ="org.codehaus.groovy.grails.plugins.freemarker.DynamicTagLibDirective"?new("g", "message")]
>
> (...)
>
> [#import "g.ftl" as g]
>
>         If the script tries to change the value of "g"
>
> [#assign g={"message": "anything"}]
>
>         Won't i lost its original value?

You will lose it. However here the thing that overrides "g" and the
affected part of the source code that wants to use "g" are in the same
file, which is much less worrisome than if something that comes from
outside hides "g".

>
>         Thanks in advance!

-- 
Best regards,
 Daniel Dekany


------------------------------------------------------------------------------
Protect Your Site and Customers from Malware Attacks
Learn about various malware tactics and how to avoid them. Understand 
malware threats, the impact they can have on your business, and how you 
can protect your company and customers by using code signing.
http://p.sf.net/sfu/oracle-sfdevnl
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.