Giter Site home page Giter Site logo

jeeveshn / django-lineage Goto Github PK

View Code? Open in Web Editor NEW

This project forked from marcuswhybrow/django-lineage

0.0 2.0 0.0 307 KB

Lightweight template tags for neater hierarchical navigation

Home Page: https://pypi.python.org/pypi/django-lineage/

License: BSD 3-Clause "New" or "Revised" License

Python 100.00%

django-lineage's Introduction

Django Lineage

Which navigation element is "active" in a template? Lineage determines a navigation element's state by inspecting the page URL:

{% load lineage %}

<ul>
    <li class="{% ancestor '/home/' %}"><a href="/home/">Home</a></li>
    <li class="{% ancestor '/blog/' %}"><a href="/blog/">Blog</a></li>
    <li class="{% ancestor '/about/' %}"><a href="/about/">About</a></li>
</ul>

If the argument of ancestor matches the start of the URL (e.g. this nav element is at least a parent of this page), it outputs "active". It's that simple!

Installation

Install using pip:

pip install django-lineage

Add 'lineage' to INSTALLED_APPS in settings.py:

INSTALLED_APPS = (
    'lineage',
)

Usage

The ancestor tag needs to, of course, be loaded into your template:

{% load lineage %}

The first way to use Lineage is the aformentioned ancestor tag. Again if the argument matches the start of the page URL it outputs "active", this should handle most use cases:

{% ancestor '/arbitrary/path/' %}

ancestor can also handle variables, filters and all that stuff:

{% ancestor some_variable|somefilter %}

Most importantly it also accepts url tag type reverse resolution (Behind the scenes the url tag derives our expected argument - a URL path string.)

{% ancestor 'core:model_detail' model.pk %}

Output Defaults to "active"

By default ancestor outputs "active" on a match. You can alter this default by adding LINEAGE_ANCESTOR_PHRASE = 'newphrase' to settings.py

Overring Output

Override output on demand using the ifancestor/endifancestor combo:

{% ifancestor 'pattern_name' %}
    This text here is only renderd if the
    URL argument is an ancestor.
{% endifancestor %}

Assumptions

Lineage depends on sensible URL hierarchies, because it compares paths using regex matching. {% ancestor '/base/' %} will fire if the current URL begins with /base/. For example /base/ and /base/section/page/ return true, but /other/path/ and /base (missing trailing slash) will not.

request must be present in the request context, since it's used to determine the current URL. Django has a context preprocessor that can insert it for you.

Similar Alternatives

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.