Search code examples
htmlliquidbusiness-catalysthreflang

Create hreflang tags only using Liquid in Business catalyst


I was wondering if there is a more efficient way to create hreflang tags, just using liquid in BC, without the need to create webapp.

I tried this way which makes sense, but it doesn't work for some reason.

{% capture pagURL -%}{module_pageaddress}{% endcapture -%}
{% if pagURL contains "http://us." -%}
<link rel="alternate" href="{{ pagURL}}" hreflang="en-us" />
<link rel="alternate" href="{{ pagURL | replace: 'http://us', 'http://www' }}" hreflang="en-uk" />
<link rel="alternate" href="{{ pagURL | replace: 'http://us', 'http://au' }}" hreflang="en-au" />
<link rel="alternate" href="{{ pagURL | replace: 'http://us', 'http://eu' }}" hreflang="en" />
{% elsif pagURL contains "http://au." -%}
<link rel="alternate" href="{{ pagURL}}" hreflang="en-au" />
<link rel="alternate" href="{{ pagURL | replace: 'http://au', 'http://www' }}" hreflang="en-uk" />
<link rel="alternate" href="{{ pagURL | replace: 'http://au', 'http://us' }}" hreflang="en-us" />
<link rel="alternate" href="{{ pagURL | replace: 'http://au', 'http://eu' }}" hreflang="en" />
{% elsif pagURL contains "http://eu." -%}
<link rel="alternate" href="{{ pagURL}}" hreflang="en" />
<link rel="alternate" href="{{ pagURL | replace: 'http://eu', 'http://us' }}" hreflang="en-us" />
<link rel="alternate" href="{{ pagURL | replace: 'http://eu', 'http://au' }}" hreflang="en-au" />
<link rel="alternate" href="{{ pagURL | replace: 'http://eu', 'http://www' }}" hreflang="en-uk" />
{% elseif pagURL contains "http://www." -%}
<link rel="alternate" href="{{ pagURL}}" hreflang="en-uk" />
<link rel="alternate" href="{{ pagURL | replace: 'http://www', 'http://us' }}" hreflang="en-us" />
<link rel="alternate" href="{{ pagURL | replace: 'http://www', 'http://au' }}" hreflang="en-au" />
<link rel="alternate" href="{{ pagURL | replace: 'http://www', 'http://eu' }}" hreflang="en" />
{% else -%}
{% endif  -%}

The weird part is, the following it works on that same page.

{% capture pagURL -%}{module_pageaddress}{% endcapture -%}
{{ pagURL}}<br>
{{ pagURL | replace: 'http://www', 'http://us' }}<br>
{{ pagURL | replace: 'http://www', 'http://au' }}<br>
{{ pagURL | replace: 'http://www', 'http://eu' }}<br>

And this also works

{{ pagURL | replace: 'http://www', 'http://us' | prepend: '<link rel="alternate" href="' | append: '" hreflang="en-us" />' }}

The shorter the code the better of course.


Solution

  • Ok Daut, you're going to love this! ;p

    I ran into similar obstacles when working on a solution for this issue. (I'll explain below).

    As far as I can tell, BC has some issues around its rendering of Liquid when it comes to URLs and variables. I don't really understand the ins and outs of Liquid in terms of the server-side processing but things do not appear to work as they should in BC.

    For example, if we take your code and strip it back to its bare essentials:

    This (whether inserted into the <head> or <body>) DOESN'T work:

        {% capture pagURL -%}
            {module_pageaddress}
        {% endcapture -%}    
    
        <link rel="alternate" href="{{pagURL}}" hreflang="en-us">
    

    It outputs <link rel="alternate" href="{{pagURL}}" hreflang="en-us">, {{pagURL}} being the literal text that is rendered.

    But this DOES work:

        {% capture pagURL -%}
            {module_pageaddress}
        {% endcapture -%}     
    
        {% assign test = '<link rel="alternate" href="' | append: {{pagURL}} | append: '" hreflang="en-us">' -%}
    
        {{test}}
    

    Compare the above to the issues I ran into.

    This DOESN'T work:

        {module_pageaddress collection="page" template=""}
        {module_siteUrl collection="site" template=""}
    
        {% assign fullUrl = "http://{{site.siteUrl}}/" -%}
    
        {% if page.pageUrl == {{fullUrl}} -%}
    
            // We are on the home page
    
        {% else -%}
    
            // We are not on the home page
    
        {% endif -%}
    

    The problem here is {{site.siteUrl}} inside the variable declaration. Not sure what the issue is but it just can't handle the jandle.

    But this DOES work:

        {module_pageaddress collection="page" template=""}
        {module_siteUrl collection="site" template=""}
    
        {% assign fullUrl = 'http://' | append: {{site.siteUrl}} | append: '/' -%}
    
        {% if page.pageUrl == {{fullUrl}} -%}
    
            // We are on the home page
    
        {% else -%}
    
            // We are not on the home page
    
        {% endif -%}
    

    And finally getting back to your example, (ironically) this works:

        {% capture pagURL -%}
            {module_pageaddress}
        {% endcapture -%}
    
        <a href="{{pagURL}}">test</a>
    

    Go figure.

    I'm pretty sure that BC has some automatic way of grabbing head elements contained anywhere on a page and re-inserting them into the head (even if they were inserted into the head to begin with).

    I wonder if that causes problems in cases like <link rel="alternate" href="{{pagURL}}" hreflang="en-us">. It doesn't explain the issue I ran into though.

    End of the day, I think there are still some big issues with BC's implementation of Liquid.