Business Catalyst:检查Liquid是否在根URL上 [英] Business Catalyst: Check if we are on the root URL with Liquid
问题描述
我想使用Liquid标记来测试正在查看的页面是否是主页(但具体来说是网站的根URL),例如www.mysite.com/
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/
我尝试使用{{globals.get}},因为根据 BC文档,这应该输出...
I attempted to use {{globals.get}}, because according to the BC Documentation, this should output...
当前相对路径和在URL中传递的查询参数.例如,对于URL http://mtica1.businesscatalyst.com/_s1/s2?a=1& ; b = 2 模块是:
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"
}
[强调我的]
但是,看来Business Catalyst不能始终如一地处理此Liquid标签.在网站根目录{{globals.get}}
的输出{}
以及其他系统页面(例如电子商务布局)上,ID
被替换为CatalogueID: "xxxxx"
,其中xxxxx是目录的ID,而不是当前相对路径" !
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!
不用说,我无法按预期输出ID: "/"
来定位站点根目录.
Needless to say, I was unable to output ID: "/"
to target the site root as expected.
如何使用Liquid测试用户是否在根URL上?
推荐答案
我在此答案的开头添加了另一个解决方案,但是将我原来的解决方案留在下面,以便其他人可以看到我所经历的过程.
尽管我对下面详细介绍的解决方案没有任何问题,但是由于空白"在BC中没有记录,所以我希望该解决方案在BC Dev团队进行更新时不会被破坏.
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.
您可以使用以下Liquid标记来检查您是否是站点根目录:
{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 -%}
上级解决方案
您可以使用以下Liquid标记来测试您是否在站点根目录上:
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.
我进行了许多测试,发现唯一的页面(作为{{globals.get}}
的结果输出{}
的页面)是根URL. (注意:我尚未对此进行详尽的测试-如果您找到另一个输出{}
的页面,请发表评论)
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 {}
)
我尝试了{% if globals.get == {} -%}
,但这只是针对所有页面,因为所有页面在{}
中都有输出.
I tried {% if globals.get == {} -%}
but this simply targets all pages because all of them have something output inside {}
.
BC文档没有任何内容,但我发现此文档提到它并感到惊讶,惊奇:它有效.
BC Documentation had nothing, but I found this document which mentions it and surprise, surprise: it works.
我们只希望BC Dev团队不要去做一些改变,以免其无法正常工作.
Let's just hope the BC Dev team don't go and change something that stops this from working.
这篇关于Business Catalyst:检查Liquid是否在根URL上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!