Git Product home page Git Product logo

django-template-utils's Introduction

django-template-utils

Just a collection of useful template tags and filters gathered in a single app.

Index

Instalation

Via pip:

pip install django-template-utils

Via easy_install:

easy_install django-template-utils

Clonning the project:

$ git clone git://github.com/gerardo-orozco/django-template-utils.git
$ python django-template-utils/setup.py install

Add the app to your istalled apps:

INSTALLED_APPS = (
    ...
    'template_utils',
    ...
)

Contributing

You have an idea of a useful tag or filter? Found a bug? Marvelous, your contribution will be very appreciated.

Please refer to the Contributing section and have a happy coding!

Usage

Tags

First load the tags inside whatever templates you are going to use them:

{% load templateutils_tags %}

active_url

Returns a class to be assiged CSS styling that should make the chosen element highlighted as responsible of being in the active url.

Example: Assuming that the reversed url is the current url, this tag will act as follows:

{% active_url request url_name %} -> class="ui-active-url"
{% active_url request url_name class_name=myclass %} -> class="myclass"
{% active_url request url_name use_class=False %} -> ui-active-url
{% active_url request url_name class_name=myclass use_class=False %} -> myclass

Where urlname is the name of the url to check; this must be defined in your URLCONF, otherwise it will raise a NoReverseMatch Error.

current_url

Returns the reversed url only if it is NOT the current url. Otherwise returns the character "#"

Example:

<a href="{% current_url request url_name %}">Some link</a>

ifmember

Checks if the current user belongs to a specific group.

  • User must be looged in.
  • Requires the Django authentication contrib app and middleware.

Usage:

{% ifmember Admins %} ... {% endifusergroup %}

mkrange

Accepts the same arguments as the builtin range function and creates a list containing the result of range.

Usage:

{% mkrange [start, ]stop[, step] as context_name %}

For example:

{% mkrange 5 10 2 as some_range %}
{% for i in some_range %}
  {{ i }}: Something I want to repeat
{% endfor %}

Produces:

5: Something I want to repeat
7: Something I want to repeat
9: Something I want to repeat

Filters

Load the filters inside whatever templates you are going to use them:

{% load templateutils_filters %}

currency

Returns value represented as currency for the give locale.

Usage:

{{ value|currency }}

For example: Assuming value is 13, {{ value|currency }} produces: $13.00

integer

Returns the given value as an int type.

Usage:

{{ value|int }}
{% tag_that_requires_int value|int %}

nolinebrs

Removes all <br> tags in the given string.

startswith

Returns whether the given value starts with the given string arg. The argument must be a string.

Usage:

{{ value|startsvith:"arg" }}

creditcard

Hides parts of strings such as credit card or bank account numbers to show only the last amount of numbers. Default amount of numbers to show is 4

Usage:

{{ value|creditcard:chars_to_show }}

For example: Assuming value is "5000000000003456"

{{ value|creditcard:4 }}

Produces:

************3456

verbose

Returns the verbose value of a ChoiceField.

Usage:

{{ form.field|display }}

or

{% for field in form %}
    {{ field|display }}
{% endfor %}

For example: Assuming field is a BooleanField with True

{{ field|display }}

Produces: The string Yes

django-template-utils's People

Contributors

gerardo-orozco avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

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.