Skip to content
This repository has been archived by the owner on Feb 12, 2022. It is now read-only.

Commit

Permalink
Clean up jinja2 namespace.
Browse files Browse the repository at this point in the history
  • Loading branch information
James Socol committed Apr 13, 2011
1 parent 8d450e0 commit aabfa34
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 18 deletions.
10 changes: 5 additions & 5 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,13 @@ Jingo/Jinja2
To use a Flag in a Jinja2 template via `Jingo
<http://github.com/jbalogh/jingo>`_, you can simply do::

{% if waffle_flag('flag_name') %}
{% if waffle.flag('flag_name') %}
Content if flag is active
{% endif %}

You can also add an ``{% else %}`` section, of course::

{% if waffle_flag('flag_name') %}
{% if waffle.flag('flag_name') %}
Flag is active!
{% else %}
Flag is inactive!
Expand All @@ -144,21 +144,21 @@ You can also add an ``{% else %}`` section, of course::
To use a Switch in a Jinja2 template via `Jingo
<http://github.com/jbalogh/jingo>`_, you can do::

{% if waffle_switch('switch_name') %}
{% if waffle.switch('switch_name') %}
Content if switch is active
{% endif %}

You can also add an ``{% else %}`` section, of course::

{% if waffle_switch('switch_name') %}
{% if waffle.switch('switch_name') %}
Switch is active!
{% else %}
Switch is inactive!
{% endif %}

For Samples::

{% if waffle_sample('sample_name') %}
{% if waffle.sample('sample_name') %}
Sample is active!
{% else %}
Sample is inactive!
Expand Down
10 changes: 8 additions & 2 deletions test_app/templates/jingo.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
{% if waffle_flag('flag') %}
{% if waffle.flag('flag') %}
flag on
{% else %}
flag off
{% endif %}

{% if waffle_switch('switch') %}
{% if waffle.switch('switch') %}
switch on
{% else %}
switch off
{% endif %}

{% if waffle.sample('sample') %}
sample on
{% else %}
sample off
{% endif %}
18 changes: 7 additions & 11 deletions waffle/helpers.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
from jingo import register
import jingo
import jinja2

from waffle import flag_is_active, sample_is_active, switch_is_active


@register.function
@jinja2.contextfunction
def waffle_flag(context, flag_name):
def flag_helper(context, flag_name):
return flag_is_active(context['request'], flag_name)


@register.function
def waffle_switch(switch_name):
return switch_is_active(switch_name)


@register.function
def waffle_sample(sample_name):
return sample_is_active(sample_name)
jingo.env.globals['waffle'] = {
'flag': flag_helper,
'switch': switch_is_active,
'sample': sample_is_active,
}
1 change: 1 addition & 0 deletions waffle/tests/test_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ def test_jingo_tags(self):
response = process_request(request, views.flag_in_jingo)
self.assertContains(response, 'flag off')
self.assertContains(response, 'switch off')
self.assertContains(response, 'sample')

0 comments on commit aabfa34

Please sign in to comment.