component

用于渲染指定的代码片段,支持以下两种类型:

  • 全局代码片段:存放在 component 目录下,可跨模块调用。
  • 私有代码片段:存放在 sectionsblocks 目录下,仅限当前模块使用。

如需使用作用域之外的变量,必须通过 hash 参数传入,Sline 全局 object 无需传 hash 参数。

{{#component file_path [hash...] /}}

必选参数

  • file_path string: 代码片段文件路径。对于全局代码片段,使用绝对路径,即 components/ 下的文件名。对于私有代码片段,使用相对路径,以 ./ 开头。

可选参数

  • hash... any: 动态哈希参数,可以传递多个键值对(格式为 key=value)的参数集合,数量不限。这些参数是用于向代码片段提供所需的变量信息。在代码片段内部,这些参数可以通过 props 对象进行访问和使用。

例子

使用全局代码片段

调用 components/title.html 全局组件:

{{#component "title" /}}

使用私有代码片段

sections/header/header.html 中调用同目录的sections/header/title.html

{{#component "./title" /}}

向组件传递参数

sections/header/header.html 中的变量 title 传入到同目录的sections/header/title.html 中使用。

<!--! sections/header/header.html -->
{{#component "./title" title=section.settings.title show=true /}}
<!--! sections/header/title.html -->
{{#if show}}
{{ title }}
{{/if}}
这篇文章对你有帮助吗?