You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@velocity.apache.org by Supun Kamburugamuva <su...@gmail.com> on 2007/07/03 14:33:08 UTC

Re: macros - what's next?

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 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