使用Twig模板系统可读取文件大小

问题描述 投票:6回答:3

我想知道是否有一种内置的方式来输出可读文件大小与Twig模板系统。说我的模板中有这样的东西:

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

我怎么能格式化maxBytes来显示像30 GB这样的东西?

twig filesize readability
3个回答
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


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 }  

树枝模板:

{{ variable | format_bytes }}

0
投票

使用symfony 4编码格式和Jeffrey Sambells formatting function的人类可读代码的枝条扩展:

SRC /枝条/ 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) }}
© www.soinside.com 2019 - 2024. All rights reserved.