patch: default values for slots from html templates
Hi all I've found out lately that you can give a slot a default value when using stan. Attached is a patch that enables that functionality for xmlstring and xmlfile templates. Stan: T.span[T.slot("my_slot_name", default="Whatever")] HTML: <span><n:slot name="my_slot_name" default="Whatever" /></span> regards Steven Index: nevow/flat/flatsax.py =================================================================== --- nevow/flat/flatsax.py (revision 1213) +++ nevow/flat/flatsax.py (working copy) @@ -103,7 +103,7 @@ if name == 'invisible': name = '' elif name == 'slot': - el = slot(attrs[(None,'name')]) + el = slot(attrs[(None,'name')], attrs.get((None, 'default'), None)) self.stack.append(el) self.current.append(el) self.current = el.children Index: nevow/flat/flatmdom.py =================================================================== --- nevow/flat/flatmdom.py (revision 1213) +++ nevow/flat/flatmdom.py (working copy) @@ -43,7 +43,7 @@ if name == 'invisible': name = '' elif name == 'slot': - return slot(element.attributes['name'])[ + return slot(element.attributes['name'], element.attributes.get("default", None))[ precompile(serialize(element.childNodes, context), context)] attrs = dict(element.attributes) # get rid of CaseInsensitiveDict
HTML: <span><n:slot name="my_slot_name" default="Whatever" /></span>
Shouldn't this be <span><n:slot name="my_slot_name">Whatever</n:slot></span> -- Sridhar Ratna - http://srid.bsdnerds.org
On 03/03/05 19:28, Sridhar Ratna wrote:
HTML: <span><n:slot name="my_slot_name" default="Whatever" /></span>
Shouldn't this be <span><n:slot name="my_slot_name">Whatever</n:slot></span>
That's not the same. e.g: given a dict like: people = [{'name': "Fred", 'surname': "Flintstone"}] <div n:data="people" n:render="sequence"> <div n:pattern="item" n:render="mapping"> <n:slot name="name" /> <n:slot name="surname" /> <n:slot name="age">Not defined</n:slot> <-- This raises a KeyError <n:slot name="age" default="Not defined" /> <-- This doesn't </div> </div> The cool thing about default="whatever" is that you can (mis)use it to mark slots as optional. IMHO it makes the render="mapping" functionality even more powerfull.
Cool! I for one will find that useful when it's committed (hint hint)... --- Ed Suominen Registered Patent Agent Open-Source Software Author (yes, both...) Web Site: http://www.eepatents.com On Thursday 03 March 2005 09:41 am, Steven Armstrong wrote:
Hi all
I've found out lately that you can give a slot a default value when using stan. Attached is a patch that enables that functionality for xmlstring and xmlfile templates.
Stan: T.span[T.slot("my_slot_name", default="Whatever")]
HTML: <span><n:slot name="my_slot_name" default="Whatever" /></span>
regards Steven
participants (3)
-
Ed Suominen
-
Sridhar Ratna
-
Steven Armstrong