1

Good morning and thank you in advance. How can I replace '#' with the entry.title. I have tried everything and I keep getting hash errors. Current code is this:

<h3>{{ wordBoldTitle|replace({'#':'<strong class="font-weight-extra-bold">{{entry.title}}</strong>'} )|raw }}</h3>

Trying to concatenate the with the entry.title.

2

In Twig, you never want to use delimiters inside other delimiters (e.g. {{ ... {{ ... }} }}).

Here's how you would format it instead:

<h3>{{ wordBoldTitle | replace('#', '<strong class="font-weight-extra-bold">' ~ entry.title ~ '</strong>') | raw }}</h3>

What we're doing here is using Twig concatenation (via the ~ operator) to combine our first string (<strong class="font-weight-extra-bold">), whatever string entry.title results in, and then our second string (</strong>).

Alternatively we can use string interpolation to achieve this; however string interpolation requires double-quotes, so to do this we'll need to escape the double quotes already in our string:

<h3>{{ wordBoldTitle | replace("#", "<strong class=\"font-weight-extra-bold\">${entry.title}</strong>") | raw }}</h3>

I also changed your replace filter from using a hash (e.g. {'#':'xxx'} to the simpler (at least in this case) comma-separated parameters. The hash works well if you need to replace multiple items, but the comma-separated parameters looks cleaner in my opinion when you only need to replace one item. You can find more about the replace filter formatting here.

| improve this answer | |
  • Works like a charm. Thank you – Jason Nov 20 '19 at 16:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.