ed.sukharev / twig-picture-extension
一个生成图片标签的 Twig 扩展
1.0.0
2019-05-10 08:59 UTC
Requires
- php: >=5.3.3
- twig/twig: >=1.0,<=1.36.0
Requires (Dev)
- doctrine/instantiator: ~1.0.5
- phpdocumentor/reflection-docblock: ~2.0.5
- phpunit/phpunit: ~4.8
Suggests
- ext-fileinfo: Allows for more precise MIME type generation
This package is auto-updated.
Last update: 2024-09-10 21:36:53 UTC
README
一个自定义的 twig 扩展,用于添加
安装
将库添加到你的应用的 composer.json
"require": { "ed.sukharev/twig-picture-extension": "~1.0", ... }
将扩展添加到 Twig_Environment
use PictureExtension\Twig\Extension\Picture; $twig = new Twig_Environment(...); $twig->addExtension(new Picture());
用法
该扩展提供了一个 picture twig 函数,该函数生成一组 source 的 picture 标签以及回退的 img 标签
{{ picture('/img/path/filename.png', 'My image alt text') }}
这将生成以下输出
<picture> <source srcset="/img/path/filename.webp" type="image/webp"> <source srcset="/img/path/filename.png" type="image/png"> <img src="/img/path/filename.png" alt="My image alt text"> </picture>
参数
picture 函数接受 3 个参数
picture($filename, $imgAlt, $imgClasses = [])
- filename:原始图像的文件名。当需要时,图片函数会替换扩展名。
- alt:当图像不可用时显示的替代文本。必需的,以免忘记添加。
- imgClasses:要添加到回退
img标签的class属性的字符串数组。
Symfony2
手动添加服务
# app/config/config.yml services: pciture_extension.twig.picture: class: PictureExtension\Twig\Extension\Picture tags: - { name: twig.extension }