How can I replace hyphens with spaces in title tags?

For collection pages that are filtered by tags, our existing code for outputting page titles is mostly working as intended. However, tags with hyphens in them are having the hyphens removed, whereas we would like them to be replaced with spaces. Ideally we’d be able to replace “-and-” with " & ", but even just getting the dashes replaced with spaces would be fine.

This is the code that we’re currently using:

{%- assign meta_tags = current_tags | join: ', ' %} – {{ 'general.meta.tags' | t: tags: meta_tags - | remove: "Tagged" | remove: """ | camelcase }}

Any help in getting this figured out would be greatly appreciated, thanks!

Hi @HopeIsReal ,

You can try escape. See code below. You can check the documentation here

{%- assign meta_tags = current_tags | join: ', ' -%} – {{- 'general.meta.tags' | t: tags: meta_tags - | remove: "Tagged" | remove: """ | camelcase | escape -}}

Or you can do the following to replace “-and-” with “&”. You can check the documentation here.

{%- assign meta_tags = current_tags | join: ', ' -%} – {{- 'general.meta.tags' | t: tags: meta_tags - | remove: "Tagged" | remove: """ | camelcase | replace: "-and-", "&" -}}

Unfortunately this didn’t work. I had actually previously tried this approach. The Replace tag is working in breadcrumbs and headings on pages, but in the meta_tags implementation it doesn’t seem to work. Spaces are still missing.

I might be missing something here. Maybe add your meta-tag code?

Honestly, I don’t understand the code below. We are changing the translated words?

{{- 'general.meta.tags' | t: tags: meta_tags | remove: "Tagged" | remove: """ | camelcase | replace: "-and-", "&" -}}

Maybe it’s because of something outside of this set of tags? Here’s the full code:

{%- capture seo_title -%}
  {{ page_title }}
  {%- if current_tags -%}
    {%- assign meta_tags = current_tags | join: ', ' -%} – {{- 'general.meta.tags' | t: tags: meta_tags - | remove: "Tagged" | remove: """ | camelcase | replace: "-and-", "&" -}}
  {%- endif -%}
{%- endcapture -%}

The remove: “Tagged” and remove: “”" are to remove those words from the page titles on filtered collections. So instead of Collection Name - tagged “hatsandbeanies” - Store Name, I want it to read Collection Name - Hats & Beanies - Store Name. (hats-and-beanies is the tag.)

I just tried this and it looks like it worked. My guess is it has something to do with the “replace” portion of the tag coming after the “camelcase”.

{%- assign meta_tags = current_tags | join: ', ' %} – {{ 'general.meta.tags' | t: tags: meta_tags - | remove: "Tagged" | remove: """ | camelcase | replace: "And", " & " }}

So the general.meta.tags is just “Collection Name”. Sorry trying to understand.

Try this instead

{%- capture seo_title -%}
  {{ page_title }}
  {%- if current_tags -%}
    {%- assign meta_tags = current_tags | join: ', ' -%} 
Collection Name - {{- meta_tags | replace: "-and-", " & "  | camelcase -}} - {{ shop.name }}
  {%- endif -%}
{%- endcapture -%}