示例#1
0
文件: tags.py 项目: aodag/WebHelpers2
def title(title, required=False, label_for=None):
    """Format the user-visible title for a form field.

    Use this for forms that have a text title above or next to each
    field.

    ``title`` -- the name of the field; e.g., "First Name".

    ``required`` -- if true, append a \*" to the title and use the
    'required' HTML format (see example); otherwise use the 'not
    required' format.

    ``label_for`` -- if provided, put ``<label for="ID">`` around the
    title.  The value should be the HTML ID of the input field related
    to this title.  Per the HTML standard, the ID should point to a
    single control (input, select, textarea), not to multiple controls
    (fieldset, group of checkboxes, group of radio buttons).  ID's are
    set by passing the keyword arg ``id`` to the appropriate helper.
    
    Note that checkboxes and radio buttions typically have their own
    individual labels in addition to the title.  You can set these with
    the ``label`` argument to ``checkbox()`` and ``radio()``.

    This helper does not accept other keyword arguments.

    See webhepers2/static/stylesheets/webhelpers2.css for suggested styles.

    >>> title("First Name")
    literal(u'<span class="not-required">First Name</span>')
    >>> title("Last Name", True)
    literal(u'<span class="required">Last Name <span class="required-symbol">*</span></span>')
    >>> title("First Name", False, "fname")
    literal(u'<span class="not-required"><label for="fname">First Name</label></span>')
    >>> title("Last Name", True, label_for="lname")
    literal(u'<span class="required"><label for="lname">Last Name</label> <span class="required-symbol">*</span></span>')
    """
    title_html = title
    required_html = literal("")
    if label_for:
        title_html = HTML.label(title_html, for_=label_for)
    if required:
        required_symbol = HTML.span("*", class_="required-symbol")
        return HTML.span(
            title_html, 
            " ",
            required_symbol,
            class_="required")
    else:
        return HTML.span(title_html, class_="not-required")
示例#2
0
 def render_static(self):
     self.add_attr('class', 'textarea')
     if self.displayval == '':
         todisplay = literal('&nbsp;')
     else:
         todisplay = self.displayval
     return HTML.span(todisplay, **self._static_attributes())
示例#3
0
 def render_static(self):
     if self.displayval == '':
         todisplay = literal('&nbsp;')
     else:
         if self.displayval.startswith('http:') or self.displayval.startswith('https:'):
             todisplay = tags.link_to(self.displayval, self.displayval)
         else:
             todisplay = self.displayval
     return HTML.span(todisplay, **self._static_attributes())
示例#4
0
 def render_static(self):
     if self.etype in ('button', 'file', 'hidden', 'image', 'submit',
                       'reset', 'password'):
         return ''
     if self.displayval == '':
         todisplay = literal('&nbsp;')
     else:
         todisplay = self.displayval
     return HTML.span(todisplay, **self._static_attributes())
示例#5
0
 def render_static(self, **kwargs):
     self.set_attrs(**kwargs)
     if self.chosen or self.get_attr(self.chosen_attr, None):
         if self.displayval or self.displayval == 0:
             todisplay = self.displayval
         else:
             todisplay = literal('&nbsp;')
     else:
         todisplay = literal('&nbsp;')
     return HTML.span(todisplay, **self._static_attributes())
示例#6
0
文件: tags.py 项目: aodag/WebHelpers2
def required_legend():
    """Return an inline HTML snippet explaining which fields are required.
    
    See webhepers/static/stylesheets/webhelpers2.css for suggested styles.

    >>> required_legend()
    literal(u'<span class="required required-symbol">*</span> = required')
    """
    return HTML(
        HTML.span("*", class_="required required-symbol"), 
        " = required",
        )
示例#7
0
    def render_static(self):
        if self.displayval == '':
            todisplay = literal('&nbsp;')
        else:
            values = []

            def mapf(option):
                if isinstance(option, tuple):
                    return str(option[0]), option[1]
                else:
                    return str(option), option
            lookup = dict(map(mapf, self.options))
            for key in tolist(self.displayval):
                try:
                    values.append(lookup[str(key)])
                except KeyError:
                    pass
            todisplay = ', '.join(values)

        self.add_attr('class', 'select')
        return HTML.span(todisplay, **self._static_attributes())
示例#8
0
 def test_tag_with_data_attr(self):
     assert HTML.span(data_foo="bar") == literal('<span data-foo="bar"></span>')
示例#9
0
 def test_tag_with_data_attr(self):
     assert HTML.span(data_foo="bar") == literal('<span data-foo="bar"></span>')
示例#10
0
 def render_static(self):
     return HTML.span('yes' if self.displayval else 'no', **self._static_attributes())