# Share a value between template components

Source: https://zotonic.com/cookbook/1588/share-a-value-between-template-components

URI: https://zotonic.com/id/1588

Language: en

Pass values explicitly and keep browser code in the supported script tag.

A variable created inside an included template does not become a global variable for the caller. Calculate the value in the caller and pass it to each component:

```django
{% with id.title as heading %}
    {% include "_garden_heading.tpl" heading=heading %}
    {% include "_garden_details.tpl" heading=heading %}
{% endwith %}
```

Use <a href="/id/1383" class="doc-reference doc-reference-tag"><code>tag#with</code></a> for a local binding, <a href="/id/1326" class="doc-reference doc-reference-tag"><code>tag#include</code></a> for a component and <a href="/id/2506" class="doc-reference doc-reference-tag"><code>tag#compose</code></a> when the caller must supply blocks to that component. Keep related markup and its initialization together where practical.

For browser initialization, use `{% javascript %}` so the normal script pipeline handles it. If data must enter JavaScript, use the JavaScript/JSON encoding appropriate to that context; HTML escaping is not a JavaScript encoder. For example:

```django
<span id="{{ #heading }}">{{ id.title }}</span>
{% javascript %}
    const heading = document.getElementById("{{ #heading }}");
    heading.classList.add("is-ready");
{% endjavascript %}
```

Here the browser reads the already rendered element; resource text is not interpolated into a script literal.

License: 

Attribution: 

Related resources: https://zotonic.com/api/agent/related/1588?language=en


## API access

- [API help](<https://zotonic.com/api-help>)
- [OpenAPI specification](<https://zotonic.com/openapi.json>)
- [This resource as JSON](<https://zotonic.com/api/agent/resource/1588?language=en>)
