wiki:DiveIntoWidgets
Warning: Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.

Version 3 (modified by plewis, 13 years ago) (diff)

revert to v1 (despam)

The idea was to provide some explanation but I haven't been able to make it in time for the DocSprint?.

ATM there is only an interactive session showing some of the very import ants widgets features (template, name, value, params and subclassing), I hope you can make some doc from there.

I need to add a section regarding CompoundWidgets, I hope to be there within some hours to do that.

>>> from turbogears import widgets
>>> class HelloWorldWidget(widgets.Widget):
...     template = """
...     <div>Hello World!</div>
...     """
...
>>> hello = HelloWorldWidget()
>>> hello.render()
'<DIV>Hello World!</DIV>'
>>> class FooBarWidget(widgets.Widget):
...     template = """
...     <ul>
...             <li>my name is ${name}</li>
...             <li>my value is ${value}</li>
...     </ul>
...     """
...
>>> foobar = FooBarWidget(name="foobar")
>>> print foobar.render()
'<UL>
                <LI>my name is foobar</LI>
                <LI>my value is </LI>
        </UL>'
>>> print foobar.render(value=5)
'<UL>
                <LI>my name is foobar</LI>
                <LI>my value is 5</LI>
        </UL>'
>>> foobar = FooBarWidget(name="foobar", default=10)
>>> print foobar.render()
'<UL>
                <LI>my name is foobar</LI>
                <LI>my value is 10</LI>
        </UL>'
>>> print foobar.render(value=60)
'<UL>
                <LI>my name is foobar</LI>
                <LI>my value is 60</LI>
        </UL>'
>>> print foobar.render(value=60, name="wrong")
'<UL>
                <LI>my name is foobar</LI>
                <LI>my value is 60</LI>
        </UL>'
>>> class ParamsWidget(widgets.Widget):
...     template = """
...     <div>my attitude is ${attitude}</div>
...     """
...     params = ["attitude"]
...     attitude = "high"
...
>>> param = ParamsWidget()
>>> param.render()
'<DIV>my attitude is high</DIV>'
>>> param = ParamsWidget(attitude="medium")
>>> param.render()
'<DIV>my attitude is medium</DIV>'
>>> param.render(attitude="low")
'<DIV>my attitude is low</DIV>'
>>> param.params
['attitude']
>>> param.attitude
'medium'
>>> ParamsWidget.attitude
'high'
>>> class AnotherParamsWidget(ParamsWidget):
...     template = """
...     <div>my attitude is ${attitude} and my favourite language is ${language}</div>
...     """
...     params = ["language"]
...     language = "python"
...
>>> another = AnotherParamsWidget()
>>> another.render()
'<DIV>my attitude is high and my favourite language is python</DIV>'
>>> AnotherParamsWidget.params
['attitude', 'language']
>>> another.params
['attitude', 'language']
>>> AnotherParamsWidget.attitude
'high'
>>> another.attitude
'high'
>>> AnotherParamsWidget.language
'python'
>>> another.language
'python'
>>> another = AnotherParamsWidget(attitude="nice", language="java")
>>> AnotherParamsWidget.attitude
'high'
>>> another.attitude
'nice'
>>> AnotherParamsWidget.language
'python'
>>> another.language
'java'
>>> another.render()
'<DIV>my attitude is nice and my favourite language is java</DIV>'
>>> another.render(attitude="very low", language="ruby")
'<DIV>my attitude is very low and my favourite language is ruby</DIV>'