twisted / twisted/twisted

twisted.web.template: unable to use a dynamic renderer to omit an attribute

Open
#5,213 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement new priority-normal web
Dominant language
Python
Stars
6k
Forks
1.2k
Avg merge
2d 10h
Merged PRs (30d)
10

Description

glyph's avatar @glyph reported
Trac ID trac#5213
Type enhancement
Created 2011-07-30 19:51:03Z

Trying to write some HTML generation code that doesn't manipulate tags (so that precompilation can one day make it fast) I needed to conditionally render an attribute: specifically, the selected attribute of an option tag. If selected appears at all, regardless of its value, browsers will treat that option as the active one. I thought that this example would remove the class attribute from a div:

#!py
import sys
from twisted.web.template import (
    Element, XMLString, renderer, flattenString)

class ConditionalAttr(Element):
    loader = XMLString(
        """
        <html xmlns:t="http://twistedmatrix.com/ns/twisted.web.template/0.1">
        <div><t:attr t:render="maybe" name="class">hello</t:attr></div>
        </html>
        """)

    @renderer
    def maybe(self, request, tag):
        return ""

flattenString(None, ConditionalAttr()).addCallback(sys.stdout.write)

but as you can see if you run it, it simply renders an empty 'class' unconditionally. This seems odd, as the renderer is on the t:attr itself, and any other node rendered thusly would be removed from the DOM entirely.

Searchable metadata
trac-id__5213 5213
type__enhancement enhancement
reporter__glyph glyph
priority__normal normal
milestone__ 
branch__ 
branch_author__ 
status__new new
resolution__None None
component__web web
keywords__ 
time__1312055463000000 1312055463000000
changetime__1374526902000000 1374526902000000
version__None None
owner__ 
cc__jknight cc__indigo cc__glyph

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reproduced example in twisted.web.template, particularly the Element, XMLString, renderer, and flattenString entry points, and trace how a t:attr renderer result is handled. Confirm the current output and determine how an empty renderer result should affect the class or selected attribute; the issue names no specific source file or test, so the relevant coverage must be located.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
web-dev
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.