Git Product home page Git Product logo

django-bettertemplates's Introduction

Django Better Templates

Features

Adds an {% includeblock <template_name> ... %} template tag.

The includeblock tag works identically to the {% include <template_name ... %} template tag built into Django, except that it allows for the definition of blocks inside the includeblock tag, which can be referenced in the included template.

This allows for a flatter template design, keeping purely layout-related HTML scruff seperate from template logic and shrinking template file sizes.

The includeblock templatetag re-uses django template rendering code and should be thread-safe, although thread safety has not been fully tested yet.

Compatible with: Django >= 1.5 (up to Django 1.8)

Installation

Install this django app (no pypi yet, sorry) and add the app to your installed_apps:


INSTALLED_APPS = (
    ...
    'bettertemplates',
    ...
)

Example

Main Template

{% includeblock 'example/includable_template.html' with myvar='Inline context variables' %}

    {% block myblock %}
        <div style="border: 1px solid orange; padding: 10px;"> 
           <p>This inner block is rendered in a template variable in the included template.</p>
           <p>(The variable must have the same name as the block.)</p>
           {% with inner_var='django filters' %}
               <p>It is identical with regular django templates and supports everything like <b>{% trans "Template Tags" %}</b> and <b>{{ inner_var|upper }}</b>.</p>
           {% endwith %}
           <p>Note also how <u>{{ myvar }}</u> are supported.
        </div>
    {% endblock myblock %}
    
    {% block mysecondblock %}
        <div style="border: 1px solid green; padding: 10px;"> 
           A second Block.
        </div>
    {% endblock mysecondblock %}
    
    {% block anotherblock %}
        This content is never rendered because the inner block is not referenced in the included template!
    {% endblock %}
    
    <b>This content is never rendered because it appears outside inner blocks!</b>
        
{% endincludeblock %}

Included Template

<div style="border: 1px solid blue; padding: 10px;">
    <table width="99%">
        <tr>
            <td width="33%">
                {{ myblock }}
            </td>
            <td width="33%">
                {{ mysecondblock }}
            </td>
            <td width="33%">
                {{ mysecondblock }}
            </td>
        </tr>
    </table>
</div>

Rendered HTML

        <div style="border: 1px solid orange; padding: 10px;"> 
           <p>This inner block is rendered in a template variable in the included template.</p>
           <p>(The variable must have the same name as the block.)</p>
           
               <p>It is identical with regular django templates and supports everything like <b>Template Tags</b> and <b>DJANGO FILTERS</b>.</p>
           
           <p>Note also how <u>Inline context variables</u> are supported.
        </div>
    
        </td>
        <td width="33%">
            
        <div style="border: 1px solid green; padding: 10px;"> 
           A second Block.
        </div>
    
        </td>
        <td width="33%">
            
        <div style="border: 1px solid green; padding: 10px;"> 
           A second Block.
        </div>
    
        </td>
    </tr>
</table>

django-bettertemplates's People

Contributors

andreasnuesslein avatar saschasommer avatar

Stargazers

 avatar

Watchers

 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.