Binding attributes with data-attribute¶
data-field places a JSON value as text inside an element. Add data-attribute and the
value goes into an HTML attribute instead — src, href, title, style, class, or
a custom data-* attribute that your CSS can target. The examples use this item:
{
"title": "Keynote Session",
"status": "Canceled",
"sessionCode": "TRACK1",
"trackCategory": "technical",
"speakerPhoto": "/api/static/templates/6/assets/speakers/jane.png",
"website": "https://conference.example.com",
"colorStyle": "background-color: #3498db;"
}
Basic usage¶
<img data-field="speakerPhoto" data-attribute="src" alt="Speaker photo" />
<a data-field="website" data-attribute="href" target="_blank">Visit Website</a>
<div data-field="colorStyle" data-attribute="style">Color Box</div>
<p data-field="title" data-attribute="title" class="session-title"></p>
After rendering, the image has src="/api/static/…/jane.png", the link
href="https://conference.example.com", the box style="background-color: #3498db;", and
the paragraph a tooltip. A style value must be a valid CSS declaration string; for
anything beyond a color, prefer classes. The attribute is only rewritten when the value
changes, so an <img> or <iframe> does not reload on every refresh.
data-attribute does not also display the text. An element with both attributes only
gets the attribute. When you need the value as text and as a styling hook, use two
elements: a wrapper that sets the attribute, and a child that shows the text.
Coloring an element by its own value¶
<div class="title" data-field="sessionCode" data-attribute="data-session-code">
<span class="session-code-text" data-field="sessionCode"></span>
</div>
.title[data-session-code="TRACK1"] .session-code-text { color: #f39c12; }
.title[data-session-code="KEYNOTE"] .session-code-text { color: #16a085; }
.title[data-session-code="WORKSHOP"] .session-code-text { color: #0b66b6; }
The wrapper receives data-session-code="TRACK1", the span shows TRACK1, and the CSS
attribute selector picks the color. A value with no matching rule keeps the default
styling — no fallback rule is needed.
Styling by a different field¶
The wrapper can read one field while the child shows another. Here the title is struck through when the status is Canceled:
<span class="title-wrapper" data-field="status" data-attribute="data-status">
<span class="title-text" data-field="title"></span>
</span>
Both patterns combine freely, and data-condition can hide a wrapper altogether — for
example data-condition="sessionCode" on the code's wrapper and its separator, so items
without a code show no empty badge:
<div class="title" data-field="sessionCode" data-attribute="data-session-code">
<span class="session-code-text" data-field="sessionCode" data-condition="sessionCode"></span>
<span class="separator" data-condition="sessionCode"> | </span>
<span class="title-wrapper" data-field="status" data-attribute="data-status">
<span class="title-text" data-field="title"></span>
</span>
</div>
A category badge works the same way — wrapper data-field="trackCategory"
data-attribute="data-category", child data-field="trackCategory", and one CSS rule per
category setting the badge's background.
Guidelines¶
data-fieldalone for text;data-field+data-attributefor an attribute; the wrapper pattern when you need both.- Safe targets:
src,href,title,alt,class,id,style,data-*. Values are not sanitized — avoidonclick,onloadand other event handlers, andjavascript:URLs. - The syntax rules and the behavior for missing fields are in the Data binding reference.
Related¶
- Displaying JSON values
- Data binding reference
- Background and banner images — when the image is part of the layout rather than the data