跳到主要内容

ARC 2.0.0-beta.25

组件、循环与 i18n

用 compile-time component 与 loop 组织 source;用 runtime list 和 locale contract 处理数据与语言。

这三项能力解决的不是同一个问题。不要在产品需要 live data list 时使用 compile-time loop,也不要在需要 authorization 时使用 visibility condition。

component 是 compile-time composition

component name(params...) {} 定义 page-level macro。use name(...) 支持位置参数、命名参数和默认值。component 有显式 ID 或内部 event target,且需要多次使用时,use ... as namespace 会为展开项加前缀,使每次使用保持不同。

静态 for 不是 data query

aup
page index {
  component card(page, label="Open") {
    button "card-$page" $label primary -> page $page
  }

  view {
    for page in [home, settings] {
      use card($page)
    }
  }
}

for x in [...] {} 在 compile time 展开静态 array,并提供 $index$first$last。它不是 source-driven 或 live data list。若是 runtime list,应使用 afs-list/repeat,并明确其 data path 与 target 支持。

include/partial 需要 runtime include resolver;import 只导入 component definition,不会创建 page node。include/component 展开有默认 depth boundary,遇到 cycle 会报告,而不会无限递归。

本地化 source,不靠猜测

app 声明 locales [en, zh] 后,page 可以包含一个 i18n {} block:

aup
i18n {
  title { en "Welcome" zh "欢迎" }
}

h1 :title

shorthand 会编译成 translation key。key 也可以引用已有 source material。缺少 inline translation 时渲染可以 fallback,严格 locale check 可以报告覆盖缺口;未知 i18n key 会失败。它不会自动翻译文案,也不替代 Web Device 的 localized content-object system。

data-driven list 语义见Event、state 与 binding;站点内容对象及其 locale 文件属于目标的 Web Device 层,不属于这篇 DSL 页面。