Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply Smarty modifier on block output

I'm trying to apply a modifier (truncate, in my case) to the output of a block (a tr block, that is, a translation block). I don't have tr as a modifier because it's not convenient for HTML markup.

I don't really know what kind of syntax I should use, nor if it's allowed (given, my usage of blocks might be a bit funky).

Something like that, if it makes any sense:

{{tr}Really long text I want to be translated then truncated{/tr}|truncate}

like image 937
Lazlo Avatar asked Jun 29 '11 17:06

Lazlo


2 Answers

It could be done like this:

{capture assign="var"}{tr}...{/tr}{/capture}
{$var|truncate}

But I personally would create truncate block function and do this

{truncate}{tr}...{/tr}{/truncate}
like image 156
Gedrox Avatar answered Oct 16 '22 18:10

Gedrox


Afaik you cant combine them the way you like. The only idea I have, is to write your own truncate Function together with your translate function:

function do_translation($params, $content, $smarty, &$repeat) {
  if (isset($content)) {
    $options = $params["options"];
    $content = yourTranslateFunction($content);
    if ($options['truncate']) $content = yourTruncateFunction($content);
    return $content;
  }
}
$smarty->registerPlugin("block", "tr", "do_translation");

Then you could invoke it in Smarty like this:

{tr truncate="1"}Really long text I want to be translated then truncated{/tr}
like image 39
JochenJung Avatar answered Oct 16 '22 20:10

JochenJung