Giter Club home page Giter Club logo

django_layers's Introduction

django layers

This package provides support for "layers" of templates and static resources that can be selecting depending on the request context.

WARNING: The API documented below may change significantly before version 1.0

Why?

Using layers you can provide alternative sets of templates ("skins") depending on different contexts. For example, using the same CMS you can, from a single code base, host different frontend designs, have a different visitor/admin frontend, do A/B testing, etc.

All of this within the same instance (so no separate instances each running with their own settings.py configuration)

How?

pip/easy_install this package, django_layers

Then add 'layers.middleware.LayerLoaderMiddleware' to your MIDDLEWARE_CLASSES, e.g.

MIDDLEWARE_CLASSES = (
    'layers.middleware.LayerLoaderMiddleware',
    ...
)

Also, add 'layers.loader.LayerLoader' to your TEMPLATE_LOADERS, e.g.

TEMPLATE_LOADERS = (
    'layers.loader.LayerLoader',
    ...
)

Optionally, if you have separate collections of static resources for each layer, add 'layers.finders.AppLayerFinder' as the first STATICFILE_FINDERS:

STATICFILES_FINDERS = (
    'layers.finders.AppLayerFinder',
    # ...
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)

and define which layers you have and where they need to be collected to

LAYERS = {
    'visitor-a':STATIC_ROOT + '/visitor-a',
    'visitor-b':STATIC_ROOT + '/visitor-b'
    }

Now you can start using layers.

Create the same templates as before but in stead (or on top of) storing them in your package's templates folder, store them in a folder called 'layers/layername/templates'.

E.g. you could have

mypackage/templates/mypackage/foo.html
mypackage/layers/visitor-a/templates/mypackage/foo.html
mypackage/layers/visitor-b/templates/mypackage/foo.html

This creates two layers, "visitor-a" and "visitor-b" and a fallback if no layer is selected.

Additionally, create a file "layers.py" with a function "get_layers" that will return the layer to be used, e.g.

def get_layer(request):
    if request.get_host().startswith("a."):
        return "visitor-a"
    if request.get_host().startswith("b."):
        return "visitor-b"

You can do anything you like in the "get_layer" callable, as long as you return a layer or nothing.

When requesting Django to render the template "mypackage/foo.html", it will render any of the three templates above depending on the request context (the hostname used).

Configuration per layer

You can also provide some global shared configuration per layer. Since all layers will share the same settings.py, it's not possible to use that for layer specific configuration.

You can do this by defining a 'get_config' method in your package's layers.py file. This will simply return a dict containing the layer specific data for each layer.

E.g.

def get_config():
    return {'visitor-a': dict(site_id=1, mailto='[email protected]'),
            'visitor-b': dict(site_id=2, mailto='[email protected]')
           }

You can then access the current layer's configuration using 'get_current_layer':

from layers.middleware import get_current_layer

def myview(request):
    layer = get_current_layer()
    return SomeModel.objects.filter(site_id=layer['site_id'])

django_layers will scan all your packages for configuration and merge the configuration dictionaries together if necessary.

Static resources per layer

You can store your per-layer statics in any app installed in your application in the layers/layer/layername/statics folder, e.g. you could have

mypackage/static/css/foo.css
mypackage/layers/visitor-a/static/css/foo.css
mypackage/layers/visitor-b/static/css/foo.css

A request for /static/css/foo.css will result in visitor-a/static/css/foo.css if the visitor-a layer is active, it will result in visitor-b/static/css/foo.css if the visitor-b layer is active or in mypackage/static/css/foo.css otherwise.

Static resources are served by the django 'runserver' command or by a webserver running in front of your application.

django_layers provides an upgraded runserver command that knows which static resources to serve depending on the active layer. It also comes with a collectlayers command that collects the layers into distinct staticfolders, similar to how 'collectstatic' works. Which layer is collected where is defined by the 'LAYERS' settings.py setting.

E.g. given the previous LAYERS definition

python manage.py collectlayers

will collect the global static resources and visitor-a specific resources into STATIC_ROOT + '/visitor-a' and another copy of the global static resources and visitor-b specific resources into STATIC_ROOT + '/visitor-b'

django_layers's People

Stargazers

 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.