e0ipso/twig-storybook
Composer 安装命令:
composer require e0ipso/twig-storybook
包简介
Extends Twig with new "stories" and "story" tags for easily writing Storybook stories using Twig templates.
README 文档
README
Twig Storybook is a Composer package that enhances the Twig templating language by introducing two new Twig tags: stories and story. With Twig Storybook, you can easily create and manage Storybook stories directly in your Twig templates, making it a powerful tool for documenting and showcasing your frontend components.
Table of Contents
Installation
You can install Twig Storybook via Composer:
composer require e0ipso/twig-storybook
Usage
With Drupal
You don't need to use this package directly, use the Storybook drupal module instead. This library is compatible with Drupal 10, and later.
Adding Twig Tags
After installing the package, you need to register the Twig tags in your Twig environment. Here's how you can do it:
use Twig\Environment; use Twig\Loader\FilesystemLoader; use TwigStorybook\Twig\StorybookExtension; // Initialize the Twig environment $loader = new FilesystemLoader('path/to/your/templates'); $twig = new Environment($loader); // Register the Storybook extension $twig->addExtension(new \TwigStorybook\Twig\TwigExtension());
Creating Stories
Once the Twig Storybook extension is registered, you can start creating stories within your Twig templates. We recommend
writing the stories in a file with name <file-name>.stories.twig.
- Use the
{% stories %}tag to define a group of stories. - Use the
{% story %}tag to define an individual story.
Here's an example:
{% stories my_card with {
title: 'Components/Examples/Card',
argTypes: {
iconType: {
options: { Power: 'power', Like: 'like', External: 'external' },
control: 'radio'
}
}
} %}
{% story default with {
name: '1. Default',
args: { header: 'I am a header!', text: 'Learn more', iconType: 'power' }
} %}
{% embed '@examples/my-card' with { header } %}
{% block card_body %}
<p>I am the <em>card</em> contents.</p>
{% include '@examples/my-button' with { text, iconType } %}
{% endblock %}
{% endembed %}
{% endstory %}
{% endstories %}
This will render as:
Storybook setup
Install Storybook as usual:
# Make use of modern versions of yarn. yarn set version berry # Avoid pnp. echo 'nodeLinker: node-modules' >> .yarnrc.yml # Install and configure stock Storybook. yarn dlx sb init --builder webpack5 --type server
Then update .storybook/main.js to scan for stories where your application stores them.
Compiling Twig stories into JSON
The Storybook application will does not understand stories in Twig format. It will fail to render them. You need to
compile them into a *.stories.json. To do so, you can leverage the
StoryRenderer::generateStoriesJsonFile($template_path, $url) method. Where the $template_path is the location of the
*.stories.twig template, and the $url is the base URL in your application that will render the story.
Storybook will make a request to "$url . '/' . $hash", so make sure your application handles that route (see below).
The Drupal module implementation includes a console command to find all
*.stories.twig and compile them into *.stories.json.
See the implementation.
Rendering Stories
To render the Storybook stories, you can use the StoryRenderer service. Every framework handles routes differently,
but we recommend using the story renderer inside your controller.
public function renderStoryControllerHandler(string $hash, Request $request): array { $data = $this->storyRenderer->renderStory($hash, $request); return new Response($data); }
To use this service, wire the services to the container. The Twig Storybook Drupal module does it like this:
services: logger.channel.twig_storybook: parent: logger.channel_base arguments: ['twig_storybook'] TwigStorybook\Service\StoryCollector: {} TwigStorybook\Service\StoryRenderer: arguments: - '@TwigStorybook\Service\StoryCollector' - '@logger.channel.twig_storybook' - '%app.root%' calls: - ['setTwigEnvironment', ['@twig']] TwigStorybook\Twig\TwigExtension: arguments: - '@TwigStorybook\Service\StoryCollector' - '%app.root%' tags: - { name: twig.extension }
Contributing
We welcome contributions from the community. If you want to contribute to Twig Storybook, please follow these guidelines:
- Fork the repository.
- Create a new branch for your feature or bug fix.
- Make your changes and ensure all tests pass.
- Submit a pull request with a clear description of your changes.
Make sure to follow the code of conduct when interacting with this community.
License
Twig Storybook is open-source software licensed under the GPL-2 License.
e0ipso/twig-storybook 适用场景与选型建议
e0ipso/twig-storybook 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 629.54k 次下载、GitHub Stars 达 9, 最近一次更新时间为 2024 年 01 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 e0ipso/twig-storybook 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 e0ipso/twig-storybook 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 629.54k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 10
- 点击次数: 24
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: GPL-2.0-or-later
- 更新时间: 2024-01-02
