renderable — Unified renderable interface¶
When you build decoupled modules where separate items is rendered, you
often need to render several different templates into one output.
One approach is to just use the {% include %}
tag, but that
is not a very object oriented approach. To make this object oriented,
we use the django_cradmin.renderable.AbstractRenderable
class to provide a unified interface inspired by the TemplateView
class in Django.
To provide a renderable object, you simply subclass
django_cradmin.renderable.AbstractRenderable
, specify
a template name, and add methods, attributes and properties to
the class to make them available to the template.
-
join_css_classes_list
(css_classes_list)¶ Join the provided list of css classes into a string.
-
class
AbstractRenderable
¶ Bases:
object
An abstract class that implements an interface for rendering something.
Everything is just helpers for the
render()
method, which renders a template with an object of this class as input.-
template_name
= None¶ The default value for
get_template_names()
.
-
get_template_names
()¶ Get the template name(s) for
render()
.Defaults to
template_name
.Raises: NotImplementedError
– Iftemplate_name
is not set.
-
render
(request=None, extra_context_data=None)¶ Render
get_template_names
with the context returned byget_context_data()
.- Paramteters:
- request (HttpRequest): If this is provided, we forward it to
get_context_data()
, and torender_to_string()
(which is used to render the template).
-
-
class
AbstractRenderableWithCss
¶ Bases:
django_cradmin.renderable.AbstractRenderable
Extends
AbstractRenderable
with a unified API for setting CSS classes.-
get_css_classes_list
()¶ Override this to define css classes for the component.
Must return a list of css classes.
See
get_css_classes_string()
.
-
get_test_css_class_suffixes_list
()¶ List of css class suffixes to include when running automatic tests.
These suffixes are filtered through the
cradmin_test_css_class()
template tag.
-
css_classes
¶ Get css classes.
Joins
get_css_classes_list()
into a string.You should not override this, override
get_css_classes_list()
instead.
-
-
class
AbstractBemRenderable
(bem_block=None, bem_element=None, bem_variant_list=None, extra_css_classes_list=None)¶ Bases:
django_cradmin.renderable.AbstractRenderable
Base class for renderables that uses BEM (http://getbem.com/) for their CSS class naming.
This is an alternative to
AbstractRenderableWithCss
that makes it much more natural to work with BEM.Parameters: - bem_block (str) – Get the BEM block. Can not be supplied if
bem_element
is supplied. - bem_element (str) – Get the BEM element. Can not be supplied if
bem_block
is supplied. - bem_variant_list (list) – Get a list of BEM variants for the block/element.
You do not include the block/element, just the part after
--
. - extra_css_classes_list (list) – List of extra css classes.
-
get_test_css_class_suffixes_list
()¶ List of css class suffixes to include when running automatic tests.
These suffixes are filtered through the
cradmin_test_css_class()
template tag.
-
bem_block_or_element
¶ Returns
get_bem_block()
falling back toget_bem_element()
.
-
get_bem_block
()¶ Get the bem block string.
-
get_bem_element
()¶ Get the bem element string.
-
get_bem_variant_list
()¶ Get a list of BEM variants.
You do not include the block/element, just the part after
--
.
-
get_extra_css_classes_list
()¶ Get a list of extra css classes.
-
css_classes
¶ Get css classes as a string.
You should not override this, override
get_bem_block()
/get_bem_element()
andget_bem_variant_list()
instead.
- bem_block (str) – Get the BEM block. Can not be supplied if