Using replace with two variables

In my configuration file, I have this code–

copyright  = "© 2019-{year}"

Which works with this code in the footer–

{{ replace site.Copyright "{year}" (string (now.Format "2006") )

I want now to add the code below with {title} representing site.Title

copyright = "© 2019-{year} {title}"

I need help with editing the code above in the footer to show both the year and the date.

Just nest the replaces… one replace statement becomes the input to the other replace statement

An example?

{{ $footerline := replace site.Copyright "{year}" (string (now.Format "2006")) }}
{{ $footerline = $footerline | replace "{title}" site.Title }}

First line replaces the {year} in site.Copyright with the current year and sets $footerline to that replaced string. Second line “pipes” $footerline into the replace function (this replaces the first parameter) and replaces {title} with site.Title.

Note the := on the first line - you initiate the variable - and the = in the subsequent line (and all that might follow) - you change the variable.

This should work, but I didn’t test it.

Outputs {title} as the result.

Because you can’t pipe outputs into replace.

Do something like this

{{ replace (replace site.Copyright "{year}" (now.Format "2006")) "{title}" site.Title }}
2 Likes

Awesome!

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.