christabor/flask_extras

View on GitHub
flask_extras/macros/utils.html

Summary

Maintainability
Test Coverage
{#
    NOTE:
    Many of these macros are single argument so they can be passed around and called, e.g. other macros.
#}

{% macro apply_classes(classes) -%}
{#
    Apply css classes inside an element.
    Usage:
    <table {{ apply_classes(['table', 'table-striped']) }}></table>
    <table class="table table-striped"></table>
#}
{% for css in classes %}{{ css }} {% endfor %}
{%- endmacro %}


{% macro apply_prop(name, val) -%}
{% if name and val %}{{ name }}="{{ val }}"{% endif %}
{%- endmacro %}


{% macro apply_dattrs(data_attrs) -%}
{#
    Apply data-attributes inside an element.
    Usage:
    <div {{ apply_dattrs(['foo', 'bar']) }}></div>
    <div data-foo data-bar></div>

    As a dictionary:
    <div {{ apply_dattrs({'foo': 'bar, 'bar': 'foo'}) }}></div>
    <div data-foo="bar" data-bar="foo"></div>
#}
{% if data_attrs is mapping %}
    {% for attr, val in data_attrs.items() %}data-{{ attr|camel2hyphen }}='{{ val }}' {% endfor %}
{% else %}
    {% for attr in data_attrs %}data-{{ attr }} {% endfor %}
{% endif %}
{%- endmacro %}


{%- macro ip_link(ipaddr) %}
<a href="http://{{ ipaddr }}" target="_blank">{{ ipaddr }}</a>
{% endmacro -%}


{%- macro simpleurl(link, link_text='View url', blank=True) %}
    {% if not link.startswith('http') %}
    {% set link = 'http://%s'|format(link) %}
    {% endif %}
    <a href="{{ link }}" {% if blank %}target="blank"{% endif %}>{{ link_text }}</a>
{% endmacro -%}


{%- macro boolean_icon_display(boolval) %}
    {% if boolval %}
        <span class="fa fa-check-circle text-success"></span>
    {% else %}
        <span class="fa fa-times-circle text-danger"></span>
    {% endif %}
{% endmacro -%}


{%- macro titleize(val) %}
{{ underscore2space(hyphen2space(val)).strip()|capitalize }}
{% endmacro -%}


{%- macro underscore2space(val) %}
{{ val|replace('_', ' ') }}
{% endmacro -%}


{%- macro hyphen2space(val) %}
{{ val|replace('-', ' ') }}
{% endmacro -%}


{%- macro dot2space(val) %}
{{ val|replace('.', ' ') }}
{% endmacro -%}


{%- macro remove_parens(val) %}
{{ val|replace('(', '')|replace(')', '') }}
{% endmacro -%}


{%- macro remove_brackets(val) %}
{{ val|replace('[', '')|replace(']', '') }}
{% endmacro -%}


{%- macro remove_braces(val) %}
{{ val|replace('{', '')|replace('}', '') }}
{% endmacro -%}