Skip to content

Cache components in Django automatically, turn caching on and off without changing the template

License

Notifications You must be signed in to change notification settings

SawdustSoftware/django-components

Repository files navigation

django-components

An encapsulated thing that is meant to be reused across the site in many
different views. Product reviews box, little stats widgets, etc. It's meant to
be like a mini-version of a view that you can embed in another page.

Benefits: easy to reuse code, change things in only one place to change
them everywhere, cache components automatically, turn caching on and off
without changing the template. All client side code (js, css, html) for one
component is in one place. No mucking around with manual cache key
generating.

In your template, add:

    {% components_maker %}
    {% reviewscomponent subscriber %}

Ta da.

To build a new component, simply inherit Component and:
    - Override fetch_data to return the context for your component template
    - Override options to pass in any variables you need, like user ids.
    This is in Classytags format.
    - Declare name as the template file to use.
    - Optionally override any other (cache key, rendering) function for
    more complex behavior

Overridables:
    - cached=True: enable / disable cache.
    - cache_duration=60*60*24: cache time in seconds.
    - tag_name=classname.lower(): name of the template tag
    - template_name: Name of the template to use
    - cache_vary_on: List of attribute names to get the value of when
    generating a cache key. Defaults to detecting the template tag
    arguments and using those.
    - sekizai_blocks: Dictionary of blockname -> content to inject js /
    css / etc into the template.

TODO: maybe have post_render also cache subcontext data
TODO: look into disabling offline compression to get compression to play
nice with components/sekizai https://github.com/ojii/django-sekizai/issues/4

About

Cache components in Django automatically, turn caching on and off without changing the template

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages