You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Thomas Nichols <nx...@yahoo.co.uk> on 2003/01/13 14:53:13 UTC

[jelly] How does context inheritance work?

Hi,

I'm having difficulty understanding what JellyContext.setInherit() 
accomplishes. In a custom tag library I have TagSupport-derivatives : e.g., 
given
<doc>
   <my:a>    <!-- maps to TagA -->
     <my:b/>    <!-- maps to TagB -->
   </my:a>
   <my:c/>    <!-- maps to TagC -->
</doc>

In TagA.doTag() :
...
getContext().setVariable ("monday", "blue");
...

In TagB.doTag() :
System.out.println (getContext().getVariable ("monday"));

This currently prints "blue" - in other words, a context var set in one tag 
is visible in the context of a child tag. Great!
FWIW, this was _not_ working until I started using CVS HEAD instead of a 
snapshot.

Now my confusion starts. Although getContext().getInherit() returns true, 
adding getContext().setInherit (false) to either (or both) doTag() methods 
seems to have no effect on this behaviour. This is what I had expected from 
findVariable() -- but not from getVariable(). What am I misunderstanding? 
Will this same context be visible in TagC?


This is all driven using a modified EmbeddedJelly.java, running CVS HEAD on 
a Linux / JDK 1.3.1 platform.

Suffice to say that Jelly is stonkingly good, I expect it imminently to 
achieve world domination. Many thanks for the diligent labours involved.

Yours aye,
Thomas.


Re: [jelly] How does context inheritance work?

Posted by James Strachan <ja...@yahoo.co.uk>.
From: "Thomas Nichols" <nx...@yahoo.co.uk>
> Hi James,
> 
> > > This is all driven using a modified EmbeddedJelly.java,
> 
> I'd love to see some standard "runners" as Paul suggests, using 
> EmbeddedJelly involved copy&paste and is therefore clearly loathsome and 
> abhorrent to mankind.

How about just using JellyContext?

Writer someWriter = new FileWriter( "output.xml" );
XMLOutput output = XMLOutput.createXMLOutput( someWriter );

JellyContext context = new JellyContext();
context.runScript( "foo.jelly", output );

James
-------
http://radio.weblogs.com/0112098/
__________________________________________________
Do You Yahoo!?
Everything you'll ever need on one web page
from News and Sport to Email and Music Charts
http://uk.my.yahoo.com

Re: [jelly] How does context inheritance work?

Posted by Thomas Nichols <nx...@yahoo.co.uk>.
Hi James,

At 14:42 13/01/2003 +0000, James Strachan wrote:
>Hi Thomas
>
>From: "Thomas Nichols" <nx...@yahoo.co.uk>
> > Hi,
> >
> > I'm having difficulty understanding what JellyContext.setInherit()
> > accomplishes. In a custom tag library I have TagSupport-derivatives :
>e.g.,
> > given
> > <doc>
> >    <my:a>    <!-- maps to TagA -->
> >      <my:b/>    <!-- maps to TagB -->
> >    </my:a>
> >    <my:c/>    <!-- maps to TagC -->
> > </doc>
> >
> > In TagA.doTag() :
> > ...
> > getContext().setVariable ("monday", "blue");
> > ...
> >
> > In TagB.doTag() :
> > System.out.println (getContext().getVariable ("monday"));
> >
> > This currently prints "blue" - in other words, a context var set in one
>tag
> > is visible in the context of a child tag. Great!
> > FWIW, this was _not_ working until I started using CVS HEAD instead of a
> > snapshot.
>
>Wierd it should do.

I was previously using maven-1.0-beta-7/lib/commons-jelly-SNAPSHOT.jar 
dated [...checks...] aargh! 1 Oct 2002, so disregard this.



> > Now my confusion starts. Although getContext().getInherit() returns true,
> > adding getContext().setInherit (false) to either (or both) doTag() methods
> > seems to have no effect on this behaviour. This is what I had expected
>from
> > findVariable() -- but not from getVariable(). What am I misunderstanding?
> > Will this same context be visible in TagC?
>
>By default the same context is used for all the tags in your example. A
>child context is only used if a tag decides it wants to use a new context to
>evaluate its children, or when a <j:include> is performed.

Excellent, this is exactly what I want.

>So the
>setInherit() was only really done for including other child scripts.
>
>Incidentally there is a <j:scope> which allows a local scope to be used to
>declare private variable scope.
>
>http://jakarta.apache.org/commons/sandbox/jelly/tags.html#core:scope
>
>e.g.
>
>You could create a number of different contexts each of which can contain
>different values of a given variable name. e.g.
>
><j:set var="foo" value="1"/>
><j:scope>
>     <j:set var="foo" value="2"/>
>
>     <j:scope>
>         <j:set var="foo" value="3"/>
>         ${foo}
>     </j:scope>
>     ${foo}
></j:scope>
>${foo}
>
>which would output "3 2 1". i.e. that the above implements a stack of
>variable values of the 'foo' variable.

Wonderful!



> > This is all driven using a modified EmbeddedJelly.java,

I'd love to see some standard "runners" as Paul suggests, using 
EmbeddedJelly involved copy&paste and is therefore clearly loathsome and 
abhorrent to mankind.

>running CVS HEAD
>on
> > a Linux / JDK 1.3.1 platform.
> >
> > Suffice to say that Jelly is stonkingly good, I expect it imminently to
> > achieve world domination.
>
>LOL! :-)
>
>
>James
>-------
>http://radio.weblogs.com/0112098/

Regards,
Thomas.


Re: [jelly] How does context inheritance work?

Posted by Paul Libbrecht <pa...@activemath.org>.
May I suggest that a package (like org.apache.commons.jelly.runners) is 
created to collect all these kind of Embedders, runners. I presume 
there will be more than one...

Paul


On Lundi, janv 13, 2003, at 15:42 Europe/Paris, James Strachan wrote:

>> This is all driven using a modified EmbeddedJelly.java, running CVS 
>> HEAD


Re: [jelly] How does context inheritance work?

Posted by James Strachan <ja...@yahoo.co.uk>.
Hi Thomas

From: "Thomas Nichols" <nx...@yahoo.co.uk>
> Hi,
>
> I'm having difficulty understanding what JellyContext.setInherit()
> accomplishes. In a custom tag library I have TagSupport-derivatives :
e.g.,
> given
> <doc>
>    <my:a>    <!-- maps to TagA -->
>      <my:b/>    <!-- maps to TagB -->
>    </my:a>
>    <my:c/>    <!-- maps to TagC -->
> </doc>
>
> In TagA.doTag() :
> ...
> getContext().setVariable ("monday", "blue");
> ...
>
> In TagB.doTag() :
> System.out.println (getContext().getVariable ("monday"));
>
> This currently prints "blue" - in other words, a context var set in one
tag
> is visible in the context of a child tag. Great!
> FWIW, this was _not_ working until I started using CVS HEAD instead of a
> snapshot.

Wierd it should do.


> Now my confusion starts. Although getContext().getInherit() returns true,
> adding getContext().setInherit (false) to either (or both) doTag() methods
> seems to have no effect on this behaviour. This is what I had expected
from
> findVariable() -- but not from getVariable(). What am I misunderstanding?
> Will this same context be visible in TagC?

By default the same context is used for all the tags in your example. A
child context is only used if a tag decides it wants to use a new context to
evaluate its children, or when a <j:include> is performed. So the
setInherit() was only really done for including other child scripts.

Incidentally there is a <j:scope> which allows a local scope to be used to
declare private variable scope.

http://jakarta.apache.org/commons/sandbox/jelly/tags.html#core:scope

e.g.

You could create a number of different contexts each of which can contain
different values of a given variable name. e.g.

<j:set var="foo" value="1"/>
<j:scope>
    <j:set var="foo" value="2"/>

    <j:scope>
        <j:set var="foo" value="3"/>
        ${foo}
    </j:scope>
    ${foo}
</j:scope>
${foo}

which would output "3 2 1". i.e. that the above implements a stack of
variable values of the 'foo' variable.


> This is all driven using a modified EmbeddedJelly.java, running CVS HEAD
on
> a Linux / JDK 1.3.1 platform.
>
> Suffice to say that Jelly is stonkingly good, I expect it imminently to
> achieve world domination.

LOL! :-)


James
-------
http://radio.weblogs.com/0112098/

__________________________________________________
Do You Yahoo!?
Everything you'll ever need on one web page
from News and Sport to Email and Music Charts
http://uk.my.yahoo.com