sercrod

:xlink:href

Summary

:xlink:href is an attribute binding directive that controls the xlink:href attribute of an SVG element. It evaluates a Sercrod expression and writes the result into xlink:href, passing the value through the configurable url filter. The binding is one way: data updates change the DOM attribute, but changes to the DOM attribute do not update data.

:xlink:href is part of the generic colon attribute family and shares the same evaluation rules as :href, :src, :action, and :formaction.

Basic example

Referencing an SVG symbol by id:

<serc-rod id="icons" data='{
  "iconRef": "#icon-check"
}'>
  <svg viewBox="0 0 24 24" aria-hidden="true">
    <defs>
      <symbol id="icon-check" viewBox="0 0 24 24">
        <path d="M3 12l6 6L21 4"></path>
      </symbol>
    </defs>

    <use :xlink:href="iconRef"></use>
  </svg>
</serc-rod>

Behavior:

Behavior

Core rules:

Evaluation timing

:xlink:href is evaluated during the element rendering phase, together with other colon style attribute bindings.

More precisely:

There is no dedicated scheduling or delay specific to :xlink:href. It participates in the normal render update cycle of the host.

Execution model

Conceptually, when Sercrod renders an element with :xlink:href, the steps are:

  1. Sercrod encounters a node that has an attribute named :xlink:href.

  2. It reads the expression string from that attribute, for example iconRef or spriteBase + '#' + name.

  3. It evaluates the expression with eval_expr, using the current scope and a context that sets mode to attr:xlink:href and el to the current element.

  4. It inspects the result:

    • If the result is strictly false, or is null or undefined, Sercrod removes the xlink:href attribute and stops.
    • Otherwise, it converts the result to a string and passes it to the url filter, along with the attribute name "xlink:href" and a context with the element and scope.
  5. If the url filter returns a truthy string, Sercrod sets the element attribute xlink:href to that string.

  6. If evaluation throws an exception, Sercrod falls back to setting xlink:href to an empty string.

  7. If cleanup.handlers is enabled in the configuration, Sercrod removes the original :xlink:href attribute from the element, leaving only the concrete xlink:href attribute in the output DOM.

Use with structural directives and loops

:xlink:href is an attribute binding and composes freely with structural directives around it.

Typical combinations:

There are no special restrictions on combining :xlink:href with structural directives such as *if, *for, or *each, because those operate at the element or container level, while :xlink:href only binds an attribute.

Best practices

Additional examples

Using a mapping table:

<serc-rod id="icons" data='{
  "icons": {
    "success": "#icon-check",
    "error":   "#icon-cross"
  },
  "status": "success"
}'>
  <svg viewBox="0 0 24 24" aria-hidden="true">
    <use :xlink:href="icons[status]"></use>
  </svg>
</serc-rod>

Here, changing status between "success" and "error" automatically switches the icon used by the use element.

Optional icon:

<serc-rod id="maybe-icon" data='{
  "showIcon": false,
  "iconRef": "#icon-info"
}'>
  <svg viewBox="0 0 24 24" aria-hidden="true">
    <use :xlink:href="showIcon ? iconRef : null"></use>
  </svg>
</serc-rod>

When showIcon is false, :xlink:href removes the attribute and the use element no longer references any symbol.

Notes