Lazlo
Lazlo

Reputation: 8790

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}

Upvotes: 7

Views: 928

Answers (4)

Vanav
Vanav

Reputation: 382

This works for Smarty 2 and Smarty 3:

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

Upvotes: 0

Gedrox
Gedrox

Reputation: 3612

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}

Upvotes: 8

Sascha Galley
Sascha Galley

Reputation: 16091

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}

Upvotes: 0

JochenJung
JochenJung

Reputation: 7213

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}

Upvotes: 1

Related Questions