Skip to content
Browse files

Adding url() helper that wraps the django.core.urlresolvers.reverse()…

… function.
  • Loading branch information...
1 parent 474d7de commit 54ba3493be9123ad59f283e1b0e7bfb7dbac3d0c @auzigog auzigog committed with coffindragger Oct 17, 2011
Showing with 23 additions and 1 deletion.
  1. +2 −1 fake_settings.py
  2. +7 −0 jingo/helpers.py
  3. +8 −0 jingo/tests/test_helpers.py
  4. +6 −0 jingo/tests/urls.py
View
3 fake_settings.py
@@ -5,7 +5,7 @@
ROOT = os.path.dirname(os.path.abspath(__file__))
INSTALLED_APPS = (
'jingo.tests.jinja_app',
- 'jingo.tests.django_app'
+ 'jingo.tests.django_app',
)
TEMPLATE_LOADERS = (
'jingo.Loader',
@@ -14,3 +14,4 @@
)
TEMPLATE_DIRS = (path('jingo/tests/templates'),)
JINGO_EXCLUDE_APPS = ('django_app',)
+ROOT_URLCONF = 'jingo.tests.urls'
View
7 jingo/helpers.py
@@ -1,6 +1,7 @@
from django.utils.translation import ugettext as _
from django.template.defaulttags import CsrfTokenNode
from django.utils.encoding import smart_unicode
+from django.core.urlresolvers import reverse
import jinja2
@@ -73,3 +74,9 @@ def field_attrs(field_inst, **kwargs):
"""Adds html attributes to django form fields"""
field_inst.field.widget.attrs.update(kwargs)
return field_inst
+
+
+@register.function
+def url(viewname, *args, **kwargs):
+ """Return URL using django's ``reverse()`` function."""
+ return reverse(viewname, args=args, kwargs=kwargs)
View
8 jingo/tests/test_helpers.py
@@ -119,3 +119,11 @@ def __str__(self):
s = render('{{ field|field_attrs(class="bar",name="baz") }}',
{'field': f})
eq_(s, '<input class="bar" name="baz" />')
+
+
+def test_url():
+ # urls defined in jingo/tests/urls.py
+ s = render('{{ url("url-args", 1, "foo") }}')
+ eq_(s, "/url/1/foo/")
+ s = render('{{ url("url-kwargs", word="bar", num=1) }}')
+ eq_(s, "/url/1/bar/")
View
6 jingo/tests/urls.py
@@ -0,0 +1,6 @@
+from django.conf.urls.defaults import patterns
+
+urlpatterns = patterns('',
+ (r'^url/(\d+)/(\w+)/$', lambda r: None, {}, "url-args"),
+ (r'^url/(?P<num>\d+)/(?P<word>\w+)/$', lambda r: None, {}, "url-kwargs"),
+)

0 comments on commit 54ba349

Please sign in to comment.
Something went wrong with that request. Please try again.