sercrod

:src

Summary

:src is an attribute binding directive that controls the src attribute of an image, script, or media element. It evaluates a Sercrod expression and writes the result into the src attribute, passing the value through a configurable url filter. The binding is one way: data changes update the DOM attribute, but changes to the DOM attribute do not update data.

:src belongs to the URL binding family together with :href, :action, :formaction, and xlink:href.

Basic example

Simple image binding:

<serc-rod id="app" data='{
  "imageUrl": "/assets/logo.png"
}'>
  <img :src="imageUrl" alt="Logo">
</serc-rod>

Behavior:

Behavior

Core rules:

Evaluation timing

:src is evaluated during the element rendering phase alongside other colon style attribute bindings.

Evaluation order, simplified:

There is no special delay, scheduling, or async behavior associated specifically with :src. It participates in the normal synchronous render cycle of the host.

Execution model

Conceptually, the runtime behaves as follows when it encounters a node with :src:

  1. Sercrod detects an attribute whose name starts with ":" and whose key slice is "src".

  2. It reads the attribute value string, for example imageUrl or cdnBase + "/images/" + fileName.

  3. It evaluates this string as an expression with eval_expr, using the current scope and a mode of attr:src, with el set to the current element.

  4. It inspects the evaluated result:

    • If the result is strictly false, or is null or undefined, Sercrod removes the src attribute from the element and stops.
    • Otherwise, the result is stringified.
  5. Sercrod calls the url filter with the string value, the attribute name "src", and a context including the element and scope.

  6. If the filter returns a truthy value, Sercrod assigns that value to the element’s src attribute.

  7. If any exception occurs during evaluation or filtering, Sercrod sets src to an empty string as a fallback.

  8. If cleanup.handlers is enabled, Sercrod removes the :src attribute from the element’s attribute list, leaving only the resolved src.

This process repeats on subsequent renders when data changes cause the element to be re rendered.

Use with structural directives and loops

Because :src is purely an attribute binding, it composes naturally with structural directives around it.

Examples:

There are no special restrictions on combining :src with *if, *for, *each, *switch, or template directives. They operate at different layers: structural directives control presence and shape of elements, while :src binds a single attribute on each element.

Best practices

Additional examples

Responsive image selection:

<serc-rod id="hero" data='{
  "small": "/img/hero-small.jpg",
  "large": "/img/hero-large.jpg",
  "isMobile": false
}'>
  <img :src="isMobile ? small : large" alt="Hero">
</serc-rod>

When isMobile is true, :src resolves to small; when false, to large. A data update followed by a re render switches the image.

Scripts based on environment:

<serc-rod id="analytics" data='{
  "env": "production",
  "scripts": {
    "staging": "https://staging.example.com/analytics.js",
    "production": "https://cdn.example.com/analytics.js"
  }
}'>
  <script :src="scripts[env]"></script>
</serc-rod>

By changing env in data, you can switch which analytics script is loaded without modifying markup.

Notes