7

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}

Lazlo
  • 8,518
  • 14
  • 77
  • 116

4 Answers4

8

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}
Gedrox
  • 3,592
  • 1
  • 21
  • 29
1

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}
JochenJung
  • 7,183
  • 12
  • 64
  • 113
0

This works for Smarty 2 and Smarty 3:

{t}Really long text I want to be translated then truncated{/t|truncate:10}

Vanav
  • 374
  • 3
  • 7
0

The way you want it to do does not work, this will throw a Smarty Exception with a Syntax Error. But you can combine multiple block functions like this:

$smarty->registerPlugin('block', 'tr', 'do_translation', true);
$smarty->registerPlugin('block', 'truncate', 'do_truncation', true);

in your template file, combine it this way:

{truncate}{tr}Really long text I want to be translated then truncated{/tr}{/truncate}
Sascha Galley
  • 15,711
  • 5
  • 37
  • 51