我想知道是否有使用Twig模板系统输出可读文件大小的内置方法。说我的模板中有这样的东西:
<p>This limit is currently set to {{ maxBytes }}</p>
如何格式化maxBytes
以显示30 GB
?
答案 0 :(得分:22)
有几种方法可以实现这一目标:
1)获得一个Twig扩展,为您处理它。一个像这样的人:https://github.com/BrazilianFriendsOfSymfony/BFOSTwigExtensionsBundle
启用后,您就可以:
{{ maxBytes|bfos_format_bytes }}
这会给你你想要的东西。
2)如果您不想添加整个扩展程序,可以创建一个宏来执行此操作。这看起来像这样:
{% macro bytesToSize(bytes) %}
{% spaceless %}
{% set kilobyte = 1024 %}
{% set megabyte = kilobyte * 1024 %}
{% set gigabyte = megabyte * 1024 %}
{% set terabyte = gigabyte * 1024 %}
{% if bytes < kilobyte %}
{{ bytes ~ ' B' }}
{% elseif bytes < megabyte %}
{{ (bytes / kilobyte)|number_format(2, '.') ~ ' KB' }}
{% elseif bytes < gigabyte %}
{{ (bytes / megabyte)|number_format(2, '.') ~ ' MB' }}
{% elseif bytes < terabyte %}
{{ (bytes / gigabyte)|number_format(2, '.') ~ ' GB' }}
{% else %}
{{ (bytes / terabyte)|number_format(2, '.') ~ ' TB' }}
{% endif %}
{% endspaceless %}
{% endmacro %}
您可以在此处详细了解放置位置以及如何使用宏:http://twig.sensiolabs.org/doc/tags/macro.html
答案 1 :(得分:14)
或者,只需创建树枝扩展名:
ByteConversionTwigExtension.php
<?php
// src/AppBundle/Twig/Extension
namespace AppBundle\Twig\Extension;
class ByteConversionTwigExtension extends \Twig_Extension
{
/**
* Gets filters
*
* @return array
*/
public function getFilters()
{
return array(
new \Twig_SimpleFilter('format_bytes', array($this, 'formatBytes')),
);
}
public function getName()
{
return 'format_bytes';
}
function formatBytes($bytes, $precision = 2)
{
$units = array('B', 'KB', 'MB', 'GB', 'TB');
$bytes = max($bytes, 0);
$pow = floor(($bytes ? log($bytes) : 0) / log(1024));
$pow = min($pow, count($units) - 1);
// Uncomment one of the following alternatives
$bytes /= pow(1024, $pow);
return round($bytes, $precision) . ' ' . $units[$pow];
}
}
services.yml
parameters:
app.byte_conversion_twig_extension.twig.extension.class: AppBundle\Twig\Extension\ByteConversionTwigExtension
services:
app.byte_conversion.twig.extension:
class: %app.byte_conversion_twig_extension.twig.extension.class%
tags:
- { name: twig.extension }
Twig模板:
{{ variable | format_bytes }}
答案 2 :(得分:0)
使用symfony 4编码格式和Jeffrey Sambells formatting function的人类可读代码的树枝扩展:
src / Twig / AppExtension.php
<?php
namespace App\Twig;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
class AppExtension extends AbstractExtension
{
/**
* @var ContainerInterface
*/
protected $container;
/**
* Constructor
*
* @param ContainerInterface $container
*/
public function __construct(
ContainerInterface $container
)
{
$this->container = $container;
}
public function getFilters()
{
return array(
new TwigFilter('formatBytes', array($this, 'formatBytes')),
);
}
/**
* @param $bytes
* @param int $precision
* @return string
*/
public function formatBytes($bytes, $precision = 2)
{
$size = ['B','kB','MB','GB','TB','PB','EB','ZB','YB'];
$factor = floor((strlen($bytes) - 1) / 3);
return sprintf("%.{$precision}f", $bytes / pow(1024, $factor)) . @$size[$factor];
}
}
services.yaml :
App\Twig\AppExtension:
arguments:
- '@service_container'
tags:
- { name: twig.extension}
在模板中的用法:
{{ bytes| formatBytes }}
{{ bytes| formatBytes(0) }}