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
| Attribute | Type | Description |
|---|---|---|
| _css_lists | list | A list of dictionaries containing CSS file paths grouped by media type, used to aggregate stylesheets for merging and rendering. |
| _js_lists | list | A 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
| Name | Type | Description |
|---|---|---|
| media | object = None | An optional object from which to extract 'css' and 'js' attributes. |
| css | dict = None | A dictionary mapping media types to lists of CSS file paths. |
| js | list = None | A 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
| Name | Type | Description |
|---|---|---|
| attrs | dict | Additional HTML attributes to be added to every rendered asset tag |
Returns
| Type | Description |
|---|---|
string | A 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
| Name | Type | Description |
|---|---|---|
| attrs | dict | HTML attributes to apply to the generated < script > tags |
Returns
| Type | Description |
|---|---|
list | A 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
| Name | Type | Description |
|---|---|---|
| attrs | dict | HTML attributes to apply to the generated < link > tags |
Returns
| Type | Description |
|---|---|
list | A 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
| Name | Type | Description |
|---|---|---|
| path | string | The asset path to resolve, which can be a URL, absolute path, or relative static path |
Returns
| Type | Description |
|---|---|
string | The 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
| Name | Type | Description |
|---|---|---|
| lists | list | One or more lists of asset paths to be merged together |
Returns
| Type | Description |
|---|---|
list | A single merged list containing all unique elements in a valid dependency order |