简单的pythonic html创建者

sphc的Python项目详细描述


  • Ultra simple and works

  • Compatible with Python3/ 2.x

  • Pythonic
    >>> adiv = tf.DIV("Hello World!", id="header_1", Class="header")
    >>> print (adiv)
    <DIV id="header_1" class="header">Hello World</DIV>
    

Examples

Hello World!

>>> import sphc
>>> tf = sphc.TagFactory()
>>> header = tf.H1("Hello World!")
>>> print(header)
<H1>Hello World</H1>

Constructing a page

>>> doc = tf.HTML()
>>> doc.body = tf.BODY()
>>> doc.body.content = tf.H1("The content")
>>> print(doc)
<HTML>
    <BODY>
        <H1>The content</H1>
    </BODY>
</HTML>

Using list of Tag objects

特别适用于构造表和选择选项:

>>> data = [('One', '1'), ('Two', '2'), ('Three', '3')]
>>> atable = tf.TABLE()
>>> for element in data:
>>>     row = tf.TR()
>>>     row.cells = [tf.TD(element[0]), tf.TD(element[1])]
>>>     atable.row = row

Wrapping

>>> block1 = tf.DIV(tf.DIV("content", Class="inner"), Class="outer")
>>> block2 = tf.DIV([tf.DIV(), tf.DIV()], Class="outer")
>>> content = tf.DIV([block1, block2])

Chaining

下面的set_required方法设置标记对象的必需属性并返回标记对象:

>>> form = tf.FORM()
>>> form.username = tf.INPUT(name="username").set_required()
>>> print(form)
<FORM>
    <INPUT name="username" required/>
</FORM>

Properties with no value required

>>> c = tf.INPUT(nv_attrs=['checked'], type='checkbox', value='foo')
>>> print(c)
<INPUT checked type="checkbox", value="foo"/>

Escaping

>>> print(tf.C(' >> ')) # Default
>>> <C> &gt;&gt; </C>

>>> print(tf.C(' >> ', escape=False))
>>> <C> >> </C>

More

既然你到这里来展示一些实验性的东西。

Hello sphc.more

(松散地基于html5boilerplate.com模板):

>>> import sphc.more
>>> tf = sphc.TagFactory()
>>> class MyPage(sphc.more.HTML5Page):
        def footer(self):
            return tf.FOOTER("Footer text")
>>> my_page = MyPage()
>>> my_page.render()

这将返回一个字符串,该字符串将包含与预期完全相同的html

Building a form

>>> import sphc
>>> import sphc.more
>>>
>>> tf = sphc.TagFactory()
>>>
>>> form = sphc.more.Form(classes=['vform'])
>>> form.add_field('Username', tf.INPUT(type="TEXT", id='username', name="username").set_required())
>>> form.add_field('Password', tf.INPUT(type="password", id='password', name="password"))
>>> form.add_buttons(tf.BUTTON("Log In", id='login-btn', type='button'))
>>> print form.build()

<FORM method="POST" Class="vform">
    <DIV Class="field">
        <DIV Class="field-label"> <LABEL For="username">Username</LABEL></DIV>
        <DIV Class="field-input">
            <INPUT required type="TEXT" name="username" id="username"></INPUT><C>*</C>
        </DIV>
    </DIV>
    <DIV Class="field">
        <DIV Class="field-label"> <LABEL For="password">Password</LABEL></DIV>
        <DIV Class="field-input">
            <INPUT type="password" name="password" id="password"></INPUT>
        </DIV>
    </DIV>
    <DIV Class="action-status"></DIV>
    <DIV Class="buttons">
        <BUTTON type="button" id="login-btn">Log In</BUTTON>
    </DIV>
</FORM>

Form with fieldsets

>>> form = sphc.more.Form()
>>>
>>> about = form.add(sphc.more.Fieldset())
>>> about.add(sphc.tf.LEGEND('About'))
>>> about.add_field('Name', sphc.tf.INPUT(name='name', type='text'))
>>>
>>> contact = form.add(sphc.more.Fieldset())
>>> contact.add(sphc.tf.LEGEND('About'))
>>> contact.add_field('Name', sphc.tf.INPUT(name='name', type='text'))

Finally

Similar packages

TODO

  • To add more tests
  • A document class ?

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
JavaSpring重定向请求处理程序   SwingJava:拆分字符串并将其放入文本区域的   Java:标记“”上出现语法错误,此标记后面应为表达式   web服务Java RestService从日志文件写入和读取数据   java如何将ArrayList<String>转换为char数组,然后向后打印每个单词?   java SimpleDataFormat解析返回年终日期   加密Java aes解密bytebuffer,包括填充为空字节   java有没有办法从特定的if语句调用变量?   java从更新返回到渲染   spring GRPC Java登录测试   java为什么下面的代码不工作(StringBuffer.toString!=null)   java是一种可行的模式吗?   使用Spring集成测试的JavaOSGi片段   java jCommander为未知和未使用的值引发异常?   在imageView的editText中输入的java图像URL