Merge pull request #479 from fauxpark/filesize-patch

Add format_size Twig filter
This commit is contained in:
Klaus Silveira
2014-07-07 11:34:15 -03:00
3 changed files with 11 additions and 2 deletions

View File

@@ -65,6 +65,7 @@ class Application extends SilexApplication
$twig->addFilter(new \Twig_SimpleFilter('htmlentities', 'htmlentities')); $twig->addFilter(new \Twig_SimpleFilter('htmlentities', 'htmlentities'));
$twig->addFilter(new \Twig_SimpleFilter('md5', 'md5')); $twig->addFilter(new \Twig_SimpleFilter('md5', 'md5'));
$twig->addFilter(new \Twig_SimpleFilter('format_date', array($app, 'formatDate'))); $twig->addFilter(new \Twig_SimpleFilter('format_date', array($app, 'formatDate')));
$twig->addFilter(new \Twig_SimpleFilter('format_size', array($app, 'formatSize')));
return $twig; return $twig;
})); }));
@@ -97,6 +98,14 @@ class Application extends SilexApplication
return $date->format($this['date.format']); return $date->format($this['date.format']);
} }
public function formatSize($size)
{
$mod = 1000;
$units = array('B', 'kB', 'MB', 'GB');
for($i = 0; $size > $mod; $i++) $size /= $mod;
return round($size, 2) . $units[$i];
}
public function getPath() public function getPath()
{ {
return $this->path . DIRECTORY_SEPARATOR; return $this->path . DIRECTORY_SEPARATOR;

View File

@@ -37,7 +37,7 @@
</p> </p>
<p> <p>
<strong>Total bytes:</strong> {{ stats.size }} bytes ({{ ((stats.size / 1024) / 1024) | number_format }} MB) <strong>Total bytes:</strong> {{ stats.size }} bytes ({{ stats.size | format_size }})
</p> </p>
</td> </td>
</tr> </tr>

View File

@@ -50,7 +50,7 @@
{%- endif -%} {%- endif -%}
">{{ file.name }}</a></td> ">{{ file.name }}</a></td>
<td>{{ file.mode }}</td> <td>{{ file.mode }}</td>
<td>{% if file.size %}{{ (file.size / 1024) | number_format }} kb{% endif %}</td> <td>{% if file.size %}{{ file.size | format_size }}{% endif %}</td>
</tr> </tr>
{% endfor %} {% endfor %}
</tbody> </tbody>