templatetags: add |startswith:"..." (#38897)
This commit is contained in:
parent
c8b764ff25
commit
31d5c01c9f
|
@ -28,6 +28,7 @@ from django.template import VariableDoesNotExist
|
|||
from django.template.base import TOKEN_BLOCK, TOKEN_VAR, TOKEN_COMMENT
|
||||
from django.template.defaultfilters import stringfilter
|
||||
from django.utils import dateparse
|
||||
from django.utils.encoding import force_text
|
||||
|
||||
from combo.data.models import Page, Placeholder
|
||||
from combo.public.menu import get_menu_context
|
||||
|
@ -264,3 +265,7 @@ def name_id(user):
|
|||
@register.simple_tag
|
||||
def get_page(page_slug):
|
||||
return Page.objects.get(slug=page_slug)
|
||||
|
||||
@register.filter
|
||||
def startswith(string, substring):
|
||||
return string and force_text(string).startswith(force_text(substring))
|
||||
|
|
|
@ -192,3 +192,12 @@ def test_asset_template_tags():
|
|||
page.save()
|
||||
t = Template('''{% load assets %}{% asset_url page.picture "collectivity:banner" size="200x200" %}''')
|
||||
assert t.render(Context({'page': page})) == '/media/page-pictures/test2.svg'
|
||||
|
||||
def test_startswith():
|
||||
t = Template('{% if foo|startswith:"bar" %}ok{% endif %}')
|
||||
context = Context({'foo': None})
|
||||
assert t.render(context) == ''
|
||||
context = Context({'foo': 'xx'})
|
||||
assert t.render(context) == ''
|
||||
context = Context({'foo': 'bar'})
|
||||
assert t.render(context) == 'ok'
|
||||
|
|
Loading…
Reference in New Issue