9

I am having issue formatting variables in smarty. I was wondering what is the best way to do it. Basically i have a string "ABC | DEFGH" i want smarty to get the substring of "DEFGH" How would i go about doing this?

{$var|substr:strpos:"|":strlen}

doesn't work

KJYe.Name
  • 16,969
  • 5
  • 48
  • 63

4 Answers4

28

Just solved this without setting var back in PHP, and by using the built-in function wrappers.

Assuming that: $var = "ABC|DEFGH";

{assign var="bar_at" value=$var|strpos:"|"}
<li>{$var}</li>
<li>{$var|substr:0:$bar_at}</li>
<li>{$var|substr:$bar_at+1}</li>

This will print:

  • ABC|DEFGH
  • ABC
  • DEFGH
  • Jimby
    • 296
    • 2
    • 2
    5

    Some info about Smarty 3 in case if someone want to achieve the same in Smarty 3 and not in Smarty 2:

    The first thing is that you need to add parentheses to make it work. substr:$bar_at+1 won't work but substr:($bar_at+1) will work.

    But in fact you can use simpler syntax:

    {assign var="var" value="ABC | DEFGH"}
    
    {$var|substr:($var|strpos:"|"+1)}
    

    This will give you DEFGH with space at the beginning (before and | are spaces inside var in your question and in my example) and as you want to get string without space you should use in this case +2 instead of +1:

    {$var|substr:($var|strpos:"|"+2)}
    

    Those above were tested in Smarty 3.1.19.

    You should also know that in previous versions (before Smarty 3.1) if you have in your string UTF-8 characters you should rather use mb_ functions.

    As of Smarty 3.1 in case if mbstring extension is installed Smarty automatically uses mb_ functions in that case so there won't be any problem with utf-8 characters.

    You can read more about Smarty utf-8 encoding

    Marcin Nabiałek
    • 109,655
    • 42
    • 258
    • 291
    4

    Those functions do not exist in smarty. You'll have to split it in PHP before sending it to the template. Or you could write your own split function smarty plugin. Or use {php} tags in the template, but I'd avoid that solution as it is against the 'spirit' and purpose of using smarty, separation of presentation and logic, etc etc.

    profitphp
    • 8,104
    • 2
    • 28
    • 21
    • 3
      conclusion - do not use smarty – ajreal Dec 08 '10 at 16:59
    • 1
      The forum post referenced above is in regards to a plugin, which does not come with smarty by default. And truncate does not do what he asked, it just cuts the string to a certain length. – profitphp Dec 08 '10 at 17:09
    • 1
      *never* use {php} tags in a smarty template, use plugin functions instead – Kris Sep 01 '11 at 22:12
    • @kris I agree they should be using sparingly, or not at all if possible, but obviously they put those tags in there so you could use them. Why would you down vote an 8 month old post because you don't agree with it? It certainly was not incorrect or necessarily bad advice. – profitphp Sep 01 '11 at 23:44
    • if you change the answer I can remove the down-vote, it gets locked in if you don't come back for a while. – Kris Oct 21 '11 at 23:17
    • @kris change it to what? It's a valid solution since there is no native split function in smarty. He could split it in php before the template, use php tags in the template, create his own smarty function, or like the accepted answer, use strpos and substring. As I said before, just because your dogma dictates that php tags should NEVER be in smarty, doesn't make it so. If there was some reason why php tags should NEVER be in a smarty template, then the creators would have not included that functionality. I'd definitely try to avoid php tags, but the answer was certainly not 'wrong' – profitphp Oct 22 '11 at 04:59
    0

    When using like Jimby:

    {$var|substr:$bar_at+1}
    

    I add parentheses or round bracket to make it work.

    {$var|substr:($bar_at+1)}
    
    inMILD
    • 23
    • 5