Skip to main content

Media

This class manages and renders collections of CSS and JavaScript assets required for web components. It provides functionality to merge multiple media definitions while preserving the relative order of assets through topological sorting. The class supports rendering assets into HTML tags and allows for the combination of different media objects using addition.

Attributes

AttributeTypeDescription
_css_listslistA list of dictionaries containing CSS file paths grouped by media type, used to aggregate stylesheets for merging and rendering.
_js_listslistA list of lists containing JavaScript file paths, used to aggregate scripts for merging and rendering.

Constructor

Signature

def Media(
media: object = None,
css: dict = None,
js: list = None
) - > null

Parameters

NameTypeDescription
mediaobject = NoneAn optional object from which to extract 'css' and 'js' attributes.
cssdict = NoneA dictionary mapping media types to lists of CSS file paths.
jslist = NoneA list of JavaScript file paths.

Methods


render()

@classmethod
def render(
attrs: dict
) - > string

Combines the rendered HTML output of both CSS and JavaScript assets into a single safe string.

Parameters

NameTypeDescription
attrsdictAdditional HTML attributes to be added to every rendered asset tag

Returns

TypeDescription
stringA mark-safe string containing all < link > and < script > tags

render_js()

@classmethod
def render_js(
attrs: dict
) - > list

Generates a list of HTML script tags for all JavaScript assets defined in the object.

Parameters

NameTypeDescription
attrsdictHTML attributes to apply to the generated < script > tags

Returns

TypeDescription
listA list of strings, each representing a < script > tag

render_css()

@classmethod
def render_css(
attrs: dict
) - > list

Generates a list of HTML link tags for all CSS assets, sorted by media type to ensure consistent rendering.

Parameters

NameTypeDescription
attrsdictHTML attributes to apply to the generated < link > tags

Returns

TypeDescription
listA list of strings, each representing a < link > tag for stylesheets

absolute_path()

@classmethod
def absolute_path(
path: string
) - > string

Given a relative or absolute path to a static asset, return an absolute path. An absolute path will be returned unchanged while a relative path will be passed to django.templatetags.static.static().

Parameters

NameTypeDescription
pathstringThe asset path to resolve, which can be a URL, absolute path, or relative static path

Returns

TypeDescription
stringThe fully qualified URL or absolute path to the asset

merge()

@classmethod
def merge(
lists: list
) - > list

Merge lists while trying to keep the relative order of the elements. Warn if the lists have the same elements in a different relative order.

Parameters

NameTypeDescription
listslistOne or more lists of asset paths to be merged together

Returns

TypeDescription
listA single merged list containing all unique elements in a valid dependency order