Page 1 of 1

Replacing text with a variable in content

Posted: Fri May 13, 2016 9:03 am
by angelpeace
My content page:

"This is my website." -> This is my website.

{content assign="myvalue"}
{assign var="key1" value="website"}
{assign var="changeto" value="<a href='#link1' title='Title Key 1'>{$key1|capitalize}</a>"}
{$myvalue|replace:"`$key1`":"`$changeto`"}

I want:

{content assign="myvalue"}
{assign var="key1" value="my"}
{assign var="key2" value="website"}
{assign var="changeto1" value="<a href='#link1' title='Title Key 1'>{$key1|capitalize}</a>"}
{assign var="changeto2" value="<a href='#link2' title='Title Key 2'>{$key2|capitalize}</a>"}

With a key 1
{$myvalue|replace:"`$key1`":"`$changeto1`"}
and key 2 and ...
{$myvalue|replace:"`$key2`":"`$changeto2`"}

=> could replace more text in one {content assign="myvalue"}

Thanks,

Re: Replacing text with a variable in content

Posted: Thu May 19, 2016 11:56 pm
by Jeff
You could use the php function str_replace:

Code: Select all

{content assign="myvalue"}
{assign var="key1" value="my"}
{assign var="key2" value="website"}
{assign var="changeto1" value="<a href='#link1' title='Title Key 1'>{$key1|capitalize}</a>"}
{assign var="changeto2" value="<a href='#link2' title='Title Key 2'>{$key2|capitalize}</a>"}

{[$key1,$key2]|str_replace:[$changeto1,$changeto2]:$myvalue}
Actually when looking at the code for the modifier replace it is a wrapper for str_replace so you use it as:

Code: Select all

{$myvalue|replace:[$key1,$key2]:[$changeto1,$changeto2]}