def bootstrap_javascript(jquery=None): """ Return HTML for Bootstrap JavaScript. Adjust url in settings. If no url is returned, we don't want this statement to return any HTML. This is intended behavior. Default value: ``None`` This value is configurable, see Settings section **Tag name**:: bootstrap_javascript **Parameters**: :jquery: Truthy to include jQuery as well as Bootstrap **Usage**:: {% bootstrap_javascript %} **Example**:: {% bootstrap_javascript jquery=1 %} """ javascript = '' if jquery is None: jquery = get_bootstrap_setting('include_jquery', False) if jquery: url = bootstrap_jquery_url() if url: javascript += render_tag('script', attrs={'src': url}) url = bootstrap_javascript_url() if url: javascript += render_tag('script', attrs={'src': url}) return mark_safe(javascript)
def test_render_tag(self): self.assertEqual(render_tag('span'), '<span></span>') self.assertEqual(render_tag('span', content='foo'), '<span>foo</span>') self.assertEqual( render_tag('span', attrs={'bar': 123}, content='foo'), '<span bar="123">foo</span>' )
def test_render_tag(self): self.assertEqual(render_tag("span"), "<span></span>") self.assertEqual(render_tag("span", content="foo"), "<span>foo</span>") self.assertEqual( render_tag("span", attrs={"bar": 123}, content="foo"), '<span bar="123">foo</span>', )
def render_icon(icon, tag='span', title=''): """ Render a Bootstrap glyphicon icon """ attrs = { 'class': 'fa fa-{icon}'.format(icon=icon), } if title: attrs['title'] = title if tag: return render_tag(tag, attrs=attrs) else: return render_tag('span', attrs=attrs)
def render_alert(content, alert_type=None, dismissable=True): """ Render a Bootstrap alert """ button = '' if not alert_type: alert_type = 'info' css_classes = ['alert', 'alert-' + text_value(alert_type)] if dismissable: css_classes.append('alert-dismissable fade in') button = """ <button type="button" class="close" data-dismiss="alert" aria-label="Close"> <span aria-hidden="true">×</span> </button> """ button_placeholder = '__BUTTON__' return mark_safe( render_tag( 'div', attrs={ 'class': ' '.join(css_classes), 'role': 'alert' }, content=button_placeholder + text_value(content), ).replace(button_placeholder, button))
def post_widget_render(self, html): return html + render_tag( 'div', {'class': 'form-check-input-div'}) + render_label( content=self.field.label, label_for=self.field.id_for_label, label_title=escape(strip_tags(self.field_help)), label_class="form-check-label", )
def render_label(content, label_for=None, label_class=None, label_title=''): attrs = {} if label_for: attrs['for'] = label_for if label_class: attrs['class'] = label_class if label_title: attrs['title'] = label_title return render_tag('label', attrs=attrs, content=content)
def render_icon(icon, title=''): """ Render a Bootstrap glyphicon icon """ attrs = { 'class': 'icon icon-{icon}'.format(icon=icon), } if title: attrs['title'] = title return render_tag('span', attrs=attrs)
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 render_icon(icon, **kwargs): """ Render a Bootstrap glyphicon icon """ attrs = { 'class': add_css_class( 'icon icon-{icon}'.format(icon=icon), kwargs.get('extra_classes', ''), ) } title = kwargs.get('title') if title: attrs['title'] = title return render_tag('span', attrs=attrs)
def render_alert(content, alert_type=None, dismissible=True): """Render a Bootstrap alert.""" button = "" if not alert_type: alert_type = "info" css_classes = ["alert", "alert-" + text_value(alert_type)] if dismissible: css_classes.append("alert-dismissible") close = _("close") button = f'<button type="button" class="close" data-dismiss="alert" aria-label="{close}">×</button>' button_placeholder = "__BUTTON__" return mark_safe( render_tag( "div", attrs={"class": " ".join(css_classes), "role": "alert"}, content=button_placeholder + text_value(content), ).replace(button_placeholder, button) )
def render_alert(content, alert_type=None, dismissable=True): """ Render a Bootstrap alert """ button = '' if not alert_type: alert_type = 'info' css_classes = ['alert', 'alert-' + text_value(alert_type)] if dismissable: css_classes.append('alert-dismissable') button = '<button type="button" class="close" ' + \ 'data-dismiss="alert" aria-hidden="true">×</button>' button_placeholder = '__BUTTON__' return mark_safe(render_tag( 'div', attrs={'class': ' '.join(css_classes)}, content=button_placeholder + text_value(content), ).replace(button_placeholder, button))
def render_alert(content, alert_type=None, dismissable=True): """ Render a Bootstrap alert """ button = "" if not alert_type: alert_type = "info" css_classes = ["alert", "alert-" + text_value(alert_type)] if dismissable: css_classes.append("alert-dismissable") button = ( '<button type="button" class="close" ' + 'data-dismiss="alert" aria-label="{}">×</button>' ) button = button.format(_("close")) button_placeholder = "__BUTTON__" return mark_safe( render_tag( "div", attrs={"class": " ".join(css_classes), "role": "alert"}, content=button_placeholder + text_value(content), ).replace(button_placeholder, button) )
def render_alert(content, alert_type=None, dismissable=True): """ Render a Bootstrap alert Port from bootstrap4, as it is bit buggy, and don't allow mark_safe """ button = "" if not alert_type: alert_type = "info" css_classes = ["alert", "alert-" + text_value(alert_type)] if dismissable: css_classes.append("alert-dismissable") button = ('<button type="button" class="close" ' + 'data-dismiss="alert" aria-label="{}">×</button>') button = button.format(_("close")) button_placeholder = mark_safe("__BUTTON__") return mark_safe( render_tag( "div", attrs={ "class": " ".join(css_classes), "role": "alert" }, content=button_placeholder + text_value(content), ).replace(button_placeholder, button))