Exemplo n.º 1
0
def partial_definition(p, definition):
    try:
        for w,s in definition[:-1]:
            if s is None:
                p(html.h(w))
            else:
                p(link_to_synset(s, w, 'gloss'))
            p(u' ')
    except:
        p(definition)
Exemplo n.º 2
0
Arquivo: index.py Projeto: jperla/weby
def templates(p):
    p(html.h2('Weby Templates'))
    p(html.h3('Philosophy'))
    with p(html.ul()):
        p(html.li('Learn as few new things as possible.  You know Python.  You know HTML.  You should not have to learn a new template language.'))
        p(html.li('Use the full power of Python.  Easily create loops, use temporary variables, build filters, and import partials,'))
        p(html.li('Separate presentation from logic. For best practices, explicitly pass only the necessary variables.  Enforce this by convention, not by crippling power.'))
    p(html.h3('Design'))
    p(html.p('Weby templates are very simple. '))
    p(html.pre_code('''
        @weby.template()
        def template_print_name(p, name):
            p(u'Hello, %s' % name)
    '''))
    p(html.p('The function ' + html.code('template_print_name') +
                ' returns an'
                ' iterator of unicode strings.'))
    p(html.p('The ' + html.code('weby.template') + ' decorator'
                ' adds a template'
                ' accumulator as the first argument, then returns'
                ' the accumulated array of strings at the end.'
                ' You can make an equivalent template function'
                ' without the decorator, but it will require some'
                ' repetitive code:'))
    p(html.pre_code('''
        def template_print_name(name):
            t = []
            t.append(u'Hello, %s' % name)
            return t
    '''))
    p(html.p('Also, the '
                ' accumulator only takes unicode strings to enforce'
                ' correctness in your application code. Weby helps'
                ' you avoid unicode errors.'))
    p(html.p('Also, if you pass the template accumulator a'
                ' 2-tuple, then it will return a context manager'
                ' for use with the ' + html.code('with') + ''
                ' statement.  For example, '))
    p(html.pre_code(html.h('''
        @weby.template()
        def template_print_name(p, name):
            with p((u'<div>',u'</div>')):
                p(u'Hello, %s' % name)
    ''')))
    p(html.p('which is just syntactic sugar for'))
    p(html.pre_code(html.h('''
        @weby.template()
        def template_print_name(p, name):
            p(u'<div>')
            p(u'Hello, %s' % name)
            p(u'</div>')
    ''')))
    p(html.p('Weby has many libraries to make this even'
                ' easier.  Weby has libraries for writing'
                ' XML, RSS, and HTML templates:'))
    p(html.pre_code('''
        from weby.templates.helpers import html

        @weby.template()
        def template_print_name(p, name):
            with p(html.div()):
                p(u'Hello, %s' % name)
    '''))
    p(html.p('Every html tag has a predictable function in Weby\'s'
                ' html library.  Also, each one can take an attribute'
                ' dictionary.  The library always returns unicode'
                ' strings, so you can nest them easily.'))
    p(html.pre_code('''
        from weby.templates.helpers import html

        @weby.template()
        def template_print_name(p, name):
            with p(html.html()):
                with p(html.body()):
                    with p(html.div({'id':'hello'})):
                            p(html.p('Hello, %s' % name,
                                        {'style':'font-weight:bold;'}))
                    with p(html.ul()):
                        for i in xrange(3):
                            p(html.li(html.a('Hello, %s' % i, 
                                                {'href':'/'})))
    '''))
    p(html.p('This generates the following html'))
    p(html.pre_code(html.h('''
        <html>
        <body>
        <div id="hello">
        <p style="font-weight:bold;">Hello, Weby</p>
        </div>
        <ul>
        <li><a href="/">Hello, 0</a></li>
        <li><a href="/">Hello, 1</a></li>
        <li><a href="/">Hello, 2</a></li>
        </ul>
        </body>
        </html>
    ''')))