You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@velocity.apache.org by Will Glass-Husain <wg...@gmail.com> on 2007/06/13 06:35:26 UTC

macros - what's next?

Hi Supun,

Now that you've knocked one of the items off your list, what's next?

The big one I'm hoping you'll work towards is VELOCITY-362, loading macros
from a file with #parse.  Lots of people (including me) are frustrated with
this issue.  (related, VELOCITY-146, VELOCITY-277 and
http://wiki.apache.org/velocity/MacroIssues )

In the sooner term, perhaps VELOCITY-529, adding macros programmatically
would be a good one to tackle.  This would be a significant extension to
Velocity capability and may be a bit easier.

A third big item Henning discusses in the wiki (with examples): the ability
to use named arguments for macros.  We'd want to make sure there was
consensus on syntax before coding.
http://wiki.apache.org/velocity/GoogleSummerOfCode2007

Other ideas from your end?

My suggestion is VELOCITY-529 (nice new capability), then VELOCITY-362
(makes life easier for many people), but what are are your interests?

WILL

-- 
Forio Business Simulations

Will Glass-Husain
wglass@forio.com
www.forio.com

Re: macros - what's next?

Posted by Supun Kamburugamuva <su...@gmail.com>.
Hi,

In the past few days I was looking into VELOCITY-529 and other issues
that you have mention. I would like to start with the VELOCITY-529.
But before doing this do I have to resolve VELOCITY-362?

Supun..

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@velocity.apache.org
For additional commands, e-mail: dev-help@velocity.apache.org


Re: macros - what's next?

Posted by Supun Kamburugamuva <su...@gmail.com>.
Hi,

In the past few days I was looking into VELOCITY-529 and other issues
that you have mention. I would like to start with the VELOCITY-529.
But before doing this do I have to resolve VELOCITY-362?

Supun.

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@velocity.apache.org
For additional commands, e-mail: dev-help@velocity.apache.org


Re: macros - what's next?

Posted by Supun Kamburugamuva <su...@gmail.com>.
Hi,

I have done according to the Will's suggestions (Not in the way that I
have proposed in a previous mail) and there are few issues that need
to be solved. They are

1) When a macro is called and if the macro cannot be found libraries
normally Velocity prints the macro call i.e #foo(1). But with my
current implementation this doesn't happen.

2) It work fine with the local inline scope but when it come to global
scope it doesn't work.

These are the two remaining issues that I have to complete.

Regards,
Supun..

p.s. Can I submit the implementation that I have done up to now in to
Jira so that some one can have a look at weather I'm going in the
correct direction?

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@velocity.apache.org
For additional commands, e-mail: dev-help@velocity.apache.org


Re: macros - what's next?

Posted by Supun Kamburugamuva <su...@gmail.com>.
In our approach we try to call

template = Velocity.getTemplate(templateFile);

before loading the templates containing the macro definitions. The
above call builds the node tree. But if this file contains a call to a
macro in a seperate macro file the macro is not registered yet(the
macros in the macro library files are registeres after the
template.merge). So it does not register the call as a macro call.

I think we can overcome this problem by loading the macro files before
the calling the Velocity.getTemplate method.

Context context = new VelocityContext();
List macroFiles = new ArrayList();
macroFiles.add("macro_library1.vm");
macroFiles.add("macro_library2.vm");

Velocity.resiterMacroLibsForTamplate(templateFile, macroFiles);

template = Velocity.getTemplate(templateFile);
Writer writer = new new OutputStreamWriter(outFileName);

template.merge(context, writer);


Regards,
Supun..

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@velocity.apache.org
For additional commands, e-mail: dev-help@velocity.apache.org


Re: macros - what's next?

Posted by Will Glass-Husain <wg...@gmail.com>.
Hi Supun,

Glad to hear you have picked this up again... thought you were lost for a
little while :-)

I'm not quite sure what is the purpose of the hash table you are
describing.  Is this intended to help cache the macros from merge to merge?

Remember, the intent is to load an the new macro file each time the template
merge is called.  Any caching is handled by the built in resource loading
facility.

Maybe it would be helpful to share some sample code?  It's probably not
useful to overwhelm the list with everything, but you could show what
happens with the method call:

template.merge(context, writer, macroFiles);

WILL



On 7/3/07, Supun Kamburugamuva <su...@gmail.com> wrote:
>
> hi,
>
> This is the approach that I have being thinking about.
>
> I'm keeping a hash table in the MacroFactory which has the mapping from
> template to the macro files i.e test_macro.vm -> macro_library1.vm,
> macro_library2.vm. When a user calls the template.merge with macro files I
> keep the information in that structure.
>
> Then I try to create a resource out of the macro library files, which
> ultimately build the node tree and add the macros to the macro manager.
>
> When macro is added in the MacroFactory I check whether the macro is in
> one
> of the macro library files (in the hash table). If it is in a macro
> library
> fie I'm replacing the macro library name with the main template name.
>
> But it seems that this approach is not working – I have done a test
> implementation, but it didn't work.
>
> Regards,
>
> Supun.
> <http://www.forio.com/>
>



-- 
Forio Business Simulations

Will Glass-Husain
wglass@forio.com
www.forio.com

Re: macros - what's next?

Posted by Supun Kamburugamuva <su...@gmail.com>.
hi,

This is the approach that I have being thinking about.

I'm keeping a hash table in the MacroFactory which has the mapping from
template to the macro files i.e test_macro.vm -> macro_library1.vm,
macro_library2.vm. When a user calls the template.merge with macro files I
keep the information in that structure.

Then I try to create a resource out of the macro library files, which
ultimately build the node tree and add the macros to the macro manager.

When macro is added in the MacroFactory I check whether the macro is in one
of the macro library files (in the hash table). If it is in a macro library
fie I'm replacing the macro library name with the main template name.

But it seems that this approach is not working – I have done a test
implementation, but it didn't work.

Regards,

Supun.
<http://www.forio.com/>

Re: macros - what's next?

Posted by Will Glass-Husain <wg...@gmail.com>.
Yes, absolutely... I think that will be a nice addition to the macro
functionality.

WILL



On 6/23/07, Supun Kamburugamuva <su...@gmail.com> wrote:
>
> Hi,
>
> Sorry, I'm little bit confused about what should I do. Can I go ahead
> and implement the method that you have suggested in the second
> approach?
>
> Supun,
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@velocity.apache.org
> For additional commands, e-mail: dev-help@velocity.apache.org
>
>


-- 
Forio Business Simulations

Will Glass-Husain
wglass@forio.com
www.forio.com

Re: macros - what's next?

Posted by Supun Kamburugamuva <su...@gmail.com>.
Hi,

Sorry, I'm little bit confused about what should I do. Can I go ahead
and implement the method that you have suggested in the second
approach?

Supun,

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@velocity.apache.org
For additional commands, e-mail: dev-help@velocity.apache.org


Re: macros - what's next?

Posted by Will Glass-Husain <wg...@gmail.com>.
Hi Supun,

I keep trying to write a long involved answer and getting interrupted.
Let's try again.

No, I don't think they are necessarily related.  It seems fine to do
VELOCITY-529 first.  (specifying one or more macro library files at merge
time).

I was originally thinking that the way to do this is to create a
MacroCartridge object similar to EventCartridge, with a method
attachToContext.  It's a little awkward for the user, but this way ensures
that the macros are carried through the entire request.

Context context = new VelocityContext();

MacroCartridge macros = new MacroCartridge();
macros.addTemplate("macro_library1.vm");
macros.addTemplate("macro_library2.vm");
macros.attachToContext(context);

template = Velocity.getTemplate(templateFile);
Writer writer = new new OutputStreamWriter(outFileName);
template.merge(context, writer);

But now I'm thinking it might be simplest to overload Template.merge and
VelocityEngine.evaluate methods

Context context = new VelocityContext();
List macroFiles = new ArrayList();
macroFiles.add("macro_library1.vm");
macroFiles.add("macro_library2.vm");
template = Velocity.getTemplate(templateFile);
Writer writer = new new OutputStreamWriter(outFileName);
template.merge(context, writer, macroFiles);

I'd hate to do this for every merge option, but including macros seems
pretty fundamental.

The key thing is that the macros should be loaded via the resource loader,
which will ensure they get cached.

Comments from anyone else?

WILL

On 6/17/07, Supun Kamburugamuva <su...@gmail.com> wrote:
>
> Hi,
>
> In the past few days I was looking into VELOCITY-529 and other issues
> that you have mention. I would like to start with the VELOCITY-529.
> But before doing this do I have to resolve VELOCITY-362?
>
> Supun..
>



-- 
Forio Business Simulations

Will Glass-Husain
wglass@forio.com
www.forio.com