Skip to content
This repository has been archived by the owner on Nov 19, 2017. It is now read-only.

Commit

Permalink
Merge abecbba into 7366953
Browse files Browse the repository at this point in the history
  • Loading branch information
noirbizarre committed Sep 13, 2014
2 parents 7366953 + abecbba commit 5a5ac38
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
8 changes: 8 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ The pre-defined html attributes is available too::
{% endfor %}
</ul>

You can also have direct access to the current active item:


.. code-block:: html+jinja

<h2>{{ nav.top.current_item.label }}</h2>


API
---

Expand Down
8 changes: 8 additions & 0 deletions flask_navigation/navbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,11 @@ def alias_item(self, alias):
"""Gets an item by its alias."""
ident = self.alias[alias]
return self.items[ident]

@property
def current_item(self):
'''Get the current active navigation Item if any'''
for item in self:
if item.is_active:
return item
return None
25 changes: 25 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,28 @@ def biu():
with app.test_client() as c:
url = c.get('/')
assert url.data == b'/biu/42'


def test_current_item():
app = Flask(__name__)

nav = Navigation()
nav.init_app(app)
news_item = Item(u'News', 'news')
navbar = nav.Bar('test_current', [
Item(u'Home', 'home'),
news_item,
])

@app.route('/')
def home():
pass

@app.route('/news')
def news():
pass

assert navbar.current_item is None

with app.test_request_context('/news'):
assert navbar.current_item == news_item

0 comments on commit 5a5ac38

Please sign in to comment.