> For the complete documentation index, see [llms.txt](https://blocksmd.gitbook.io/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://blocksmd.gitbook.io/docs/data-binding.md).

# Data-binding

## Data-binding, `<div>` and `<span>` elements

Create wrapping `<div>` elements by putting content inside pairs of `:::`. The content inside can be any valid Markdown, such as headings, paragraphs, lists, form fields, etc. Class names and other attributes are supported via `[...]` ([learn more](/docs/class-names-and-attributes.md)). Moreover, you can bind one or more fields to a `<div>` element by adding the names of the fields inside `{$...$}` (separated by spaces), and placing this within the `[...]`. This means that whenever the value of a binded field changes, the content inside the `<div>` will be automatically re-rendered.

The templating is done using [Nunjucks](https://mozilla.github.io/nunjucks/), so its entire list of features such as if-else statements, loops, filters, etc. are fully supported. You can also bind a single field to a `<span>` element using `{$ field $}`. This is obviously not as flexible, but it is simple, and can be used inside paragraphs, headings, list items, etc.

```
product* = SelectBox(
  | question = Product | options = T-Shirts, Socks
  | selected = T-Shirts | subfield
)

price* = NumberInput(
  | question = Price | unitEnd = $ | subfield | min = 1
)

quantity* = NumberInput(
  | question = Quantity | subfield | min = 1
)

[.col-4]
Selected: {$ product $}

::: [.col-8 .text-end .xs:text-start {$ price quantity $}]
{% if price and quantity -%}
  Total: ${{ price }} × {{ quantity }} = ${{ price * quantity }}
{% else -%}
  Total: Set price and quantity
{% endif %}
:::
```

Moreover, you can use data-binding to dynamically update class names and other attributes based on user input. In the example below, the `color` and `background-color` of the paragraph and the list within the `<div>` element will change depending on what is chosen by the user.

```
color* = ChoiceInput(
  | question = Choose a color
  | choices = Red "red", Blue "blue"
)

::: [{$ color $}]
[.col-6 style="color: {{ color }}"]
Paragraph

- [.col-6 style="background-color: {{ color }}"]
- Item
- Item
:::
```

Here's another example showing how to dynamically change the code depending on the choice of language:

````
#! page = single

language* = ChoiceInput(
  | question = What is your language of choice?
  | choices = Python, JavaScript
  | checked = Python
)

::: [{$ language $}]
{% if language == "Python" %}
```Python
def add(a, b):
  return a + b
```
{% else %}
```JavaScript
function add(a, b) {
  return a + b;
}
```
{% endif %}
:::
````


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://blocksmd.gitbook.io/docs/data-binding.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
