diff --git a/dev/reference/api/index.html b/dev/reference/api/index.html index ec0e3246..ee2a5fa3 100644 --- a/dev/reference/api/index.html +++ b/dev/reference/api/index.html @@ -362,7 +362,7 @@

SlotContent module-attribute ¤

SlotContent = Union[SlotResult, SlotFunc[TSlotData], 'Slot[TSlotData]']
 

SlotFunc ¤

SlotRef ¤

SlotRef(slot: SlotNode, context: Context)
 

Bases: object

See source code

SlotRef allows to treat a slot as a variable. The slot is rendered only once the instance is coerced to string.

This is used to access slots as variables inside the templates. When a SlotRef is rendered in the template with {{ my_lazy_slot }}, it will output the contents of the slot.

SlotResult module-attribute ¤

SlotResult = Union[str, SafeString]
-

TagFormatterABC ¤

Bases: abc.ABC

See source code

Abstract base class for defining custom tag formatters.

Tag formatters define how the component tags are used in the template.

Read more about Tag formatter.

For example, with the default tag formatter (ComponentFormatter), components are written as:

{% component "comp_name" %}
+

TagFormatterABC ¤

Bases: abc.ABC

See source code

Abstract base class for defining custom tag formatters.

Tag formatters define how the component tags are used in the template.

Read more about Tag formatter.

For example, with the default tag formatter (ComponentFormatter), components are written as:

{% component "comp_name" %}
 {% endcomponent %}
 

While with the shorthand tag formatter (ShorthandComponentFormatter), components are written as:

{% comp_name %}
 {% endcomp_name %}
@@ -380,16 +380,16 @@
         name = tokens.pop(0)
         return TagResult(name, tokens)
 

Methods:

end_tag abstractmethod ¤

end_tag(name: str) -> str
-

See source code

Formats the end tag of a block component.

Parameters:

  • name (str) –

    Component's registered name. Required.

Returns:

  • str ( str ) –

    The formatted end tag.

parse abstractmethod ¤

parse(tokens: List[str]) -> TagResult
-

See source code

Given the tokens (words) passed to a component start tag, this function extracts the component name from the tokens list, and returns TagResult, which is a tuple of (component_name, remaining_tokens).

Parameters:

  • tokens ([List(str]) –

    List of tokens passed to the component tag.

Returns:

  • TagResult ( TagResult ) –

    Parsed component name and remaining tokens.

Example:

Assuming we used a component in a template like this:

{% component "my_comp" key=val key2=val2 %}
+

See source code

Formats the end tag of a block component.

Parameters:

  • name (str) –

    Component's registered name. Required.

Returns:

  • str ( str ) –

    The formatted end tag.

parse abstractmethod ¤

parse(tokens: List[str]) -> TagResult
+

See source code

Given the tokens (words) passed to a component start tag, this function extracts the component name from the tokens list, and returns TagResult, which is a tuple of (component_name, remaining_tokens).

Parameters:

  • tokens ([List(str]) –

    List of tokens passed to the component tag.

Returns:

  • TagResult ( TagResult ) –

    Parsed component name and remaining tokens.

Example:

Assuming we used a component in a template like this:

{% component "my_comp" key=val key2=val2 %}
 {% endcomponent %}
 

This function receives a list of tokens:

['component', '"my_comp"', 'key=val', 'key2=val2']
 
  • component is the tag name, which we drop.
  • "my_comp" is the component name, but we must remove the extra quotes.
  • The remaining tokens we pass unmodified, as that's the input to the component.

So in the end, we return:

TagResult('my_comp', ['key=val', 'key2=val2'])
 

start_tag abstractmethod ¤

start_tag(name: str) -> str
-

See source code

Formats the start tag of a component.

Parameters:

  • name (str) –

    Component's registered name. Required.

Returns:

  • str ( str ) –

    The formatted start tag.

TagResult ¤

Bases: tuple

See source code

The return value from TagFormatter.parse().

Read more about Tag formatter.

Attributes:

component_name instance-attribute ¤

component_name: str
-

See source code

Component name extracted from the template tag

For example, if we had tag

{% component "my_comp" key=val key2=val2 %}
+

See source code

Formats the start tag of a component.

Parameters:

  • name (str) –

    Component's registered name. Required.

Returns:

  • str ( str ) –

    The formatted start tag.

TagResult ¤

Bases: tuple

See source code

The return value from TagFormatter.parse().

Read more about Tag formatter.

Attributes:

component_name instance-attribute ¤

component_name: str
+

See source code

Component name extracted from the template tag

For example, if we had tag

{% component "my_comp" key=val key2=val2 %}
 

Then component_name would be my_comp.

tokens instance-attribute ¤

tokens: List[str]
-

See source code

Remaining tokens (words) that were passed to the tag, with component name removed

For example, if we had tag

{% component "my_comp" key=val key2=val2 %}
+

See source code

Remaining tokens (words) that were passed to the tag, with component name removed

For example, if we had tag

{% component "my_comp" key=val key2=val2 %}
 

Then tokens would be ['key=val', 'key2=val2'].

autodiscover ¤

autodiscover(map_module: Optional[Callable[[str], str]] = None) -> List[str]
 

See source code

Search for all python files in COMPONENTS.dirs and COMPONENTS.app_dirs and import them.

See Autodiscovery.

Parameters:

  • map_module (Callable[[str], str], default: None ) –

    Map the module paths with map_module function. This serves as an escape hatch for when you need to use this function in tests.

Returns:

  • List[str]

    List[str]: A list of module paths of imported files.

To get the same list of modules that autodiscover() would return, but without importing them, use get_component_files():

from django_components import get_component_files
 
diff --git a/dev/reference/tag_formatters/index.html b/dev/reference/tag_formatters/index.html
index 76e09f72..553f47d5 100644
--- a/dev/reference/tag_formatters/index.html
+++ b/dev/reference/tag_formatters/index.html
@@ -1,10 +1,10 @@
- Tag formatters - Django-Components      

Tag Formatters¤

Tag formatters allow you to change the syntax for calling components from within the Django templates.

Tag formatter are set via the tag_formatter setting.

Available tag formatters¤

ComponentFormatter ¤

Bases: django_components.tag_formatter.TagFormatterABC

See source code

The original django_component's component tag formatter, it uses the {% component %} and {% endcomponent %} tags, and the component name is given as the first positional arg.

Example as block:

{% component "mycomp" abc=123 %}
+ Tag formatters - Django-Components      

Tag Formatters¤

Tag formatters allow you to change the syntax for calling components from within the Django templates.

Tag formatter are set via the tag_formatter setting.

Available tag formatters¤

ComponentFormatter ¤

Bases: django_components.tag_formatter.TagFormatterABC

See source code

The original django_component's component tag formatter, it uses the {% component %} and {% endcomponent %} tags, and the component name is given as the first positional arg.

Example as block:

{% component "mycomp" abc=123 %}
     {% fill "myfill" %}
         ...
     {% endfill %}
 {% endcomponent %}
 

Example as inlined tag:

{% component "mycomp" abc=123 / %}
-

ShorthandComponentFormatter ¤

Bases: django_components.tag_formatter.TagFormatterABC

See source code

The component tag formatter that uses {% <name> %} / {% end<name> %} tags.

This is similar to django-web-components and django-slippers syntax.

Example as block:

{% mycomp abc=123 %}
+

ShorthandComponentFormatter ¤

Bases: django_components.tag_formatter.TagFormatterABC

See source code

The component tag formatter that uses {% <name> %} / {% end<name> %} tags.

This is similar to django-web-components and django-slippers syntax.

Example as block:

{% mycomp abc=123 %}
     {% fill "myfill" %}
         ...
     {% endfill %}
diff --git a/dev/reference/template_tags/index.html b/dev/reference/template_tags/index.html
index ca4d03f9..e9e25be0 100644
--- a/dev/reference/template_tags/index.html
+++ b/dev/reference/template_tags/index.html
@@ -1,7 +1,7 @@
  Template tags - Django-Components      

Template tags¤

All following template tags are defined in

django_components.templatetags.component_tags

Import as

{% load component_tags %}
 

component¤

{% component [arg, ...] **kwargs [only] %}
 {% endcomponent %}
-

See source code

Renders one of the components that was previously registered with @register() decorator.

Args:

  • name (str, required): Registered name of the component to render
  • All other args and kwargs are defined based on the component itself.

If you defined a component "my_table"

from django_component import Component, register
+

See source code

Renders one of the components that was previously registered with @register() decorator.

Args:

  • name (str, required): Registered name of the component to render
  • All other args and kwargs are defined based on the component itself.

If you defined a component "my_table"

from django_component import Component, register
 
 @register("my_table")
 class MyTable(Component):
@@ -39,10 +39,10 @@
 {% endcomponent %}
 

Isolating components¤

By default, components behave similarly to Django's {% include %}, and the template inside the component has access to the variables defined in the outer template.

You can selectively isolate a component, using the only flag, so that the inner template can access only the data that was explicitly passed to it:

{% component "name" positional_arg keyword_arg=value ... only %}
 

component_css_dependencies¤

{% component_css_dependencies %}
-

See source code

Marks location where CSS link tags should be rendered after the whole HTML has been generated.

Generally, this should be inserted into the <head> tag of the HTML.

If the generated HTML does NOT contain any {% component_css_dependencies %} tags, CSS links are by default inserted into the <head> tag of the HTML. (See JS and CSS output locations)

Note that there should be only one {% component_css_dependencies %} for the whole HTML document. If you insert this tag multiple times, ALL CSS links will be duplicately inserted into ALL these places.

component_js_dependencies¤

{% component_js_dependencies %}
-

See source code

Marks location where JS link tags should be rendered after the whole HTML has been generated.

Generally, this should be inserted at the end of the <body> tag of the HTML.

If the generated HTML does NOT contain any {% component_js_dependencies %} tags, JS scripts are by default inserted at the end of the <body> tag of the HTML. (See JS and CSS output locations)

Note that there should be only one {% component_js_dependencies %} for the whole HTML document. If you insert this tag multiple times, ALL JS scripts will be duplicately inserted into ALL these places.

fill¤

{% fill name data=None default=None %}
+

See source code

Marks location where CSS link tags should be rendered after the whole HTML has been generated.

Generally, this should be inserted into the <head> tag of the HTML.

If the generated HTML does NOT contain any {% component_css_dependencies %} tags, CSS links are by default inserted into the <head> tag of the HTML. (See JS and CSS output locations)

Note that there should be only one {% component_css_dependencies %} for the whole HTML document. If you insert this tag multiple times, ALL CSS links will be duplicately inserted into ALL these places.

component_js_dependencies¤

{% component_js_dependencies %}
+

See source code

Marks location where JS link tags should be rendered after the whole HTML has been generated.

Generally, this should be inserted at the end of the <body> tag of the HTML.

If the generated HTML does NOT contain any {% component_js_dependencies %} tags, JS scripts are by default inserted at the end of the <body> tag of the HTML. (See JS and CSS output locations)

Note that there should be only one {% component_js_dependencies %} for the whole HTML document. If you insert this tag multiple times, ALL JS scripts will be duplicately inserted into ALL these places.

fill¤

{% fill name data=None default=None %}
 {% endfill %}
-

See source code

Use this tag to insert content into component's slots.

{% fill %} tag may be used only within a {% component %}..{% endcomponent %} block. Runtime checks should prohibit other usages.

Args:

  • name (str, required): Name of the slot to insert this content into. Use "default" for the default slot.
  • default (str, optional): This argument allows you to access the original content of the slot under the specified variable name. See Accessing original content of slots
  • data (str, optional): This argument allows you to access the data passed to the slot under the specified variable name. See Scoped slots

Examples:

Basic usage:

{% component "my_table" %}
+

See source code

Use this tag to insert content into component's slots.

{% fill %} tag may be used only within a {% component %}..{% endcomponent %} block. Runtime checks should prohibit other usages.

Args:

  • name (str, required): Name of the slot to insert this content into. Use "default" for the default slot.
  • default (str, optional): This argument allows you to access the original content of the slot under the specified variable name. See Accessing original content of slots
  • data (str, optional): This argument allows you to access the data passed to the slot under the specified variable name. See Scoped slots

Examples:

Basic usage:

{% component "my_table" %}
   {% fill "pagination" %}
     < 1 | 2 | 3 >
   {% endfill %}
@@ -84,7 +84,7 @@
   {% endfill %}
 {% endcomponent %}
 

html_attrs¤

{% html_attrs attrs=None defaults=None **kwargs %}
-

See source code

Generate HTML attributes (key="value"), combining data from multiple sources, whether its template variables or static text.

It is designed to easily merge HTML attributes passed from outside with the internal. See how to in Passing HTML attributes to components.

Args:

  • attrs (dict, optional): Optional dictionary that holds HTML attributes. On conflict, overrides values in the default dictionary.
  • default (str, optional): Optional dictionary that holds HTML attributes. On conflict, is overriden with values in the attrs dictionary.
  • Any extra kwargs will be appended to the corresponding keys

The attributes in attrs and defaults are merged and resulting dict is rendered as HTML attributes (key="value").

Extra kwargs (key=value) are concatenated to existing keys. So if we have

attrs = {"class": "my-class"}
+

See source code

Generate HTML attributes (key="value"), combining data from multiple sources, whether its template variables or static text.

It is designed to easily merge HTML attributes passed from outside with the internal. See how to in Passing HTML attributes to components.

Args:

  • attrs (dict, optional): Optional dictionary that holds HTML attributes. On conflict, overrides values in the default dictionary.
  • default (str, optional): Optional dictionary that holds HTML attributes. On conflict, is overriden with values in the attrs dictionary.
  • Any extra kwargs will be appended to the corresponding keys

The attributes in attrs and defaults are merged and resulting dict is rendered as HTML attributes (key="value").

Extra kwargs (key=value) are concatenated to existing keys. So if we have

attrs = {"class": "my-class"}
 

Then

{% html_attrs attrs class="extra-class" %}
 

will result in class="my-class extra-class".

Example:

<div {% html_attrs
     attrs
@@ -95,7 +95,7 @@
 

renders

<div class="my-class extra-class" data-id="123">
 

See more usage examples in HTML attributes.

provide¤

{% provide name **kwargs %}
 {% endprovide %}
-

See source code

The "provider" part of the provide / inject feature. Pass kwargs to this tag to define the provider's data. Any components defined within the {% provide %}..{% endprovide %} tags will be able to access this data with Component.inject().

This is similar to React's ContextProvider, or Vue's provide().

Args:

  • name (str, required): Provider name. This is the name you will then use in Component.inject().
  • **kwargs: Any extra kwargs will be passed as the provided data.

Example:

Provide the "user_data" in parent component:

@register("parent")
+

See source code

The "provider" part of the provide / inject feature. Pass kwargs to this tag to define the provider's data. Any components defined within the {% provide %}..{% endprovide %} tags will be able to access this data with Component.inject().

This is similar to React's ContextProvider, or Vue's provide().

Args:

  • name (str, required): Provider name. This is the name you will then use in Component.inject().
  • **kwargs: Any extra kwargs will be passed as the provided data.

Example:

Provide the "user_data" in parent component:

@register("parent")
 class Parent(Component):
     template = """
       <div>
@@ -126,7 +126,7 @@
 

❌ Don't do this

user = self.inject("user_data")["user"]
 

slot¤

{% slot name **kwargs [default] [required] %}
 {% endslot %}
-

See source code

Slot tag marks a place inside a component where content can be inserted from outside.

Learn more about using slots.

This is similar to slots as seen in Web components, Vue or React's children.

Args:

  • name (str, required): Registered name of the component to render
  • default: Optional flag. If there is a default slot, you can pass the component slot content without using the {% fill %} tag. See Default slot
  • required: Optional flag. Will raise an error if a slot is required but not given.
  • **kwargs: Any extra kwargs will be passed as the slot data.

Example:

@register("child")
+

See source code

Slot tag marks a place inside a component where content can be inserted from outside.

Learn more about using slots.

This is similar to slots as seen in Web components, Vue or React's children.

Args:

  • name (str, required): Registered name of the component to render
  • default: Optional flag. If there is a default slot, you can pass the component slot content without using the {% fill %} tag. See Default slot
  • required: Optional flag. Will raise an error if a slot is required but not given.
  • **kwargs: Any extra kwargs will be passed as the slot data.

Example:

@register("child")
 class Child(Component):
     template = """
       <div>
diff --git a/versions.json b/versions.json
index 2abc1632..c1a38633 100644
--- a/versions.json
+++ b/versions.json
@@ -1,7 +1,7 @@
 [
   {
     "version": "dev",
-    "title": "dev (db4ca8b)",
+    "title": "dev (894dee3)",
     "aliases": []
   },
   {