Business Catalyst: Check if we are on the root URL

2019-07-19 03:17发布

问题:

I would like to use Liquid markup to test if the page that is being viewed is the home page (but specifically the root URL of a website), e.g. www.mysite.com/

I attempted to use {{globals.get}}, because according to the BC Documentation, this should output...

The current relative path and the query parameters passed in the URL. For example, for the URL http://mtica1.businesscatalyst.com/_s1/s2?a=1&b=2 the output of the module is:

{
    "ID": "/_s1/s2",
    "a": "1",
    "b": "2"
}

[emphasis mine]

However, it appears that Business Catalyst does not deal with this Liquid tag consistently. On the site root {{globals.get}} outputs {} and on other system pages such as the eCommerce layouts, ID is replaced with CatalogueID: "xxxxx", where xxxxx is the ID of the Catalogue, NOT the "current relative path" at all!

Needless to say, I was unable to output ID: "/" to target the site root as expected.

How can I test if a user is on the root URL using Liquid?

回答1:

EDIT: I have added another solution to the beginning of this answer but have left my original solution below so others can see the process I went through.

While I have had no issues with the solution detailed below, due to "blank" being undocumented in BC, I would prefer a solution that does't have the potential to be broken if the BC Dev team make updates.


NEW SOLUTION

You can check if you are the site root using the following Liquid markup:

    {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 site root

    {% else -%}

        // we are not on the site root

    {% endif -%}

PREVIOUS SOLUTION

You can test if you are on the site root using the following Liquid markup:

{% if globals.get == blank -%}
    // we are on the root URL - do something
{% else -%}
    // we are not on the root URL
{% endif -%}

The "key" piece of information here is the keyword blank.

I did a number of tests and discovered that the only page that outputs {} as a result of {{globals.get}} is the root URL. (Note: I have not tested this exhaustively - please comment if you find another page that outputs {})

I tried {% if globals.get == {} -%} but this simply targets all pages because all of them have something output inside {}.

BC Documentation had nothing, but I found this document which mentions it and surprise, surprise: it works.

Let's just hope the BC Dev team don't go and change something that stops this from working.



回答2:

that is not actually correct. System pages and other pages through modules will also encounter that. Globals.get is data and can occur on the home page through many different things like if you have google analytics or other 3rd party tracking, certain clicks through and more. Unfortunately the above is a very inaccurate way to do that.

Could you explain what your goal is - Why you need this?