[Twisted-web] data="../foo", or, how to prevent adding IData to centxt

Donovan Preston twisted-web@twistedmatrix.com
Thu, 11 Dec 2003 10:57:56 -0500


--Apple-Mail-1-509279288
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
	charset=US-ASCII;
	format=flowed


On Dec 11, 2003, at 6:58 AM, Christopher Armstrong wrote:

> I have a renderer:
>
>     def render_select(self, ctx, data):
>         try:
>             return ctx.keyed(data)
>         except context.NodeNotFound:
>             return ctx.keyed('else')

*snip*

> This is causing nevow to try to do IContainer(directive('usertype')) 
> so it can get the 'addPageForm' child. addPageForm isn't a child of 
> usertype, though, so the most obvious thing to me (coming from a 
> Woven) background would be to use "../addPageForm" instead of 
> "addPageForm" -- but nevow doesn't support that.

In woven, the only way to put some data onto the stack was to use a 
directive (a string indicating how to find some data). In nevow, stan 
tags can hold references to other objects using "specials" -- special 
attribute names which aren't used in rendering HTML directly, but are 
used to *inform* the rendering process. Data is one of those specials. 
So if you are returning a node and you don't like the data which it 
will naturally get due to it's position in the context hierarchy, you 
can tell it what data to use explicitly.

def render_select(self, ctx, data):
     try:
         rv = ctx.keyed(data)
     except context.NodeNotFound:
         rv = ctx.keyed('else')
     return rv(data=ctx.locate(IData, depth=2))

In this case, we locate a stan node using the ctx.keyed method, which 
finds a node with the given key; we then "call" it and pass the data we 
want that node to use. (Calling stan nodes is the method used to set 
attributes/specials.)

The "depth" parameter to the locate method call is the moral equivalent 
of Woven's "." and ".." and "../.." directives. Calling locate with 
depth=1 will locate the current data, or ".", and calling locate with 
depth=2 will locate the parent data, or "..", etc.

nevow was specifically designed to allow this, as it gives your python 
code far more control over what is really going on in the rendering 
process.

dp

--Apple-Mail-1-509279288
Content-Transfer-Encoding: 7bit
Content-Type: text/enriched;
	charset=US-ASCII



On Dec 11, 2003, at 6:58 AM, Christopher Armstrong wrote:


<excerpt>I have a renderer:


    def render_select(self, ctx, data):

        try:

            return ctx.keyed(data)

        except context.NodeNotFound:

            return ctx.keyed('else')

</excerpt>

*snip*


<excerpt>This is causing nevow to try to do
IContainer(directive('usertype')) so it can get the 'addPageForm'
child. addPageForm isn't a child of usertype, though, so the most
obvious thing to me (coming from a Woven) background would be to use
"../addPageForm" instead of "addPageForm" -- but nevow doesn't support
that.

</excerpt>

In woven, the only way to put some data onto the stack was to use a
directive (a string indicating how to find some data). In nevow, stan
tags can hold references to other objects using "specials" -- special
attribute names which aren't used in rendering HTML directly, but are
used to *inform* the rendering process. Data is one of those specials.
So if you are returning a node and you don't like the data which it
will naturally get due to it's position in the context hierarchy, you
can tell it what data to use explicitly.


<fontfamily><param>Verdana</param>def render_select(self, ctx, data):

    try:

        rv = ctx.keyed(data)

    except context.NodeNotFound:

        rv = ctx.keyed('else')

    return rv(data=ctx.locate(IData, depth=2))

</fontfamily>

In this case, we locate a stan node using the ctx.keyed method, which
finds a node with the given key; we then "call" it and pass the data
we want that node to use. (Calling stan nodes is the method used to
set attributes/specials.)


The "depth" parameter to the locate method call is the moral
equivalent of Woven's "." and ".." and "../.." directives. Calling
locate with depth=1 will locate the current data, or ".", and calling
locate with depth=2 will locate the parent data, or "..", etc.


nevow was specifically designed to allow this, as it gives your python
code far more control over what is really going on in the rendering
process.


dp


--Apple-Mail-1-509279288--