Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Readable file sizes with the Twig templating system

I'm wondering if there's a built in way to output readable file sizes with the Twig templating system. Say I have something like this in my template:

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

How could I format maxBytes to display something like 30 GB?

like image 492
James Dawson Avatar asked Mar 07 '13 19:03

James Dawson


2 Answers

There are a couple of ways you can go about accomplishing that:

1) get a Twig extension that will handle it for you. One like this one: https://github.com/BrazilianFriendsOfSymfony/BFOSTwigExtensionsBundle

Once enabled you would just do:

{{ maxBytes|bfos_format_bytes }}

And this will give you what you want.

2) You can create a macro that will do this if you dont want to add an entire extension. That would look something like this:

{% 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 %}

You can read more about where to put and how to use the macros here: http://twig.sensiolabs.org/doc/tags/macro.html

like image 87
Chase Avatar answered Nov 01 '22 18:11

Chase


Or, just create the twig extension:

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 }  

Twig template:

{{ variable | format_bytes }}
like image 17
devilcius Avatar answered Nov 01 '22 17:11

devilcius