Component caching allows you to store the rendered output of a component. Next time the component is rendered with the same input, the cached output is returned instead of re-rendering the component.
This is particularly useful for components that are expensive to render or do not change frequently.
Info
Component caching uses Django's cache framework, so you can use any cache backend that is supported by Django.
Component caching allows you to store the rendered output of a component. Next time the component is rendered with the same input, the cached output is returned instead of re-rendering the component.
This is particularly useful for components that are expensive to render or do not change frequently.
Info
Component caching uses Django's cache framework, so you can use any cache backend that is supported by Django.
By default, context variables are passed down the template as in regular Django - deeper scopes can access the variables from the outer scopes. So if you have several nested forloops, then inside the deep-most loop you can access variables defined by all previous loops.
With this in mind, the {% component %} tag behaves similarly to {% include %} tag - inside the component tag, you can access all variables that were defined outside of it.
And just like with {% include %}, if you don't want a specific component template to have access to the parent context, add only to the {% component %} tag:
{%component"calendar"date="2015-06-19"only/%}
+ Component context and scope - Django-Components
By default, context variables are passed down the template as in regular Django - deeper scopes can access the variables from the outer scopes. So if you have several nested forloops, then inside the deep-most loop you can access variables defined by all previous loops.
With this in mind, the {% component %} tag behaves similarly to {% include %} tag - inside the component tag, you can access all variables that were defined outside of it.
And just like with {% include %}, if you don't want a specific component template to have access to the parent context, add only to the {% component %} tag:
{%component"calendar"date="2015-06-19"only/%}
NOTE: {% csrf_token %} tags need access to the top-level context, and they will not function properly if they are rendered in a component that is called with the only modifier.
If you find yourself using the only modifier often, you can set the context_behavior option to "isolated", which automatically applies the only modifier. This is useful if you want to make sure that components don't accidentally access the outer context.
Components can also access the outer context in their context methods like get_template_data by accessing the property self.outer_context.
Then if get_template_data() of the component "my_comp" returns following data:
{"my_var":456}
Then the template will be rendered as:
123 # my_var # cheese
-
Because variables "my_var" and "cheese" are searched only inside RootComponent.get_template_data(). But since "cheese" is not defined there, it's empty.
Info
Notice that the variables defined with the {% with %} tag are ignored inside the {% fill %} tag with the "isolated" mode.
\ No newline at end of file
+
Because variables "my_var" and "cheese" are searched only inside RootComponent.get_template_data(). But since "cheese" is not defined there, it's empty.
Info
Notice that the variables defined with the {% with %} tag are ignored inside the {% fill %} tag with the "isolated" mode.
\ No newline at end of file
diff --git a/dev/concepts/advanced/component_libraries/index.html b/dev/concepts/advanced/component_libraries/index.html
index f866d59b..36fa7817 100644
--- a/dev/concepts/advanced/component_libraries/index.html
+++ b/dev/concepts/advanced/component_libraries/index.html
@@ -1,4 +1,4 @@
- Component libraries - Django-Components
In previous examples you could repeatedly see us using @register() to "register" the components. In this section we dive deeper into what it actually means and how you can manage (add or remove) components.
In previous examples you could repeatedly see us using @register() to "register" the components. In this section we dive deeper into what it actually means and how you can manage (add or remove) components.
Django-components provides a seamless integration with HTML fragments with AJAX (HTML over the wire), whether you're using jQuery, HTMX, AlpineJS, vanilla JavaScript, or other.
If the fragment component has any JS or CSS, django-components will:
Automatically load the associated JS and CSS
Ensure that JS is loaded and executed only once even if the fragment is inserted multiple times
Info
What are HTML fragments and "HTML over the wire"?
It is one of the methods for updating the state in the browser UI upon user interaction.
How it works is that:
User makes an action - clicks a button or submits a form
The action causes a request to be made from the client to the server.
Server processes the request (e.g. form submission), and responds with HTML of some part of the UI (e.g. a new entry in a table).
A library like HTMX, AlpineJS, or custom function inserts the new HTML into the correct place.
Django-components provides a seamless integration with HTML fragments with AJAX (HTML over the wire), whether you're using jQuery, HTMX, AlpineJS, vanilla JavaScript, or other.
If the fragment component has any JS or CSS, django-components will:
Automatically load the associated JS and CSS
Ensure that JS is loaded and executed only once even if the fragment is inserted multiple times
Info
What are HTML fragments and "HTML over the wire"?
It is one of the methods for updating the state in the browser UI upon user interaction.
How it works is that:
User makes an action - clicks a button or submits a form
The action causes a request to be made from the client to the server.
Server processes the request (e.g. form submission), and responds with HTML of some part of the UI (e.g. a new entry in a table).
A library like HTMX, AlpineJS, or custom function inserts the new HTML into the correct place.
\ No newline at end of file
diff --git a/dev/concepts/advanced/provide_inject/index.html b/dev/concepts/advanced/provide_inject/index.html
index cc11855d..0384e22e 100644
--- a/dev/concepts/advanced/provide_inject/index.html
+++ b/dev/concepts/advanced/provide_inject/index.html
@@ -1,4 +1,4 @@
- Prop drilling and provide / inject - Django-Components
Prop drilling refers to a scenario in UI development where you need to pass data through many layers of a component tree to reach the nested components that actually need the data.
Normally, you'd use props to send data from a parent component to its children. However, this straightforward method becomes cumbersome and inefficient if the data has to travel through many levels or if several components scattered at different depths all need the same piece of information.
This results in a situation where the intermediate components, which don't need the data for their own functioning, end up having to manage and pass along these props. This clutters the component tree and makes the code verbose and harder to manage.
A neat solution to avoid prop drilling is using the "provide and inject" technique.
With provide / inject, a parent component acts like a data hub for all its descendants. This setup allows any component, no matter how deeply nested it is, to access the required data directly from this centralized provider without having to messily pass props down the chain. This approach significantly cleans up the code and makes it easier to maintain.
Prop drilling refers to a scenario in UI development where you need to pass data through many layers of a component tree to reach the nested components that actually need the data.
Normally, you'd use props to send data from a parent component to its children. However, this straightforward method becomes cumbersome and inefficient if the data has to travel through many levels or if several components scattered at different depths all need the same piece of information.
This results in a situation where the intermediate components, which don't need the data for their own functioning, end up having to manage and pass along these props. This clutters the component tree and makes the code verbose and harder to manage.
A neat solution to avoid prop drilling is using the "provide and inject" technique.
With provide / inject, a parent component acts like a data hub for all its descendants. This setup allows any component, no matter how deeply nested it is, to access the required data directly from this centralized provider without having to messily pass props down the chain. This approach significantly cleans up the code and makes it easier to maintain.
Django-components provides a seamless integration with HTML fragments with AJAX (HTML over the wire), whether you're using jQuery, HTMX, AlpineJS, vanilla JavaScript, or other.
This is achieved by the combination of the "document" and "fragment" strategies.
Django-components provides a seamless integration with HTML fragments with AJAX (HTML over the wire), whether you're using jQuery, HTMX, AlpineJS, vanilla JavaScript, or other.
This is achieved by the combination of the "document" and "fragment" strategies.
\ No newline at end of file
diff --git a/dev/concepts/advanced/tag_formatters/index.html b/dev/concepts/advanced/tag_formatters/index.html
index 986df2a8..c8edc122 100644
--- a/dev/concepts/advanced/tag_formatters/index.html
+++ b/dev/concepts/advanced/tag_formatters/index.html
@@ -1,4 +1,4 @@
- Tag formatters - Django-Components
Template tags introduced by django-components, such as {% component %} and {% slot %}, offer additional features over the default Django template tags:
Template tags introduced by django-components, such as {% component %} and {% slot %}, offer additional features over the default Django template tags:
The simplest way to create a custom template tag is using the template_tag decorator. This decorator allows you to define a template tag by just writing a function that returns the rendered content.
The @djc_test decorator is a powerful tool for testing components created with django-components. It ensures that each test is properly isolated, preventing components registered in one test from affecting others.
The @djc_test decorator is a powerful tool for testing components created with django-components. It ensures that each test is properly isolated, preventing components registered in one test from affecting others.
It can be cumbersome to specify default values for each input in each method.
To make things easier, Components can specify their defaults. Defaults are used when no value is provided, or when the value is set to None for a particular input.
To define defaults for a component, you create a nested Defaults class within your Component class. Each attribute in the Defaults class represents a default value for a corresponding input.
It can be cumbersome to specify default values for each input in each method.
To make things easier, Components can specify their defaults. Defaults are used when no value is provided, or when the value is set to None for a particular input.
To define defaults for a component, you create a nested Defaults class within your Component class. Each attribute in the Defaults class represents a default value for a corresponding input.
To avoid having to manually define the endpoints for each component, you can set the component to be "public" with Component.View.public = True. This will automatically create a URL for the component. To retrieve the component URL, use get_component_url().
In addition, Component has a render_to_response() method that renders the component template based on the provided input and returns an HttpResponse object.
To avoid having to manually define the endpoints for each component, you can set the component to be "public" with Component.View.public = True. This will automatically create a URL for the component. To retrieve the component URL, use get_component_url().
In addition, Component has a render_to_response() method that renders the component template based on the provided input and returns an HttpResponse object.
The most common use of django-components is to render HTML when the server receives a request. As such, there are a few features that are dependent on the request object.
The most common use of django-components is to render HTML when the server receives a request. As such, there are a few features that are dependent on the request object.
\ No newline at end of file
diff --git a/dev/concepts/fundamentals/render_api/index.html b/dev/concepts/fundamentals/render_api/index.html
index 6ef22377..424260f3 100644
--- a/dev/concepts/fundamentals/render_api/index.html
+++ b/dev/concepts/fundamentals/render_api/index.html
@@ -1,4 +1,4 @@
- Render API - Django-Components
When a component is being rendered, whether with Component.render() or {% component %}, a component instance is populated with the current inputs and context. This allows you to access things like component inputs.
We refer to these render-time-only methods and attributes as the "Render API".
Render API is available inside these Component methods:
When a component is being rendered, whether with Component.render() or {% component %}, a component instance is populated with the current inputs and context. This allows you to access things like component inputs.
We refer to these render-time-only methods and attributes as the "Render API".
Render API is available inside these Component methods:
When you write a component, you define its template. The template will always be the same each time you render the component.
However, sometimes you may want to customize the component slightly to change the content of the component. This is where slots come in.
Slots allow you to insert parts of HTML into the component. This makes components more reusable and composable.
<div class="calendar-component"> <div class="header">{# This is where the component will insert the content #}{%slot"header"/%}
@@ -468,4 +468,4 @@
# ❌ Does not workifself.is_filled["my super-slot :)"]:# Do something
-
\ No newline at end of file
+
\ No newline at end of file
diff --git a/dev/concepts/fundamentals/subclassing_components/index.html b/dev/concepts/fundamentals/subclassing_components/index.html
index 6a374f11..b09d209a 100644
--- a/dev/concepts/fundamentals/subclassing_components/index.html
+++ b/dev/concepts/fundamentals/subclassing_components/index.html
@@ -1,4 +1,4 @@
- Subclassing components - Django-Components
In larger projects, you might need to write multiple components with similar behavior. In such cases, you can extract shared behavior into a standalone component class to keep things DRY.
When subclassing a component, there's a couple of things to keep in mind:
If a child component class defines either member of a pair (e.g., either template or template_file), it takes precedence and the parent's definition is ignored completely.
In larger projects, you might need to write multiple components with similar behavior. In such cases, you can extract shared behavior into a standalone component class to keep things DRY.
When subclassing a component, there's a couple of things to keep in mind:
If a child component class defines either member of a pair (e.g., either template or template_file), it takes precedence and the parent's definition is ignored completely.
\ No newline at end of file
diff --git a/dev/concepts/fundamentals/template_tag_syntax/index.html b/dev/concepts/fundamentals/template_tag_syntax/index.html
index e9377ca3..4f7f2f90 100644
--- a/dev/concepts/fundamentals/template_tag_syntax/index.html
+++ b/dev/concepts/fundamentals/template_tag_syntax/index.html
@@ -1,4 +1,4 @@
- Template tag syntax - Django-Components
All template tags in django_component, like {% component %} or {% slot %}, and so on, support extra syntax that makes it possible to write components like in Vue or React (JSX).
When you have a tag like {% component %} or {% slot %}, but it has no content, you can simply append a forward slash / at the end, instead of writing out the closing tags like {% endcomponent %} or {% endslot %}:
So this:
{%component"button"%}{%endcomponent%}
+ Template tag syntax - Django-Components
All template tags in django_component, like {% component %} or {% slot %}, and so on, support extra syntax that makes it possible to write components like in Vue or React (JSX).
When you have a tag like {% component %} or {% slot %}, but it has no content, you can simply append a forward slash / at the end, instead of writing out the closing tags like {% endcomponent %} or {% endslot %}:
\ No newline at end of file
diff --git a/dev/concepts/fundamentals/typing_and_validation/index.html b/dev/concepts/fundamentals/typing_and_validation/index.html
index b18dcb18..36bc01fa 100644
--- a/dev/concepts/fundamentals/typing_and_validation/index.html
+++ b/dev/concepts/fundamentals/typing_and_validation/index.html
@@ -1,4 +1,4 @@
- Typing and validation - Django-Components
Input validation was NOT part of Django Components between versions 0.136 and 0.139 (inclusive).
The Component class optionally accepts class attributes that allow you to define the types of args, kwargs, slots, as well as the data returned from the data methods.
Use this to add type hints to your components, to validate the inputs at runtime, and to document them.
fromtypingimportNamedTuple,Optional
+ Typing and validation - Django-Components
Input validation was NOT part of Django Components between versions 0.136 and 0.139 (inclusive).
The Component class optionally accepts class attributes that allow you to define the types of args, kwargs, slots, as well as the data returned from the data methods.
Use this to add type hints to your components, to validate the inputs at runtime, and to document them.
Next we will add CSS and JavaScript to our template.
Info
In django-components, using JS and CSS is as simple as defining them on the Component class. You don't have to insert the <script> and <link> tags into the HTML manually.
Behind the scenes, django-components keeps track of which components use which JS and CSS files. Thus, when a component is rendered on the page, the page will contain only the JS and CSS used by the components, and nothing more!
Next we will add CSS and JavaScript to our template.
Info
In django-components, using JS and CSS is as simple as defining them on the Component class. You don't have to insert the <script> and <link> tags into the HTML manually.
Behind the scenes, django-components keeps track of which components use which JS and CSS files. Thus, when a component is rendered on the page, the page will contain only the JS and CSS used by the components, and nothing more!
Our calendar component's looking great! But we just got a new assignment from our colleague - The calendar date needs to be shown on 3 different pages:
On one page, it needs to be shown as is
On the second, the date needs to be bold
On the third, the date needs to be in italics
As a reminder, this is what the component's template looks like:
Our calendar component's looking great! But we just got a new assignment from our colleague - The calendar date needs to be shown on 3 different pages:
On one page, it needs to be shown as is
On the second, the date needs to be bold
On the third, the date needs to be in italics
As a reminder, this is what the component's template looks like:
<divclass="calendar"> Today's date is <span>{{date}}</span></div>
There's many ways we could approach this:
Expose the date in a slot
Style .calendar > span differently on different pages
Pass a variable to the component that decides how the date is rendered
Create a new component
First two options are more flexible, because the custom styling is not baked into a component's implementation. And for the sake of demonstration, we'll solve this challenge with slots.
When a component is used inside another template, slots allow the parent template to override specific parts of the child component by passing in different content.
This mechanism makes components more reusable and composable.
Let's update our calendar component to support more customization. We'll add {% slot %} tag to the template:
<divclass="calendar">
@@ -117,4 +117,4 @@
<divclass="calendar"> Today's date is <i>2024-12-16</i></div>
-
Info
When to use slots vs variables?
Generally, slots are more flexible - you can access the slot data, even the original slot content. Thus, slots behave more like functions that render content based on their context.
On the other hand, variables are simpler - the variable you pass to a component is what will be used.
Moreover, slots are treated as part of the template - for example the CSS scoping (work in progress) is applied to the slot content too.
\ No newline at end of file
+
Info
When to use slots vs variables?
Generally, slots are more flexible - you can access the slot data, even the original slot content. Thus, slots behave more like functions that render content based on their context.
On the other hand, variables are simpler - the variable you pass to a component is what will be used.
Moreover, slots are treated as part of the template - for example the CSS scoping (work in progress) is applied to the slot content too.
\ No newline at end of file
diff --git a/dev/getting_started/components_in_templates/index.html b/dev/getting_started/components_in_templates/index.html
index 7e32d0cc..55181fb1 100644
--- a/dev/getting_started/components_in_templates/index.html
+++ b/dev/getting_started/components_in_templates/index.html
@@ -1,4 +1,4 @@
- Components in templates - Django-Components
\ No newline at end of file
diff --git a/dev/guides/devguides/dependency_mgmt/index.html b/dev/guides/devguides/dependency_mgmt/index.html
index a89b99fb..40304889 100644
--- a/dev/guides/devguides/dependency_mgmt/index.html
+++ b/dev/guides/devguides/dependency_mgmt/index.html
@@ -1,4 +1,4 @@
- JS and CSS rendering - Django-Components
First of all, when we consider a component, it has two kind of dependencies - the "inlined" JS and CSS, and additional linked JS and CSS via Media.js/css:
fromdjango_componentsimportComponent,types
+ JS and CSS rendering - Django-Components