You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@velocity.apache.org by Jose Alberto Fernandez <JF...@viquity.com> on 2000/12/06 03:17:37 UTC

Local macros [was: RE: quick note on recent changes]

Hi,

I am opening this discussion to all, since I would like to
hear other peoples opinion.

For the kind of translation templates I am trying to build I will 
need to be able to limit the scope of macros defined on a template
to that template only. In my case I may have multiple people
writing templates and using the same Velocity runtime but each one
will be using its own set of unrelated macros.

I do not mean by this that I do not want the avility to have global
libraries of macros. But that I also need to be able to define
macros whose scope is limited to the template.
In other words, each template must have its own local dictionary
of macros that gets consulted before the global dictionary.

If you need more info about usage escenarios, let me know.

Jose Alberto

-----Original Message-----
From: Geir Magnusson Jr. [mailto:gmj@xyris.com]
Sent: Tuesday, December 05, 2000 1:52 PM
To: Jose Alberto Fernandez
Subject: RE: quick note on recent changes





-----Original Message----- 
From:   Jose  Alberto Fernandez [SMTP:JFernandez@viquity.com] 
Sent:   Tuesday, December 05, 2000 4:35 PM 
To:     'Geir Magnusson Jr.' 
Subject:        RE: quick note on recent changes 
I was not thinking on NameSpaces, but now that you bring this in, let me
ask. 
Are the macros defined in a template, local to that template? Or are they
global to the Runtime? 
This is really something I need since different unrelated people will be
writing the templates. 
I think is is quite understandable, that we need to be able to isolate them.

Currently it's a global soup.  I was thinking about namespaces and such.  I
guess the conclusion was that for the first go-round, I wouldn't put them
in.  Some of the thinking came from the idea that in a common runtime
environment, like servlets, there should be some coordination between
writers.  But of course there are exceptions to that.  Let me know what you
are wanting to do - maybe we can work something out.  Always interested in
actual use cases.
 
On my original question, I found I can do what I wanted by just declaring my
macros 
with the format I want, within another macro definintion that I will never
call. The innner macros 
get register fine, and all the white space around the macro definitions
never gets rendered. 
  
Coool, 
Coool!  I don't understand the problem still though. 
Oh!  I see.  Yes, if there is whitespace when you are defining  your macros,
it will render if they macros are in the template.  You can have the macros
load at engine start time, by using the properties :
velocimacro.library.global=VM_global_library.vm 
velocimacro.library.local=VM_local_library.vm 
Of course, you can change the filenames to whatever you want.  The point /
hope / idea here is that the global libraray is a collection of useful VMs
that we can package with the velocity distribution, and the local is a
collection of your application or site-local VMs.  And whitespace crap
around the VMs doesn't get rendered, of course.
Maybe I'll add 
velocimacro.library.1.name = 
velocimacro.library.2.name = 
etc.  Will be at home after this : geirm@optonline.net 
geir 
Jose Alberto 
-----Original Message-----
From: Geir Magnusson Jr. [mailto:gmj@xyris.com]
Sent: Tuesday, December 05, 2000 11:32 AM
To: Jose Alberto Fernandez
Subject: RE: quick note on recent changes


LIke namespaces...  I think that would get panned.  The general philosophy
(which I support) is to keep the number of directives to the bare minimum,
and the notion of namespace is prollie right out.
The idea is to keep the template language as simple as possible for the
designer crowd. 
However, I have a helper class that you can use in Java to invoke macros
right from java code, something like 
  boolean invokeVM(  strVMName, strParms[], context, writer) 
which returns true if it found and executed the VM, and false if not. 
We can add methods there where you can dump macros, or simply just reload
via the usual template pattern.  Would that help?
geir 


        -----Original Message-----
From:   Jose  Alberto Fernandez [SMTP:JFernandez@viquity.com]
Sent:   Tuesday, December 05, 2000 2:27 PM
To:     'Geir Magnusson Jr.'
Cc:     'gmj@xyris.com'
Subject:        RE: quick note on recent changes 
        Well, that is good to know. 
        Here is an idea for another directive.
Notice that in the kind of problem I am trying to atack with Velocity
the library of macros will be escentially different for each template
hence, the global library is not really an option.
I would like to have a way to define part of "library zone" on the template.

        In such a zone, you just load the macros defined there but the rest
of the space formating does not count. For example: 
        -----------
#macrozone 
        #macro(a)
...
#end 
        ## Nicely formated comments with blank lines
## separating the different macro definintions, etc 
        #macro(b $x)
... $x ...
#end 
        #end##End of macro zone
This is the start of the actual template
#a()
#b(123)
End of template
------------
Of course I could load the macro zone using #parse(file.mac)
or have a #loadlib(file.mac) directive. But the point is that
spacing there will not count. 
        Can I achive this by defining my macros inside a macro I never use? 
        -----------
#macro(zone) 
        #macro(a)
...
#end 
        ## Nicely formated comments with blank lines
## separating the different macro definintions, etc 
        #macro(b $x)
... $x ...
#end 
        #end##End of macro zone
This is the start of the actual template
#a()
#b(123)
End of template
------------ 
        I will try it. 
        Jose Alberto 
        > -----Original Message-----
> From: Geir Magnusson Jr. [ <ma...@optonline.net>]
> Sent: Monday, December 04, 2000 8:44 PM
> To: Jose Alberto Fernandez
> Subject: Re: quick note on recent changes
>
>
> OOOHhhhhhhhhhh.
>
> I get it.
>
> You are putting the macro def after the use. :)
>
> Ah.
>
> That's actually wrong, but it works for some subtle reasons I
> won't bore
> you with.
>
> Yes, that's not documented and it should be.
>
> Yes, i have a fix coming :)
>
> Thanks for helping me find it!  I had two reports, but
> couldn't make it
> happen, because everyone *knows* :) that you must define before use :)
>
> The way its supposed to be is that you define all your macro's first,
> either in a global library that sucked in at startup, or at the top of
> each template.
>
> I do have a fix coming to solve that, so you can #macro()
> anywhere.  It
> will be a performance hit,
> but if you are caching, it will be fine.
>
> Thanks for the help!
>
> more inline ->
>
> Jose Alberto Fernandez wrote:
> >
> > Yes, is still happening.
> >
> > BTW, I have gotten some other very wierd errors.
> > For example assume this input file:
> > ----------
> > <something>
> > #mymacro
> > <morestuff>$msg.SomeValue</morestuff>
> > </something>
> >
> > #macro(mymacro)
> > <otherstuff>$msg.OtherProp</otherstuff>
> > #myOtherMacro($msg.Value)
> > #end
> > ----------
> >
> > The code above expands fine. However if I move the content
> > of the macro in place (cut & paste). I get errors of
> > parameters wrong for myOtherMacro.
>
> What do you mean?  I don't understand.
>
> > --------
> > <something>
> > <otherstuff>$msg.OtherProp</otherstuff>
> > #myOtherMacro($msg.Value)
> > <morestuff>$msg.SomeValue</morestuff>
> > </something>
> > ----------
> >
> > When I put the thing back the way it was it started working again.
> > Of course, my real life example is more complicated but
> > that is the just of what I did. I have no idea why it happened.
> > Lets see if tomorrow I can send you something,
>
> Cool.
>
>
> >
> > Jose Alberto
> >
> > > -----Original Message-----
> > > From: Geir Magnusson Jr. [ <ma...@optonline.net>]
> > > Sent: Monday, December 04, 2000 7:45 PM
> > > To: Jose Alberto Fernandez
> > > Subject: Re: quick note on recent changes
> > >
> > >
> > >
> > > Can you make the zero arg VM problem repeat?  I am now on
> a windows98
> > > box, and I can't get it to happen.
> > >
> > > geir
> > >
> > >
> > > --
> > > Geir Magnusson Jr.                              
> geirm@optonline.com
> > > Dakota tribal wisdom: "when you discover you are riding a
> dead horse,
> > > the best strategy is to dismount."
> > >
>
> --
> Geir Magnusson Jr.                               geirm@optonline.com
> Dakota tribal wisdom: "when you discover you are riding a dead horse,
> the best strategy is to dismount."
> 

Re: Local macros [was: RE: quick note on recent changes]

Posted by Daniel Rall <dl...@finemaltcoding.com>.
Jon Stevens <jo...@latchkey.com> writes:

> on 12/5/2000 8:14 PM, "Geir Magnusson Jr." <ge...@optonline.net> wrote:
> 
> > Again, I think these are useful, but I am not 100% convinced we want to
> > do this right now. (But the geek in me does :)
> > 
> > geir
> 
> My gut feeling is a -1 on #macro's having scope.
> 
> The fact that I made Anakia work is seriously pushing the overall design
> scope of Velocity IMHO.
> 
> If you start adding things like scope, then you are going to complicate
> things for designers. I'm against doing anything like that.

I am not in favor of adding scope to #macro's.  Leave them as simple
global symbols.

Daniel

Re: Local macros [was: RE: quick note on recent changes]

Posted by Jon Stevens <jo...@latchkey.com>.
on 12/5/2000 8:14 PM, "Geir Magnusson Jr." <ge...@optonline.net> wrote:

> Again, I think these are useful, but I am not 100% convinced we want to
> do this right now. (But the geek in me does :)
> 
> geir

My gut feeling is a -1 on #macro's having scope.

The fact that I made Anakia work is seriously pushing the overall design
scope of Velocity IMHO.

If you start adding things like scope, then you are going to complicate
things for designers. I'm against doing anything like that.

-jon


Re: Local macros [was: RE: quick note on recent changes]

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
Jose Alberto Fernandez wrote:
> 
> Hi,
> 
> I am opening this discussion to all, since I would like to
> hear other peoples opinion.
> 
> For the kind of translation templates I am trying to build I will
> need to be able to limit the scope of macros defined on a template
> to that template only. In my case I may have multiple people
> writing templates and using the same Velocity runtime but each one
> will be using its own set of unrelated macros.
> 
> I do not mean by this that I do not want the avility to have global
> libraries of macros. But that I also need to be able to define
> macros whose scope is limited to the template.
> In other words, each template must have its own local dictionary
> of macros that gets consulted before the global dictionary.
> 
> If you need more info about usage escenarios, let me know.
>

I thought about this, something like namespaces, in the beginning of our
VM adventure, and it was panned I think in discussion. I am still pretty
conservative about adding more directives. But I think attitudes have
changed about VMs since their intro, so this is a chance to rehash
this.  I am not thrilled about 'local' VMs, as this I think will result
in much confusion for junior people who don't fully understand the
implications of namespaces.

But...

For example, one implementation could be :

When you want to add VMs to a namespace, you start the template with a
new directive, something like :

#vmnamespace( "name ")

(and you can have some convention that if it's empty then defined macros
are local or something)

then follow with the macro definitions

#macro( foo $a )
  <blargh>
#end

#macro( bar $a )
   <blargh>
#end

This could be in a regular template, or a central library template.  I
am happier about seeing these in central library templates more than
regular ones.  We can add properties like

	velocimacro.library.1.name = lib1.vm
	velocimacro.library.2.name = lib2.vm

to add multiple libraries or something.

Then, when you want to use the macros in the namespace :

	#<namespace>:<vmname>( args )

so the above would be

	#name:foo( $a )

(or omit the <namespace> part if the macro is in the same template...)

Now, other than support in the VM system, the only addition is the new
directive, and of course allowing ':' as a directive identifier
character.

The problem with the above of course is that if you change the name of
the namespace, then you're screwed, as you have to update every use in
every template.

So think also about a directive like :

#usevmnamespace( "name"
)

that would be placed at the beginning of a template, and then you can
invoke the macros as you wish w/o the namespace prefix -> the declared
namespace will be checked...

Again, I think these are useful, but I am not 100% convinced we want to
do this right now. (But the geek in me does :)

geir

-- 
Geir Magnusson Jr.                               geirm@optonline.com
Dakota tribal wisdom: "when you discover you are riding a dead horse,
the best strategy is to dismount."