v0.6.22

Attribute Binding

Last updated


This page covers binding data to HTML attributes with the : or q- prefix, and how :hide is evaluated.

Syntax


  1. <div id="app">
  2. <a :href="link" :title="label">{{ label }}</a>
  3. <img :src="avatar" :alt="name" />
  4. <button q-data-id="userId">Edit</button>
  5. <a :href="/user/{{ userId }}">Profile</a>
  6. </div>
  7. <script>
  8. const app = new QUI({
  9. id: "app",
  10. data: {
  11. link: "https://quickui.pardn.io",
  12. label: "Docs",
  13. avatar: "https://quickui.pardn.io/favicon.png",
  14. name: "QuickUI",
  15. userId: 42,
  16. },
  17. when: {
  18. rendered: () => [...$("app").children].forEach((e) => console.log(e.outerHTML)),
  19. },
  20. });
  21. </script>
Live preview · @pardnchiu/quickui@0.6.22

PrefixDescription
:Standard form, for example :href
q-Equivalent form, for example q-href; useful for toolchains that reject : in attribute names

The attribute value is first looked up as a data path; when nothing matches, the value itself is used. When the value contains {{ }}, only the first {{ }} is replaced and the surrounding text is kept. The original : attribute is removed afterwards.

Dispatch Rules


AttributeWritten as
:id, :src, :alt, :hrefElement property
:htmlinnerHTML, see Text Binding
: prefix and the name matches a CSS property (such as :width, :color)Written to style, see Style Binding
:hideSets display: none when truthy
:lazyloadSee Image Lazy Loading
Anything else (such as :title, :data-id, :value)setAttribute with the prefix stripped

Names that match a CSS property always go to style: <img :width="w"> sets the CSS width, not the width attribute.

:hide


  1. <div id="app">
  2. <div :hide="isHidden">Content</div>
  3. <button @click="toggle">Toggle</button>
  4. </div>
  5. <script>
  6. const app = new QUI({
  7. id: "app",
  8. data: { isHidden: false },
  9. event: {
  10. toggle: () => {
  11. app.data.isHidden = !app.data.isHidden;
  12. },
  13. },
  14. when: {
  15. updated: () => console.log(app.data.isHidden, $("app").children[0].outerHTML),
  16. },
  17. });
  18. </script>
Live preview · @pardnchiu/quickui@0.6.22

ValueResult
true, non-zero numbers, non-empty stringsHidden
false, 0, "0", empty stringShown; the value is written as a hide attribute (for example hide="false")
A key that does not exist in dataThe attribute value itself (a non-empty string) is used, so the element is hidden

The string "false" is truthy and also hides the element; use booleans.
Once hidden, setting the value back to a falsy one does not remove display: none, so the element stays hidden. Press Toggle twice in the preview above to reproduce it.

Localized Attribute Values


Any attribute whose value is i18n.key is replaced with the translation for the current locale, for example placeholder="i18n.search". Attributes only support single-level keys, see i18n.
中文