Apply Smarty modifier at block output

I am trying to apply a modifier ( truncate , in my case) to the output of a block (a tr block, i.e. a translation block). I do not have tr as a modifier because it is not convenient for HTML markup.

I really don't know what syntax I should use, and if it is allowed (given my use of blocks can be a bit funky).

Something like this, if that makes sense:

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

+6
source share
4 answers

This can be done as follows:

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

But I will personally create a truncate block function and do it

 {truncate}{tr}...{/tr}{/truncate} 
+7
source

Afayk, you cannot combine them the way you like. The only idea I have is to write my own truncate function along with your translation 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 can call it in Smarty as follows:

 {tr truncate="1"}Really long text I want to be translated then truncated{/tr} 
+1
source

The way you want does not work, it will throw a Smarty exception with a syntax error. But you can combine several block functions as follows:

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

in the template file, merge it as follows:

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

This works for Smarty 2 and Smarty 3:

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

0
source

Source: https://habr.com/ru/post/891667/


All Articles