tmilos / symfony-control
1.0.1
2016-02-04 11:02 UTC
Requires
- symfony/config: ~2.3|~3.0
- symfony/dependency-injection: ~2.3|~3.0
- symfony/http-kernel: ~2.3|~3.0
- symfony/property-access: ~2.3|~3.0
- twig/twig: ~1.20|~2.0
Requires (Dev)
- phpunit/phpunit: ^4.8
- satooshi/php-coveralls: ~0.6
This package is not auto-updated.
Last update: 2024-09-14 18:42:10 UTC
README
Symfony bundle 实现了 Twig 控制标签。通过新的 control
twig 标签,可以轻松渲染块,并传递指定的参数。
安装
使用 composer 需要 tmilos/symfony-control
require tmilos/symfony-control
将包添加到您的 AppKernel
class AppKernel { public function registerBundles() { $bundles = array( // ... new Tmilos\ControlBundle\TmilosControlBundle(), // ... ); } }
控制 twig 标签
{% control BLOCK_NAME with EXPRESSION %}
它将显示指定的 BLOCK_NAME,并将指定的 EXPRESSION 作为 control
变量的值,仅在当前块作用域内有效。
property twig 函数
{{ property(object, 'path.to.property') }}
返回对象属性的值
围绕 Symfony\Component\PropertyAccess\PropertyAccessor::getValue()
的包装
has_property twig 函数
{{ has_property(object, 'path.to.property') }}
返回 bool
围绕 Symfony\Component\PropertyAccess\PropertyAccessor::isReadable()
的包装
使用示例
{# table.html.twig #} {% block table %} <table> <tr> {% for col in control.columns %} <th>{{ col|trans }}</th> {% endfor </tr> {% for row in control.rows %} <tr> {% for col in control.columns %} <td> {{ has_property(row, col) ? property(row, col) : block(col) }} </td> {% endfor %} </tr> {% endfor </table> {% endblock %}
{# index.html.twig #} {% extends 'base.html.twig' %} {% use 'table.html.twig' %} {% block body %} {% control table with { columns: ['name', 'user.email', 'special_column'], rows: entities } %} {% endblock %} {% block special_column %} <a href="{{ path('foo', {id: row.id}) }}">edit</a> {% endblock %}