Apply Smarty modifier on block output
Asked Answered
E

4

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}

Eyesore answered 29/6, 2011 at 17:50 Comment(0)
P
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}
Plastered answered 5/7, 2011 at 9:22 Comment(0)
J
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}
Jockstrap answered 5/7, 2011 at 6:52 Comment(0)
D
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}
Densimeter answered 5/7, 2011 at 9:14 Comment(0)
S
0

This works for Smarty 2 and Smarty 3:

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

Sarcophagus answered 30/8, 2015 at 1:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.