cristianoc72 / icon-button
用于Symfony表单库的简单扩展,用于为按钮应用图标
Requires
- php: >=5.5
- symfony/form: ^3.0
- symfony/translation: ^3.0
- symfony/twig-bridge: ^3.0
Requires (Dev)
- phpunit/phpunit: ^4.7
- symfony/browser-kit: ^3.0
- symfony/css-selector: ^3.0
This package is auto-updated.
Last update: 2024-09-18 06:43:37 UTC
README
Icon-button是Symfony表单按钮和提交类型的扩展。您可以在标签前后轻松添加图标。
目前,只支持Bootstrap Glyphicons。
安装
通过composer安装此包
composer require cristianoc72/icon-button
与Silex一起工作
确保您已启用TwigServiceProvider
和FormServiceProvider
及其所有依赖项。请参阅http://silex.sensiolabs.org/doc/providers/form.html。
然后在您的应用程序中注册扩展
<?php //Tell Twig where icon-button theme is $app->register(new \Silex\Provider\TwigServiceProvider(), [ 'twig.path' => [__DIR__ . '/your/application/templates', __DIR__ . '/../vendor/cristianoc72/icon-button/resources/template'], 'twig.form.templates' => ['your_application_form_layout.html.twig', 'icon_button.html.twig'] ]); //then register the extension $app->extend('form.type.extensions', function ($extensions) use ($app) { $extensions[] = new cristianoc72\IconButton\IconButtonTypeExtension(); return $extensions; }) );
并告诉Twig关于icon-button模板的信息;
与Symfony一起工作
将扩展作为服务注册
# app/config/config.yml services: ..... app.icon_button_type_extension: class: cristianoc72\IconButton\IconButtonTypeExtension tags: - { name: form.type_extension, extended_type: Symfony\Component\Form\Extension\Core\Type\ButtonType } twig: ... paths: "%kernel.root_dir%/../vendor/cristianoc72/icon-button/resource/template" form_themes: - ........ - icon_button.html.twig
用法
Icon-button扩展为Symfony按钮类型添加了两个新属性:icon
和icon_position
。icon
属性是包含图标glyphicon CSS选择器的字符串。
您可以通过设置icon-position
属性来在按钮标签前后显示图标。icon_position
接受以下值之一:after
、before
、0、1(0表示“before”,1表示“after”)
<?php $form->add('save', 'submit', ['icon' => 'glyphicon-floppy-save', 'icon_position' => 'after']);
结果如下
如果您更喜欢图标在标签之前
<?php $form->add('save', 'submit', ['icon' => 'glyphicon-floppy-save', 'icon_position' => 'before']);
结果如下
当您在一个多步骤表单向导中工作时,您总是定义一个上一个步骤按钮和一个下一个步骤按钮。当您将名为previous_step
或next_step
的按钮添加到您的表单中时,此扩展会自动添加以下图标
- ** previous_step **:
icon
设置为glyphicon-step-backward
,icon_position
设置为before
- ** next_step **:
icon
设置为glyphicon-step-forward
,icon_position
设置为after
因此,如果您对默认图标感到满意,您可以直接编写
<?php $form->add('previous_step', 'submit'); $form->add('next_step', 'submit');
生成的按钮如下
如果您还需要一个reset
按钮,它位于上一个按钮和下一个按钮之间
<?php $form->add('previous_step', 'submit'); $form->add('next_step', 'submit'); $form->add('reset', 'reset');
如下所示
测试
此库使用PhpUnit进行测试。要运行测试套件,从您的项目根目录中,执行
vendor/bin/phpunit
贡献
每个贡献都受欢迎!一个拼写错误(尤其是关于我糟糕的英语),一个错误修复,一个添加项,一个建议,所有这些都是重要的。
如果您想做出贡献,只需fork此存储库并提交一个pull请求。
当然,在提交pull请求之前,有一些小规则需要遵守
- 此项目遵循PSR-1和PSR-2编码标准。如果您添加或修改代码,我们建议运行
php-cs-fixer.phar
。请参阅http://cs.sensiolabs.org。 - 当您修改现有代码时,运行测试套件并享受一切绿色。
- 当您添加新功能时,编写一个测试来证明您的代码正常工作。
许可证
此库在MIT许可证下发布。有关详细信息,请参阅LICENSE文件。