商业与职场

Symfony Twig组件

用PHP类或纯Twig实现按钮、卡片与提示框等服务端渲染组件,按逻辑需求选择组件形式。

# TwigComponent

使用 PHP 类 + Twig 模板构建可复用的 UI 组件。可以将其理解为 React/Vue 组件,但它在服务器端渲染,无需任何 JavaScript。

有两种形式:**类组件**(PHP 类 + Twig 模板),适用于需要逻辑、服务或计算属性的组件;**匿名组件**(仅使用 Twig,无 PHP 类),适用于简单的展示元素。

## 何时使用 TwigComponent

当你需要带 props 的可复用标记,且初次渲染后不需要服务器重新渲染时,使用 TwigComponent。如果组件需要响应用户输入(通过 AJAX 重新渲染、数据绑定、动作),则使用 LiveComponent。

适合的场景:按钮、提示框、卡片、徽章、图标、表单控件、布局区块、导航项、表格行、模态框(仅结构)。

## 安装

```bash
composer require symfony/ux-twig-component
```

## 类组件

一个使用 `#[AsTwigComponent]` 注解的 PHP 类,搭配一个 Twig 模板。

```php
// src/Twig/Components/Alert.php
namespace App\Twig\Components;

use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;

#[AsTwigComponent]
final class Alert
{
    public string $type = 'info';
    public string $message;
    public bool $dismissible = false;
}
```

```twig
{# templates/components/Alert.html.twig #}

    {{ message }}
    {% if dismissible %}
        <button type="button" class="close">×</button>
    {% endif %}

```

```twig
{# Usage #}
<twig:Alert type="success" message="Saved!" />
<twig:Alert type="danger" message="Error occurred" dismissible />

{# With block content instead of message prop #}
<twig:Alert type="warning">
    Warning: Check your input
</twig:Alert>
```

## 匿名组件(仅 Twig)

无需 PHP 类。直接在模板中使用 `{% props %}` 声明 props。适用于没有逻辑的简单展示组件。

```twig
{# templates/components/Button.html.twig #}
{% props variant = 'primary', size = 'md', disabled = false %}

<button
    class="btn btn-{{ variant }} btn-{{ size }}"
    {{ disabled ? 'disabled' }}
    {{ attributes }}
>
    {% block content %}{% endblock %}
</button>
```

```twig
<twig:Button variant="danger" size="lg">Delete</twig:Button>
```

## Props

### 公有属性(类组件)

公有属性会成为 props。必需的 props 没有默认值。

```php
#[AsTwigComponent]
final class Card
{
    public string $title;           // Required
    public ?string $subtitle = null; // Optional
    public bool $shadow = true;      // Optional with default
}
```

### 使用 mount() 处理派生状态

使用 `mount()` 根据传入的 props 计算值。该方法在组件初始化期间运行一次。

```php
#[AsTwigComponent]
final class UserCard
{
    public User $user;
    public string $displayName;

    public function mount(User $user): void
    {
        $this->user = $user;
        $this->displayName = $user->getFullName();
    }
}
```

```twig
<twig:UserCard :user="currentUser" />
```

### 动态 Props(冒号前缀)

在 prop 前加上 `:`,即可传递 Twig 表达式,而非字符串字面量。

```twig
{# Pass a variable #}
<twig:Alert :type="alertType" :message="flashMessage" />

{# Pass an expression #}
<twig:UserList :users="users|filter(u => u.active)" />
```

## 块(插槽)

块允许父模板向组件的指定区域注入内容。

### 默认块

组件标签之间的内容会进入 `{% block content %}`:

```twig
{# Component template #}
{% block content %}{% endblock %}

{# Usage #}
<twig:Card>This is the card content</twig:Card>
```

### 命名块

```twig
{# templates/components/Modal.html.twig #}
<dialog class="modal" {{ attributes }}>
    <header>{% block header %}Default Header{% endblock %}</header>
    <main>{% block content %}{% endblock %}</main>
    <footer>{% block footer %}{% endblock %}</footer>
</dialog>
```

```twig
<twig:Modal>
    <twig:block name="header"><h2>Confirm Action</h2></twig:block>
    <twig:block name="content">Are you sure?</twig:block>
    <twig:block name="footer">
        <button>Cancel</button>
        <button>Confirm</button>
    </twig:block>
</twig:Modal>
```

## 计算属性

以 `get` 为前缀的方法可以在模板中通过 `this.xxx` 访问。每次访问时都会重新计算(不会在重新渲染之间缓存——有关缓存,请参阅 LiveComponent 的 `computed`)。

```php
#[AsTwigComponent]
final class ProductCard
{
    public Product $product;

    public function getFormattedPrice(): string
    {
        return number_format($this->product->getPrice(), 2) . ' EUR';
    }

    public function isOnSale(): bool
    {
        return $this->product->getDiscount() > 0;
    }
}
```

```twig

    {{ this.formattedPrice }}
    {% if this.onSale %}
        Sale!
    {% endif %}

```

## 属性

传递给组件的额外 HTML 属性可通过 `{{ attributes }}` 访问。通过这种方式,使用组件的人可以添加自定义类、id、数据属性等。

```twig
{# Usage #}
<twig:Alert type="info" message="Hello" class="my-class" id="main-alert" data-controller="alert" />

{# In component template -- renders class, id, data-controller #}
...
```

### 属性方法

```twig
{# Merge with defaults #}

{# Exclude specific #}

{# Only render specific #}

{# Check existence #}
{% if attributes.has('disabled') %}
```

## 将组件作为服务

组件是 Symfony 服务——自动装配自然适用。使用构造函数处理依赖,使用公有属性处理 props。

```php
#[AsTwigComponent]
final class FeaturedProducts
{
    public function __construct(
        private readonly ProductRepository $products,
    ) {}

    public function getProducts(): array
    {
        return $this->products->findFeatured(limit: 6);
    }
}
```

```twig
{# templates/components/FeaturedProducts.html.twig #}

    {% for product in this.products %}
        <twig:ProductCard :product="product" />
    {% endfor %}

```

```twig
{# Usage -- no props needed, data comes from service #}
<twig:FeaturedProducts />
```

## 生命周期钩子

```php
use Symfony\UX\TwigComponent\Attribute\PreMount;
use Symfony\UX\TwigComponent\Attribute\PostMount;

#[AsTwigComponent]
final class DataTable
{
    public array $data;
    public string $sortBy = 'id';

    #[PreMount]
    public function preMount(array $data): array
    {
        // Modify/validate incoming data before property assignment
        $data['sortBy'] ??= 'id';
        return $data;
    }

    #[PostMount]
    public function postMount(): void
    {
        // Runs after all props are set
        $this->data = $this->sortData($this->data);
    }
}
```

## 嵌套组件

组件可以自然组合——像 HTML 元素一样嵌套它们:

```twig
<twig:Card>
    <twig:block name="header">
        <twig:Icon name="star" /> Featured
    </twig:block>
    <twig:block name="content">
        <twig:ProductList :products="featuredProducts">
            <twig:block name="empty">
                <twig:Alert type="info" message="No products found" />
            </twig:block>
        </twig:ProductList>
    </twig:block>
</twig:Card>
```

## 配置

```yaml
# config/packages/twig_component.yaml
twig_component:
    anonymous_template_directory: 'components/'
    defaults:
        App\Twig\Components\: 'components/'
```

## HTML 与 Twig 语法

```twig
{# HTML syntax (recommended -- better IDE support, more readable) #}
<twig:Alert type="success" message="Done!" />

{# Twig syntax (alternative -- useful in edge cases) #}
{% component 'Alert' with {type: 'success', message: 'Done!'} %}
{% endcomponent %}
```

在所有情况下优先使用 HTML 语法(`<twig:...>`)。Twig 语法(`{% component %}`)是旧式语法,可读性较差。

## 参考资料

- **完整 API**(属性选项、钩子、配置、所有方法):[references/api.md](references/api.md)
- **模式**(表单、表格、布局、组合、实际示例):[references/patterns.md](references/patterns.md)
- **注意事项**(属性、命名、嵌套组件、常见陷阱):[references/gotchas.md](references/gotchas.md)

## 另请参阅

- **UX Icons** 可自然集成到 TwigComponent 模板中:在组件标记内使用 `<twig:ux:icon name="lucide:check" />`。
- **UX Map** 可通过 `{{ ux_map(map, {style: 'height: 400px;'}) }}` 在 TwigComponent 模板中渲染。

同类提示词

商业与职场

招聘面试轮次与面试官排期

提供岗位要求和面试官的可用时间,规划各轮评估内容、人员搭配及面试顺序,并起草题库和候选人通知。

# 面试安排 > 设计高效的面试流程,优化面试小组构成和日程安排,带来良好的候选人体验。 ## 何时使用此技能 - 设置多轮面试流程 - 协调面试小组日程 - 设计面试阶段 - 优化候选人体验 - 创建面试执行手册 ## 方法论基础 基于**结构化面试最佳实践**和**候选人体验研究**,结合: - 与阶段相适应的评估 - 面试小组多样性要求 - 招聘周期优化 - 候选人沟通 ## Claude的工作与由你决定的事项 | Claude的工作 | 由你决定 | |-------------|------------| | 设计面试阶段 | 最
可填 1 项
商业与职场

十五镜头产品营销视频分镜

填写营销文案与产品外观描述,按开场钩子、故事和促销安排分镜,逐条标明产品镜头或场景镜头。

你是一位精通营销心理学的商业视频分镜师。请根据营销文本和产品外观描述,创建具有强营销力的15个分镜。 营销文本:{text} 产品外观描述:{image} 【核心标记规则】⚠️ 极其重要 每个分镜必须在开头使用以下标记之一: - [产品镜头] - 画面中包含产品实体,需要保持产品外观一致性 - [场景镜头] - 不包含产品的纯环境、人物或情境画面 标记决定后端使用哪个生成模型,必须准确标注! 【营销策略分析】(内部分析,不输出) 分析营销文本,提取以下信息用于指导分镜创作: 1. 目标受众:识别目标人群特征,确定画面风格和场景选择 2. 核心卖点
可填 2 项中文原创
商业与职场

等距分层与爆炸图动画

为产品演示或信息图制作 2.5D 场景,用统一投影和分面明暗表现立体感,支持层叠展开及爆炸图动画。

# 等轴测(2.5D)动画 等轴测场景无需使用真正的透视,就能让讲解动画、UI 操作演示和信息图呈现出搭建而成的立体感:各轴等比例缩短、Z 轴方向堆叠的层级、拉伸立体块、爆炸图。整体风格几何感强、构图讲究——深度来自一致的轴向和各面的明暗处理,动效则通过逐层展示结构来实现。 ## 适用场景 - 用于讲解动画、产品 UI 或“工作原理”信息图的 2.5D 场景。 - 从下往上搭建的堆叠层级 / 楼层 / 技术栈塔。 - 拉伸立体块、等轴测网格、等轴测城市 / 服务器机架场景。 - 沿 Z 轴拆开再重新组装的爆炸图。 - 需要增添轻微生动感的静态等轴测
商业与职场

六页电影感演示文稿设计总览

选择浅色或深色方向,用图像模型把六页幻灯片合成一张电影感概览图,适合演示提案和版式评审。

# 演示文稿设计 创建一张**高端 6 页演示文稿设计展板**,将所有幻灯片合成为一张图片——让用户一眼看完整套演示文稿,适合电视屏幕展示,具有电影感。 主要用途:PPT 设计概念、主题演讲风格的幻灯片设计稿、战略演示文稿视觉方案、活动 / 新闻 / 体育 / 商业分析演示文稿、编辑式数据故事、高管演示文稿。 ## 图像生成要求 最终展板必须由**图像生成模型**生成(默认使用内置的 `image_gen` 工具,或经批准的项目图像生成工作流)。不要使用代码、SVG、HTML/CSS、canvas、图表、占位图或确定性形状脚本创建展板。这些方法只
商业与职场

十五款AI视频模型选型顾问

回答三至四个需求问题,按中文表现、音频、编辑能力、时长和可用性等条件获取模型推荐及理由。

# model-selector 15 个主流 AI 视频模型(11 商业 + 4 开源)的"购物顾问"。问用户 3-4 个关键问题,给出推荐 + 理由。 ## 何时不用此 skill - 用户已经确定要用某个模型 → 用对应的 `<model>-prompter` skill - 用户问"提示词怎么写" → 用 `seedance-prompter` / `kling-prompter` / `happyhorse-prompter` - 用户的提示词不工作 → 用 `seedance-debugger` ## 11 模型能力速查矩阵 | 模
中文原创
商业与职场

界面创意转视觉设计规格

填写界面用途、受众、尺寸和文案,将初步构想细化为包含网格、字体、配色与图像风格的设计规格。

# 设计优先的 UI 提示词技能 此技能适用于**设计优先的提示词编写**:将模糊的想法转化为严谨的规格说明,生成风格一致的 UI。 ## 核心原则 **像设计系统一样编写提示词,而不是许愿。** ## 提示词结构(复制/粘贴) 使用以下框架,然后填入空白。 ```text GOAL - What are we making? (e.g., landing page hero / onboarding / dashboard / carousel slide) - Who is it for? (persona) - What’s the suc
✓