replacing text with a variable

In this tutorial we will learn how to use a modifier and how to replace a text with specific value.

Things you will learn in this article:

  1. how to use the replace modifier and
  2. how to replace some text with a variable and how to run a variable within smarty code

If you have more complex templates, you will often need this modifier. It can save you code and can make things easier.

Imagine you retrieve data from your content or a module. For instance you write this content block in your template:

{content block="myvalue" assign="myvalue" oneline="true"}

We assigend it to the variable $myvalue. This is useful if we want to modify or work with block. In this case, we do not need WYSIWYG but just one line. Therefore we add the paramater oneline. If we enter a value like "600px" in the page which uses the template with the code above, we get exactly this value in the frontend. But in some cases, we just need the integer value without the "px" suffix. Now we can use the replace modifier:


What did we do? We added the replace modifier and then we look for "px" to replace it with something. In this case with nothing. The result is now: "600". Now, what if we want em instead of px? Well, that's still easy:


Now we get "600em".

Well, we have just learned how to use the replace modifier. Let's go one step further and make it a little bit more dynamic.

{assign var="myunit" value="em"}

That looks somehow weird. But we work in Smarty environment. And this is similar to mysql where we need backticks to run a variable within the smarty code. Otherwise it would output this: "600$myunit". But with the backticks, it will be passing the value from the myunit var: "600em". That's all. It depends on you what you can do with it. It's just one example.