Giter Club home page Giter Club logo

django-app-namespace-template-loader's Introduction

Django App Namespace Template Loader

Build Status - develop branch Coverage of the code

Provides a template loader that allows you to load a template from a specific application. This allows you to both extend and override a template at the same time.

The default Django loaders require you to copy the entire template you want to override, even if you only want to override one small block.

This is the issue that this package tries to resolve.

Examples:

You want to change the titles of the admin site, you would originally created this template: :

$ cat my-project/templates/admin/base_site.html
{% extends "admin/base.html" %}
{% load i18n %}

{% block title %}{{ title }} | My Project{% endblock %}

{% block branding %}
<h1 id="site-name">My Project</h1>
{% endblock %}

{% block nav-global %}{% endblock %}

Extend and override version with a namespace: :

$ cat my-project/templates/admin/base_site.html
{% extends "admin:admin/base_site.html" %}

{% block title %}{{ title }} - My Project{% endblock %}

{% block branding %}
<h1 id="site-name">My Project</h1>
{% endblock %}

Note that in this version the block nav-global does not have to be present because of the inheritance.

Shorter version without namespace: :

$ cat my-project/templates/admin/base_site.html
{% extends ":admin/base_site.html" %}

{% block title %}{{ title }} - My Project{% endblock %}

{% block branding %}
<h1 id="site-name">My Project</h1>
{% endblock %}

If we do not specify the application namespace, the first matching template will be used. This is useful when several applications provide the same templates but with different features.

Example of multiple empty namespaces: :

$ cat my-project/application/templates/application/template.html
{% block content %}
<p>Application</p>
{% endblock content %}

$ cat my-project/application_extension/templates/application/template.html
{% extends ":application/template.html" %}
{% block content %}
{{ block.super }}
<p>Application extension</p>
{% endblock content %}

$ cat my-project/templates/application/template.html
{% extends ":application/template.html" %}
{% block content %}
{{ block.super }}
<p>Application project</p>
{% endblock content %}

Will render: :

<p>Application</p>
<p>Application extension</p>
<p>Application project</p>

Installation

First of all install django-app-namespace-template-loader with your favorite package manager. Example : :

$ pip install django-app-namespace-template-loader

Once installed, add app_namespace.Loader to the TEMPLATE_LOADERS setting of your project. :

TEMPLATE_LOADERS = [
  'app_namespace.Loader',
  ... # Other template loaders
]

With Django >= 1.8 app_namespace.Loader should be added to the 'loaders' section in the OPTIONS dict of the DjangoTemplates backend instead. :

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'OPTIONS': {
            'loaders': [
                'app_namespace.Loader',
                'django.template.loaders.filesystem.Loader',
                'django.template.loaders.app_directories.Loader',
            ],
        },
    },
]

Note: With Django 1.8, app_namespace.Loader should be first in the list of loaders.

Known limitations

app_namespace.Loader can not work properly if you use it in conjunction with django.template.loaders.cached.Loader and inheritance based on empty namespaces.

Notes

Based originally on: http://djangosnippets.org/snippets/1376/

Requires: Django >= 1.8

Tested with Python 2.7, 3.3, 3.4.

If you want to use this application for previous versions of Django, use the version 0.3.1 of the package.

If you want to use this application with Python 2.6, use the version 0.2 of the package.

django-app-namespace-template-loader's People

Contributors

bittner avatar blueyed avatar fantomas42 avatar kakulukia avatar

Watchers

 avatar

Forkers

3rand chris7

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.