Python django.template 模块,Library() 实例源码

我们从Python开源项目中,提取了以下4个代码示例,用于说明如何使用django.template.Library()

项目:django-performance-testing    作者:PaesslerAG    | 项目源码 | 文件源码
def test_there_is_a_correct_templatetag_library():
    """
    see https://docs.djangoproject.com/en/1.10/howto/custom-template-tags/
    """
    from django_performance_testing.templatetags import djpt_limits
    assert hasattr(djpt_limits, 'register')
    assert isinstance(djpt_limits.register, template.Library)
    assert 'djptlimit' in djpt_limits.register.tags


# The below tests are based on the registry's content. Tests will rely on the
# global defaults that are tested elsewhere in the test*registry*.py files
项目:django-performance-testing    作者:PaesslerAG    | 项目源码 | 文件源码
def get_args_kwargs(parser, token):
    """ copied from
        django.template.(base|library).Library.simple_tag.compile_func """
    def to_limit(limit_name, **limit_kwargs):
        pass
    params, varargs, varkw, defaults = getargspec(to_limit)
    function_name = 'djptlimit'
    bits = token.split_contents()[1:]
    takes_context = False
    args, kwargs = parse_bits(
        parser, bits, params,
        varargs, varkw, defaults, takes_context, function_name)
    return args, kwargs
项目:Django    作者:englam    | 项目源码 | 文件源码
def yes_no(bool_value, show_str):
    if bool_value:
        return show_str.partition('/')[0]
    else:
        return show_str.partition('/')[2]


#register = template.Library()
#register.filter('yes_no', yes_no)
项目:nav    作者:UNINETT    | 项目源码 | 文件源码
def compile_template_with_filters(template_string, filters):
    """Compile a Django template, using additional filters.

    This is like Template(template_string) except that additional
    filters to be made available to the template may be specified.

    Normally, one would define filters as documented in [1], but this
    requires the INSTALLED_APPS settings to be set, which is not the
    case in NAV[2]. This function is just a hack to get around that
    limitation. The code is based on
    django.template.compile_string[3].

    filters should be a dictionary mapping filter names to functions.

    [1]: http://docs.djangoproject.com/en/dev/howto/custom-template-tags/
    [2]: https://nav.uninett.no/wiki/devel:django_introduction#settings
    [3]: http://code.djangoproject.com/browser/django/trunk/django/template/__init__.py

    """
    lib = template.Library()
    for name in filters.keys():
        lib.filter(name, filters[name])
    lexer = template.Lexer(template_string, None)
    parser = template.Parser(lexer.tokenize())
    parser.add_library(lib)
    return parser.parse()