Find and replace across entire template

Hi,

Just wondering if there’s a way to do a number of replaces across an entire template, potentially after it has generated the output (i.e. after Freemarker has parsed the model and replaced all the variables, etc).

Right now we do this in a Matrix REST asset that just does a few string replaces over the entire HTML response before displaying to the user, but I’d like to be able to do this on the Funnelback side before the response is returned, if possible.

Cheers,

  • iain

It may be more effective to perform those replacements before the data model gets to the template layer via Hook Scripts:

Any templates in that collection will benefit from corresponding changes to the data model, helping centralise some of the configuration.

Is there any more detail you can provide on the specific scenario you’re trying to solve?

Most of it is adding an additional query string param personal=false to any links within the template, so that after the initial search, clicking any facets, pagination or sorting options will not reapply a personalisation occurring on the site that looks for that param and applies a cookie if it doesn’t find it.

e.g. A user sets their preferred ‘region’, which should be used as a pre-selected facet on the initial search, but should otherwise just respect the other query string params upon clicking anything else.

At the moment, we have a Matrix REST JS asset that looks for &form=listing in the response HTML and replaces it with &personal=false&form=listing. There’s a couple of other replaces, one which is rewriting the webpath/endpoint from search.html to the current page’s URL (which I guess can be better achieved with a blank ui.modern.search.link in the collection.cfg.

We’re also replacing a default/placeholder image with another, so maybe looping through the results in the data model could achieve that, though it might not be that simple if some of the metadata fields have multiple values etc.

The reason I don’t want to just keep using Matrix REST assets is because we’re using Squiz Edge and want to use ESI to fetch the results for better caching and performance.

Hi there,

An alternative is to use hook_pre_process.groovy. Essentially, this script allows you to default or inject parameters which you can then later check in Freemarker.

In your case, you can do something like:

// Check if query = empty and personal = empty (could also check if any facets has been applied) // inject personal=false // else // do something.....
We commonly use this to do things:

  • Create listing pages where we want to show all results *but the user has not entered a query)
  • Default the origin to enable geospatial search
  • Default dates to enable events mode

Hope this helps

That would require me to append &personal=false on every link, including facets, etc… Can I modify the QueryString or similar? Anything that would affect facets, pagination and all other link directives/tags?