redant/twig-components-bundle

Symfony 的 Twig 组件包

1.2.1 2024-09-10 12:22 UTC

This package is auto-updated.

Last update: 2024-09-10 12:23:25 UTC


README

这是一个为 Twig Components 库提供的 Symfony 包。

此包允许您使用新的 component 标签创建强大、可重用且自动生成文档的 Twig 组件。Twig 组件允许您快速构建和维护自己的 UI 工具包,其中每个按钮、表格或卡片只需设计一次,即可在整个应用程序中使用。

此 Symfony 的 Twig 组件包在每次编译 Symfony 容器时会自动搜索模板目录中的定义组件,使生活变得更加简单。

有关 Twig 组件的更多信息,请参阅 文档

设置和用法

您可以通过 Composer 安装此包

$ composer require redant/twig-components-bundle

当此包在您的 Symfony 包配置中启用时,它将搜索所有模板目录中的定义组件,每次容器编译时。

这包括

  • templates/components 文件夹(主要应用程序模板)
  • 每个已安装包的 Resources/views/components 文件夹(如果已定义)

全局变量

如果您不喜欢定义组件的全局变量名称,可以使用 twig_components.global_variable 参数来更改此设置

# app/config/twig.yaml

twig_components:
    global_variable: 'ui'

这将注册按钮组件为 ui.button()

注意:如果您将前缀设置为 false,则不会为定义的组件注册任何 Twig 全局。您只能使用 render_component 函数。

命名空间

如果您在 components/ 目录的子目录中定义组件,则额外的目录将命名空间您的组件。

例如,定义在 components/ui/button.html.twig 的组件将可通过 component.ui.button({}) 或通过 render_component('ui.button', {}) 访问。

生成文档

创建组件定义的额外好处是自动创建文档。例如,它解释了组件可以用于什么以及它接受哪些参数。例如,Twig 组件包可以自动为示例按钮组件生成如下表格

您可以使用提供的 twig:components:generate-docs 命令生成带有文档的静态 HTML 文件。

Description:
  Generate documentation for Twig components

Usage:
  twig:components:generate-docs [options] <path>

Arguments:
  path                  Output directory

Options:
  --title=TITLE         Title for the generated documentation [default: "Twig components"]
  --generic             Disregard twig_component.global_variable settings and only show render_component() examples```

技巧:当您使用注释({# ... #})开始组件模板文件时,其内容将被添加到组件文档的顶部。

许可证

此库使用 MIT 许可证授权 - 请参阅 LICENSE 文件以获取详细信息。