1

Since we can get the file extension in php by using the explode function or in_array check. Is there a way in Smarty to get the file extension of a string. But how can we get the file extension in tpl page using smarty?currently i am passing all the extension as an array and using the {foreach} to list in tpl file.

ArunJaganathan
  • 695
  • 2
  • 7
  • 20

2 Answers2

3

Try this

<?php

    $smarty->assign('filename', 'foo\bar.txt');

?>

{* in your tpl *}

{$filename|pathinfo:$smarty.const.PATHINFO_EXTENSION}

{* you get 'txt' *}

Panama Jack
  • 24,158
  • 10
  • 63
  • 95
1

I use a plugin I created just for that:

<?php
function smarty_modifier_file_extension($file)
{
    $bits = explode('.',$file);
    $ext = $bits?array_pop($bits):''; 
    return $ext;
}

Save it as modifier.file_extension.php in your smarty's plugins folder (or a custom plugins folder if you have any) and use it like this:

 {$filename|file_extension}
Borgtex
  • 3,235
  • 1
  • 19
  • 28