I am just starting with web development and found web.py to be
beginners friendly framework.

I would like to contribute the following code. Basically it allows to
edit html page, using some nice editor, like Quanta+, and then to
"copy" the html to the code:

class Label(web.form.Input):
    tmpl = """<label name="%(name)s" %(atts)s>%(value)s</label>%(note)s"""
    def render(self):
        value = ''
        if self.value is not None:
            value = web.net.websafe(self.value)
        return self.tmpl % dict(   name=web.net.websafe(self.name)
                                 , value=value
                                 , atts=self.addatts()
                                 , note=self.rendernote(self.note) )

class RawHTML(web.form.Input):
    def __init__(self, html, *validators, **attrs):
        web.form.Input.__init__( self, '', *validators, **attrs )
        self.__html = html
    def render(self):
        return self.__html

class SimpleForm( web.form.Form ):
    tmpl = """<form %(attrs)s>\n%(inputs)s\n</form>\n"""
    def __init__(self, *inputs, **kw):
        web.form.Form.__init__( self, *inputs, **kw )
        self.__kw = kw
        self.__kw.pop( 'validators', None )

    def render_addattrs( self ):
        attrs = []
        for (n, v) in self.__kw.iteritems():
            attrs.append( ' %s="%s"' % (n, web.net.websafe(v)) )
        return ''.join( attrs )

    def render( self ):
        controls = []
        controls.append( self.rendernote(self.note) )
        for i in self.inputs:
            if i.pre:
                controls.append( i.pre )
            controls.append( i.render() )
            if i.post:
                controls.append( i.post )
        return self.tmpl % dict( attrs=self.render_addattrs(),
inputs='\n'.join( controls ) )


Label class is just a convenience class
RawHTML - takes a html as input, and renders it as-is
SimpleForm - class, that renders ``form`` tag and ``inputs``, without
formatting.

Example:

"""
    <form action="/generated.html" method="post" id="SourceCodeForm"
style="width:80%; float:left;">
        <label>Source code:</label>
        <br/>
        <textarea name="source_code" rows="20" style="width:100%;"></textarea>
        <br/>
        <input type="submit" name="show_decls" value="Show declarations" />
        <a></a>
        <input type="submit" name="generate_bp" value="Generate
Boost.Python code" />
        <a></a>
        <input type="submit" name="generate_pypp" value="Generate Py++ code" />
    </form>
"""

class generator_t( SimpleForm ):
    def __init__( self, action, method='post' ):
        SimpleForm.__init__( self
            , Label( 'source_code', value="Source code:" )
            , RawHTML( '<br/>' )
            , web.form.Textarea( "source_code", rows="20", style="width:100%;")
            , RawHTML( '<br/>' )
            , web.form.Button( 'Show declarations', type="submit" )
            , RawHTML( '<a></a>' )
            , web.form.Button( 'Generate Boost.Python code', type="submit" )
            , RawHTML( '<a></a>' )
            , web.form.Button( 'Generate Py++ code',
id="generate_pypp_code", type="submit" )
            , action=action
            , method=method
            , style="width:80%; float:left;" )

The SimpleForm class implementation could be simpler, if:
* form.Form class would keep and provide access for keyword argument,
passed to __init__ method. This would also allow not to duplicate some
code
* render_addattrs method could be define as a module function, which
takes arguments - both SimpleForm and classes, derived from
form.Input, could use it

I also have some question: is there a good reason, for defining tag
``form`` in a template, instead of form.Form class?

Any comments are welcome.

-- 
Roman Yakovenko
C++ Python language binding
http://www.language-binding.net/

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"web.py" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/webpy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to