Git Product home page Git Product logo

django-templates-macros's Introduction

PyPI Version

django-templates-macros

Add macros to your django templates

Origin

The original code of this app is taken from https://djangosnippets.org/snippets/2892/ by MattP.

This snippets cites:

Based on snippet by
    Michal Ludvig <[email protected]> http://www.logix.cz/michal
    http://djangosnippets.org/snippets/363/

Extended for args and kwargs into templatetags/kwacro.py by
    Skylar Saveland <[email protected]> http://skyl.org
    https://gist.github.com/skyl/1715202

Modified to support rendering into context by [email protected]

Where

You can find this package here:

Usage

  1. Add this app "macros" to your INSTALLED_APPS
  2. In your template load the library:

    {% load macros %}
  3. Define a new macro called 'my_macro' that takes args and/or kwargs All will be optional:

    {% macro my_macro arg1 arg2 baz="Default baz" %}
        {% firstof arg1 "default_arg1" %}
        {% if arg2 %}{{ arg2 }}{% else %}default_arg2{% endif %}
        {{ baz }}
    {% endmacro %}
  4. Use the macro with string parameters or context variables:

    {% usemacro my_macro "foo" "bar" baz="KW" %}
    <br>
    {% usemacro my_macro num_pages "bar" %}
    <br>
    {% setmacro my_macro %} {{ my_macro }}

Renders like:

foo bar KW
77 bar Default baz
default_arg1 default_arg2 Default baz
  1. Alternatively save your macros in a separate file, e.g. "mymacro.html" and load it to the current template with:

    {% loadmacros "mymacros.html" %}

    Then use these loaded macros in as described above.

Bear in mind that defined and loaded macros are local to each template file and are not inherited through {% extends ... %} tags.

django-templates-macros's People

Contributors

forever-young avatar jezdez avatar twidi avatar worldveil avatar

Watchers

 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.