示例#1
0
def render_button(content,
                  button_type=None,
                  icon=None,
                  button_class='',
                  size='',
                  href='',
                  name=None,
                  value=None,
                  title=None):
    attrs = {}
    classes = add_css_class('btn', button_class)
    size = text_value(size).lower().strip()
    if size == 'xs':
        classes = add_css_class(classes, 'btn-xs')
    elif size == 'sm' or size == 'small':
        classes = add_css_class(classes, 'btn-sm')
    elif size == 'lg' or size == 'large':
        classes = add_css_class(classes, 'btn-lg')
    elif size == 'md' or size == 'medium':
        pass
    elif size:
        raise BootstrapError(
            'Parameter "size" should be "xs", "sm", "lg" or empty ("{}" given).'
            .format(size))
    if button_type:
        if button_type == 'submit':
            classes = add_css_class(classes, 'btn-primary')
            attrs['class'] = 'btn btn-primary'
        elif button_type == 'reset':
            attrs['class'] = 'btn btn-warning'
        elif button_type not in ('reset', 'button', 'link'):
            raise BootstrapError(
                'Parameter "button_type" should be "submit", "reset", "button", "link" or empty ("{}" given).'
                .format(button_type))
        attrs['type'] = button_type
        icon_content = render_icon(icon) if icon else ''
        if href:
            attrs['href'] = href
            tag = 'a'
        else:
            tag = 'button'
        if name:
            attrs['name'] = name
        if value:
            attrs['value'] = value
        if title:
            attrs['title'] = title
        return render_tag(tag,
                          attrs=attrs,
                          content=mark_safe(
                              text_concat(icon_content, content,
                                          separator=' ')))
 def test_text_concat(self):
     self.assertEqual(text_concat(1, 2), "12")
     self.assertEqual(text_concat(1, 2, separator='='), "1=2")
     self.assertEqual(text_concat(None, 2, separator='='), "2")