Hook that runs just before the component's template is rendered.
You can use this hook to access or modify the context or the template:
defon_render_before(self,context,template)->None:
-# Insert value into the Context
-context["from_on_before"]=":)"
-
-# Append text into the Template
-template.nodelist.append(TextNode("FROM_ON_BEFORE"))
-
The template argument is None if the component has no template.
Example:
You can use this hook to access the context or the template:
fromdjango.templateimportContext,Template
+fromdjango_componentsimportComponent
+
+classMyTable(Component):
+defon_render_before(self,context:Context,template:Optional[Template])->None:
+# Insert value into the Context
+context["from_on_before"]=":)"
+
+assertisinstance(template,Template)
+
Warning
If you want to pass data to the template, prefer using get_template_data() instead of this hook.
Warning
Do NOT modify the template in this hook. The template is reused across renders.
Since this hook is called for every component, this means that the template would be modified every time a component is rendered.
You can use hooks together with provide / inject to create components that accept a list of items via a slot.
In the example below, each tab_item component will be rendered on a separate tab page, but they are all defined in the default slot of the tabs component.
You can use hooks together with provide / inject to create components that accept a list of items via a slot.
In the example below, each tab_item component will be rendered on a separate tab page, but they are all defined in the default slot of the tabs component.
Django Components expects the rendered template to be a valid HTML. This is needed to enable features like CSS / JS variables.
Here is how the HTML is post-processed:
Insert component ID: Each root element in the rendered HTML automatically receives a data-djc-id-cxxxxxx attribute containing a unique component instance ID.
Insert CSS ID: If the component defines CSS variables through get_css_data(), the root elements also receive a data-djc-css-xxxxxx attribute. This attribute links the element to its specific CSS variables.
Insert JS and CSS: After the HTML is rendered, Django Components handles inserting JS and CSS dependencies into the page based on the dependencies rendering strategy (document, fragment, or inline).
Compared to the secondary JS / CSS files, the definition of file paths for the main HTML / JS / CSS files is quite simple - just strings, without any lists, objects, or globs.
If the path cannot be resolved relative to the component, django-components will attempt to resolve the path relative to the component directories, as set in COMPONENTS.dirs or COMPONENTS.app_dirs.
# When we create Calendar component, the files like `calendar/template.html`
-# are not yet loaded!
-@register("calendar")
-classCalendar(Component):
-template_file="calendar/template.html"
-css_file="calendar/style.css"
-js_file="calendar/script.js"
-
-classMedia:
-css="calendar/style1.css"
-js="calendar/script2.js"
-
-# It's only at this moment that django-components reads the files like `calendar/template.html`
-print(Calendar.css)
-# Output:
-# .calendar {
-# width: 200px;
-# background: pink;
-# }
-
Warning
Do NOT modify HTML / CSS / JS after it has been loaded
django-components assumes that the component's media files like js_file or Media.js/css are static.
If you need to dynamically change these media files, consider instead defining multiple Components.
Modifying these files AFTER the component has been loaded at best does nothing. However, this is an untested behavior, which may lead to unexpected errors.
Each component has only a single template associated with it.
However, whether it's for A/B testing or for preserving public API when sharing your components, sometimes you may need to render different templates based on the input to your component.
You can use Component.on_render() to dynamically override what template gets rendered.
By default, the component's template is rendered as-is.
Django Components expects the rendered template to be a valid HTML. This is needed to enable features like CSS / JS variables.
Here is how the HTML is post-processed:
Insert component ID: Each root element in the rendered HTML automatically receives a data-djc-id-cxxxxxx attribute containing a unique component instance ID.
Insert CSS ID: If the component defines CSS variables through get_css_data(), the root elements also receive a data-djc-css-xxxxxx attribute. This attribute links the element to its specific CSS variables.
Insert JS and CSS: After the HTML is rendered, Django Components handles inserting JS and CSS dependencies into the page based on the dependencies rendering strategy (document, fragment, or inline).
Compared to the secondary JS / CSS files, the definition of file paths for the main HTML / JS / CSS files is quite simple - just strings, without any lists, objects, or globs.
If the path cannot be resolved relative to the component, django-components will attempt to resolve the path relative to the component directories, as set in COMPONENTS.dirs or COMPONENTS.app_dirs.
# When we create Calendar component, the files like `calendar/template.html`
+# are not yet loaded!
+@register("calendar")
+classCalendar(Component):
+template_file="calendar/template.html"
+css_file="calendar/style.css"
+js_file="calendar/script.js"
+
+classMedia:
+css="calendar/style1.css"
+js="calendar/script2.js"
+
+# It's only at this moment that django-components reads the files like `calendar/template.html`
+print(Calendar.css)
+# Output:
+# .calendar {
+# width: 200px;
+# background: pink;
+# }
+
Warning
Do NOT modify HTML / CSS / JS after it has been loaded
django-components assumes that the component's media files like js_file or Media.js/css are static.
If you need to dynamically change these media files, consider instead defining multiple Components.
Modifying these files AFTER the component has been loaded at best does nothing. However, this is an untested behavior, which may lead to unexpected errors.
\ No newline at end of file
diff --git a/dev/concepts/fundamentals/html_js_css_variables/index.html b/dev/concepts/fundamentals/html_js_css_variables/index.html
index 3ddc59d7..84e992a6 100644
--- a/dev/concepts/fundamentals/html_js_css_variables/index.html
+++ b/dev/concepts/fundamentals/html_js_css_variables/index.html
@@ -95,7 +95,7 @@
"show_details":kwargs.show_details,}