[Freemarker-user] macro override

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

[Freemarker-user] macro override

Argyro Kazaki
Halo all,

I have two files, a.ftl and b.ftl and this is also the order in which
they are loaded.

Within a.ftl I have the following macro:

<#macro foo pojo>
    <#-- do stuff with pojo -->
</#macro>

Within b.ftl I have a macro with the same signature, which naturally
overrides the first (because b.ftl it is loaded after a.ftl):

<#macro foo pojo>
    <#-- do stuff with pojo -->
</#macro>

Is there a way to call the first macro within the latter? I am trying to
achieve something similar like overriding java methods and still being
able to call super implementation.

Is it possible to do something like this within b.ftl so that the inner
@foo call be redirected to super implementation? (of course now ends
with a StackOverflowError)

<#macro foo pojo>
    <#-- do stuff with pojo -->
    <@foo pojo />
</#macro>


Thanks,
Argyro Kazaki


------------------------------------------------------------------------------
This SF.Net email is sponsored by the Verizon Developer Community
Take advantage of Verizon's best-in-class app development support
A streamlined, 14 day to market process makes app distribution fast and easy
Join now and get one step closer to millions of Verizon customers
http://p.sf.net/sfu/verizon-dev2dev 
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Reply | Threaded
Open this post in threaded view
|

Re: [Freemarker-user] macro override

Daniel Dekany
Sunday, December 20, 2009, 7:52:07 PM, Argyro Kazaki wrote:

> Halo all,
>
> I have two files, a.ftl and b.ftl and this is also the order in which
> they are loaded.
>
> Within a.ftl I have the following macro:
>
> <#macro foo pojo>
>     <#-- do stuff with pojo -->
> </#macro>
>
> Within b.ftl I have a macro with the same signature, which naturally
> overrides the first (because b.ftl it is loaded after a.ftl):
>
> <#macro foo pojo>
>     <#-- do stuff with pojo -->
> </#macro>
>
> Is there a way to call the first macro within the latter? I am trying to
> achieve something similar like overriding java methods and still being
> able to call super implementation.

Unfortunately no. In FreeMarker macros are just variables. So when you
import b.ftl, it just like when you set the foo variable... the old
value of foo is lost. (One would think that the old variable could be
saved before its overwritten with some name like super_foo, but the
feature that allows calling macros that are defined later will
interfere with this, and it's the new macro that will be saved.)

If you are using #import, since then the macros will have a prefix, it
is possible to call the other "foo"... but then you don't have
overriding, so this is certainly not useful for you. But if the
inheritance hierarchy is static, you could <#import "a.ftl" as super>
in "b.ftl", then you can call <@super.foo pojo /> there... of course
that's very far from real inheritance for obvious reasons.

> Is it possible to do something like this within b.ftl so that the inner
> @foo call be redirected to super implementation? (of course now ends
> with a StackOverflowError)
>
> <#macro foo pojo>
>     <#-- do stuff with pojo -->
>     <@foo pojo />
> </#macro>
>
>
> Thanks,
> Argyro Kazaki
>
>
> ------------------------------------------------------------------------------
> This SF.Net email is sponsored by the Verizon Developer Community
> Take advantage of Verizon's best-in-class app development support
> A streamlined, 14 day to market process makes app distribution fast and easy
> Join now and get one step closer to millions of Verizon customers
> http://p.sf.net/sfu/verizon-dev2dev 
> _______________________________________________
> FreeMarker-user mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/freemarker-user
>

--
Best regards,
 Daniel Dekany


------------------------------------------------------------------------------
This SF.Net email is sponsored by the Verizon Developer Community
Take advantage of Verizon's best-in-class app development support
A streamlined, 14 day to market process makes app distribution fast and easy
Join now and get one step closer to millions of Verizon customers
http://p.sf.net/sfu/verizon-dev2dev 
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Reply | Threaded
Open this post in threaded view
|

Re: [Freemarker-user] macro override

Daniel Dekany
Monday, December 21, 2009, 1:06:06 AM, Daniel Dekany wrote:

> Sunday, December 20, 2009, 7:52:07 PM, Argyro Kazaki wrote:
>
>> Halo all,
>>
>> I have two files, a.ftl and b.ftl and this is also the order in which
>> they are loaded.
>>
>> Within a.ftl I have the following macro:
>>
>> <#macro foo pojo>
>>     <#-- do stuff with pojo -->
>> </#macro>
>>
>> Within b.ftl I have a macro with the same signature, which naturally
>> overrides the first (because b.ftl it is loaded after a.ftl):
>>
>> <#macro foo pojo>
>>     <#-- do stuff with pojo -->
>> </#macro>
>>
>> Is there a way to call the first macro within the latter? I am trying to
>> achieve something similar like overriding java methods and still being
>> able to call super implementation.
>
> Unfortunately no. In FreeMarker macros are just variables. So when you
> import b.ftl,

I meant, when you *include* b.ftl...

--
Best regards,
 Daniel Dekany


------------------------------------------------------------------------------
This SF.Net email is sponsored by the Verizon Developer Community
Take advantage of Verizon's best-in-class app development support
A streamlined, 14 day to market process makes app distribution fast and easy
Join now and get one step closer to millions of Verizon customers
http://p.sf.net/sfu/verizon-dev2dev 
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Reply | Threaded
Open this post in threaded view
|

Re: [Freemarker-user] macro override

Guo Du
In reply to this post by Argyro Kazaki
On Sun, Dec 20, 2009 at 6:52 PM, Argyro Kazaki <[hidden email]> wrote:
> Halo all,
>
> I have two files, a.ftl and b.ftl and this is also the order in which
> they are loaded.
A work around if you do want both macro. By introduce c.ftl (see
following sample code), then you include c.ftl instead of b.ftl in
your main ftl.

<@foo pojo /> <#--macro from a.ftl-->
<#include "b.ftl">
<@foo pojo /> <#--macro from b.ftl-->

--Guo

------------------------------------------------------------------------------
This SF.Net email is sponsored by the Verizon Developer Community
Take advantage of Verizon's best-in-class app development support
A streamlined, 14 day to market process makes app distribution fast and easy
Join now and get one step closer to millions of Verizon customers
http://p.sf.net/sfu/verizon-dev2dev 
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Reply | Threaded
Open this post in threaded view
|

Re: [Freemarker-user] macro override

Daniel Dekany
Monday, December 21, 2009, 12:37:27 PM, Guo Du wrote:

> On Sun, Dec 20, 2009 at 6:52 PM, Argyro Kazaki <[hidden email]> wrote:
>> Halo all,
>>
>> I have two files, a.ftl and b.ftl and this is also the order in which
>> they are loaded.
> A work around if you do want both macro. By introduce c.ftl (see
> following sample code), then you include c.ftl instead of b.ftl in
> your main ftl.
>
> <@foo pojo /> <#--macro from a.ftl-->
> <#include "b.ftl">
> <@foo pojo /> <#--macro from b.ftl-->
>
> --Guo

How would this work?

--
Best regards,
 Daniel Dekany


------------------------------------------------------------------------------
This SF.Net email is sponsored by the Verizon Developer Community
Take advantage of Verizon's best-in-class app development support
A streamlined, 14 day to market process makes app distribution fast and easy
Join now and get one step closer to millions of Verizon customers
http://p.sf.net/sfu/verizon-dev2dev 
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Reply | Threaded
Open this post in threaded view
|

Re: [Freemarker-user] macro override

Guo Du
On Mon, Dec 21, 2009 at 1:25 PM, Daniel Dekany <[hidden email]> wrote:
> How would this work?
Line 1: <@foo pojo /> <#--macro from a.ftl-->
Line 2: <#include "b.ftl">
Line 3: <@foo pojo /> <#--macro from b.ftl-->

When FM evaluate line 1, it will lookup macro from exist variables and
it will resolved to macro in a.ftl.
After FM evaluate line 2, the macro variables will be overrrided to
macro in b.ftl.
When FM evaluate line 3, it will use the macro which will come from b.ftl.

Little trick :)

-Guo

------------------------------------------------------------------------------
This SF.Net email is sponsored by the Verizon Developer Community
Take advantage of Verizon's best-in-class app development support
A streamlined, 14 day to market process makes app distribution fast and easy
Join now and get one step closer to millions of Verizon customers
http://p.sf.net/sfu/verizon-dev2dev 
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Reply | Threaded
Open this post in threaded view
|

Re: [Freemarker-user] macro override

Daniel Dekany
Monday, December 21, 2009, 2:32:55 PM, Guo Du wrote:

> On Mon, Dec 21, 2009 at 1:25 PM, Daniel Dekany <[hidden email]> wrote:
>> How would this work?
> Line 1: <@foo pojo /> <#--macro from a.ftl-->
> Line 2: <#include "b.ftl">
> Line 3: <@foo pojo /> <#--macro from b.ftl-->
>
> When FM evaluate line 1, it will lookup macro from exist variables and
> it will resolved to macro in a.ftl.
> After FM evaluate line 2, the macro variables will be overrrided to
> macro in b.ftl.
> When FM evaluate line 3, it will use the macro which will come from b.ftl.

I don't think it addresses the original problem however. There after
you have "overridden" the macro, you should be able to call the
overridden one from the overriding one...

> Little trick :)
>
> -Guo

--
Best regards,
 Daniel Dekany


------------------------------------------------------------------------------
This SF.Net email is sponsored by the Verizon Developer Community
Take advantage of Verizon's best-in-class app development support
A streamlined, 14 day to market process makes app distribution fast and easy
Join now and get one step closer to millions of Verizon customers
http://p.sf.net/sfu/verizon-dev2dev 
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Reply | Threaded
Open this post in threaded view
|

Re: [Freemarker-user] macro override

Guo Du
On Mon, Dec 21, 2009 at 4:12 PM, Daniel Dekany <[hidden email]> wrote:
> I don't think it addresses the original problem however. There after
> you have "overridden" the macro, you should be able to call the
> overridden one from the overriding one...
Yes, I agree that there is no way to call the same macro inside the
overrided macro.

The trick could get the parent macro content, doesn't give you access
to original macro.

I wondering weather we can assign the parent macro to a new variable
name dynamically? If we can, then we can assign a new name such as
parent_foo could be invoked later in new macro foo.

-Guo

------------------------------------------------------------------------------
This SF.Net email is sponsored by the Verizon Developer Community
Take advantage of Verizon's best-in-class app development support
A streamlined, 14 day to market process makes app distribution fast and easy
Join now and get one step closer to millions of Verizon customers
http://p.sf.net/sfu/verizon-dev2dev 
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Reply | Threaded
Open this post in threaded view
|

Re: [Freemarker-user] macro override

Daniel Dekany
Monday, December 21, 2009, 6:37:56 PM, Guo Du wrote:

> On Mon, Dec 21, 2009 at 4:12 PM, Daniel Dekany <[hidden email]> wrote:
>> I don't think it addresses the original problem however. There after
>> you have "overridden" the macro, you should be able to call the
>> overridden one from the overriding one...
> Yes, I agree that there is no way to call the same macro inside the
> overrided macro.
>
> The trick could get the parent macro content, doesn't give you access
> to original macro.
>
> I wondering weather we can assign the parent macro to a new variable
> name dynamically? If we can, then we can assign a new name such as
> parent_foo could be invoked later in new macro foo.

I couldn't figure out a way for that... not immediately at least. But
anyway, te parent_<originalMacroName> trick will fail when you try to
extend a macro library that also extended something.

> -Guo
>

--
Best regards,
 Daniel Dekany


------------------------------------------------------------------------------
This SF.Net email is sponsored by the Verizon Developer Community
Take advantage of Verizon's best-in-class app development support
A streamlined, 14 day to market process makes app distribution fast and easy
Join now and get one step closer to millions of Verizon customers
http://p.sf.net/sfu/verizon-dev2dev 
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user