巴西Symfony爱好者 / twig-extensions-bundle
Symfony BFOSTwigExtensionsBundle
dev-master
2016-09-19 11:14 UTC
Requires
- php: >=5.3.3
- twig/twig: ~1.12
This package is not auto-updated.
Last update: 2024-09-20 20:56:56 UTC
README
此捆绑包提供了以下 Twig过滤器
bfos_format_bytes
: 将数字按字节格式化为可读格式。bfos_align_right
: 格式化字符串:用空格右对齐。在预html标签中使用很有用。bfos_align_left
: 格式化字符串:用空格左对齐。在预html标签中使用很有用。
并提供以下 表单类型
bfos_richtextarea
: bfos_richtextarea类型,允许您直接使用富文本表单字段。目前使用CkEditor。
安装
您需要在deps文件中安装子模块:
// deps
[BFOSTwigExtensionsBundle]
git=git://github.com/BrazilianFriendsOfSymfony/BFOSTwigExtensionsBundle.git
target=/bundles/BFOS/TwigExtensionsBundle
然后:
bash$ php bin/vendors install
配置
将以下内容添加到app/autoload.php中:
// app/autoload.php
$loader->registerNamespaces(array(
// ...
'BFOS' => __DIR__.'/../vendor/bundles',
// ...
));
并将以下内容添加到app/AppKernel.php中:
// app/AppKernel.php
$bundles = array(
// ...
new BFOS\TwigExtensionsBundle\BFOSTwigExtensionsBundle(),
// ...
);
将以下内容添加到您的config.yml中
twig:
form:
resources:
- 'BFOSTwigExtensionsBundle:Form:form_div_layout.html.twig'
使用方法
如何使用:
- FCBKComplete实体小部件
设置您的类型
public function buildForm(FormBuilder $builder, array $options)
{
$url = $this->container->get('router')->generate('users_autocomplete');
$builder->add('users', 'bfos_fcbkcomplete_entity',
array('class'=>'FOS\UserBundle\Entity\User', 'url'=>$url, 'fcbkcomplete_options'=>array('maxitems'=>40, 'maxshownitems'=>40)));
}
设置自动完成列表的动作
/**
* Auto completer list action.
*
* @Route("/users/autocomplete", name="users_autocomplete")
* @Method("get")
*/
public function usersAutoCompleteAction(){
if(!($q = $this->getRequest()->get('tag'))){
return new \Symfony\Component\HttpFoundation\Response('');
}
$arr = array();
$users = $this->getDoctrine()->getRepository('FOSUserBundle:User')->findForAutoComplete($q);
foreach($users as $user){
$arr[] = array('key'=> (string) $user->getId(), 'value'=> sprintf('%s (%s)', $user->getUsername(), $user->getEmail())) ;
}
return new \Symfony\Component\HttpFoundation\Response(json_encode($arr), 200, array('Content-Type'=> 'application/json'));
}
设置存储库方法
public function findForAutoComplete($string, $limit = 20){
if(!$string){
return array();
}
$qb = $this->createQueryBuilder('u');
$query = $qb->where('u.username LIKE :str')->orWhere('u.email LIKE :str')->setParameter('str', "%$string%")->getQuery();
return $query->setMaxResults($limit)->getResult();
}
- 日期选择器小部件
设置您的类型
public function buildForm(FormBuilder $builder, array $options)
{
$builder->add('date', 'bfos_date');
}
警告:该小部件不支持将小部件选项设置为single_text。
- 日期时间选择器小部件
设置您的类型
public function buildForm(FormBuilder $builder, array $options)
{
$builder->add('date', 'bfos_datetime');
}
警告:该小部件不支持将小部件选项设置为single_text。
-
过滤器
bfos_format_bytes
:{{ [整数] | bfos_format_bytes }}
您还可以指定要使用的基数(SI或二进制):
{# Default. SI #}
{{ [integer] | bfos_format_bytes( true ) }}
{# Use the binary #}
{{ [integer] | bfos_format_bytes( false ) }}
-
过滤器
bfos_align_right
和bfos_align_left
{# 默认。使用10列对齐。 #} {{ [字符串] | bfos_align_right }} {# 使用20列空间对齐。 #} {{ [字符串] | bfos_align_right(20) }}
{# 默认。使用10列对齐。 #} {{ [字符串] | bfos_align_left }} {# 使用20列空间对齐。 #} {{ [字符串] | bfos_align_left(20) }}
示例
以下代码
<pre>
{% set sizes = [0, 27, 999, 1000, 1023, 1024, 1728, 110592, 7077888, 452984832, 28991029248, 1855425871872, 9223372036854775807] %}
{{ "Size in bytes"|bfos_align_left(20) }} {{ "SI"|bfos_align_right(15)}} {{ "Binary"|bfos_align_right(15)}}
{% for i in sizes %}
{{ i|bfos_align_left(20) }} = {{ i|bfos_format_bytes|bfos_align_right(15) }} {{ i|bfos_format_bytes(false)|bfos_align_right(15) }}
{% endfor %}
</pre>
将生成以下结果
Size in bytes SI Binary
0 = 0 B 0 B
27 = 27 B 27 B
999 = 999 B 999 B
1000 = 1000 B 1000 B
1023 = 1.0 kB 1023 B
1024 = 1.0 kB 1024 B
1728 = 1.7 kB 1.7 KiB
110592 = 110.6 kB 108.0 KiB
7077888 = 7.1 MB 6.8 MiB
452984832 = 453.0 MB 432.0 MiB
28991029248 = 29.0 GB 27.0 GiB
1855425871872 = 1.9 TB 1.7 TiB
9.2233720368548E+18 = 9.2 EB 8.0 EiB