8

Twig テンプレート システムで読み取り可能なファイル サイズを出力する組み込みの方法があるかどうか疑問に思っています。テンプレートに次のようなものがあるとします。

<p>This limit is currently set to {{ maxBytes }}</p>

maxBytesのようなものを表示するようにフォーマットするにはどうすればよい30 GBですか?

4

3 に答える 3

26

それを達成するには、いくつかの方法があります。

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, '.') ~ ' KiB' }}
    {% elseif bytes < gigabyte %}
        {{ (bytes / megabyte)|number_format(2, '.') ~ ' MiB' }}
    {% elseif bytes < terabyte %}
        {{ (bytes / gigabyte)|number_format(2, '.') ~ ' GiB' }}
    {% else %}
        {{ (bytes / terabyte)|number_format(2, '.') ~ ' TiB' }}
    {% endif %}
{% endspaceless %}
{% endmacro %}

マクロの配置場所と使用方法について詳しくは、http: //twig.sensiolabs.org/doc/tags/macro.htmlを参照してください。

于 2013-03-08T20:46:27.357 に答える
16

または、小枝拡張機能を作成するだけです:

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', 'KiB', 'MiB', 'GiB', 'TiB');
        $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 }  

小枝テンプレート:

{{ variable | format_bytes }}
于 2016-05-30T10:50:58.600 に答える
1

symfony 4 コーディング形式とJeffrey Sambells フォーマット関数を使用した人間が読めるコードの小枝拡張:

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) }}
于 2019-04-30T08:38:06.003 に答える