Skip to content
DevinimJS
Phase 1 · Lesson 6

Attributes & Data — Passing Configuration

Use data-* attributes to configure components from HTML. PHP, static sites, and CMS backends can all provide initial values.

What you'll learn
  • Read configuration from HTML with this.str(), this.num(), this.bool(), and this.json()
  • Provide sensible defaults for every attribute
  • React to live attribute changes with observedAttributes and onAttribute()
  • Use data-* as the bridge between server-rendered HTML and client-side reactivity

The data-* contract

DevinimJS components read their configuration from this.dataset. The HTML (or PHP template) provides values:

<dv-counter
  data-start="10"
  data-step="5"
  data-label="Quantity"
  data-disabled="false"
></dv-counter>

Four attribute helpers

HelperReadsReturnsExample
this.str(key, fallback)data-keystringthis.str('label', 'Default')
this.num(key, fallback)data-keyfinite numberthis.num('start', 0)
this.bool(key, fallback)data-keybooleanthis.bool('disabled', false)
this.json(key, fallback)data-keyparsed JSONthis.json('items', [])

str() — string values

initialState() {
  return {
    label: this.str('label', 'Submit'),
    placeholder: this.str('placeholder', 'Enter value...'),
  };
}

num() — numeric values

Returns a finite number. Non-numeric values fall back to the default:

initialState() {
  return {
    min: this.num('min', 0),
    max: this.num('max', 100),
    step: this.num('step', 1),
  };
}
// data-min="0" data-max="100" data-step="5"
// If data-step is missing or "abc", returns 1 (the fallback)

bool() — boolean values

Understands 'false' and '0' as false. Everything else is true:

initialState() {
  return {
    disabled: this.bool('disabled', false),
    required: this.bool('required', false),
    autofocus: this.bool('autofocus', false),
  };
}
// data-disabled  → true  (present with any value except 'false'/'0')
// data-disabled="false" → false
// no attribute   → false (the fallback)

json() — structured data

Pass arrays and objects from the server. Use single quotes in HTML attributes:

<!-- HTML -->
<dv-product-list data-items='[{"id":1,"name":"Widget"},{"id":2,"name":"Gadget"}]'></dv-product-list>
initialState() {
  return {
    items: this.json('items', []),
  };
}

PHP integration pattern

In PHP: <dv-product-list data-items='<?= json_encode($products) ?>'>. The server renders the data as JSON in the attribute. The component reads it at mount time.

Live attribute changes with observedAttributes

If an attribute changes after the component is connected (e.g., via setAttribute), the component can react:

static observedAttributes = ['data-start'];

onAttribute(name, newValue, oldValue) {
  if (name === 'data-start') {
    this.state.count = Number(newValue) || 0;
  }
}

Note: observedAttributes only works with the exact attribute name including the data- prefix.

Live demo: Configurable badge

Live demo — configured from data-* attributes

Recap

Four helpers

str(), num(), bool(), json() — typed reads from data-*.

Always provide fallbacks

The second argument is the default. Your component works even if attributes are missing.

Server-friendly

PHP, Laravel, WordPress — any backend can write data-* attributes.

Live updates

Use observedAttributes + onAttribute() for post-connect attribute changes.