Supportlogo-header

Last edited 02:02AM, Apr 26, 2007

Eric Member

identifying page in the layout

Hi, I’m modifying my theme.liquid file, and I need a way for the global nav to know which page is currently being displayed.

I tried this, just as a proof-of-concept (this is the super-simplified version of what I eventually want to do!):

  {% for link in linklists.main-menu.links %}
    -- {{ link.title | link_to: link.url }}
    {% if link.title == page.title %}
         (this is the active page!)
    {% endif %}
    <br>
  {% endfor %}

But page.title doesn’t seem to be available to me at this point in the code. Any suggestions?

thanks,
Eric

Internet Themepark Inc. Los Angeles, California check out our latest project: http://teslamotors.com

02:10AM, Apr 26, 2007

Jared Burns Shopify Advisor

You’ll need to use a variety of statements that are specific to the page you’re currently at. Here’s some examples to help you get on track…

  • {% if collection.title == link.title %}class="current"{% endif %}
  • {% if page.title == link.title %}class="current"{% endif %}
  • {% if blog.title == link.title %}class="current"{% endif %}
  • {% if product.title == link.title %}class="current"{% endif %}
  • {% if link.url == "/cart" %}class="current"{% endif %}

You’ll probably want to group them into a single if/elsif statement or use case/when. You may also need to nest if statements, such as when using {% if template %}.

06:07AM, Apr 26, 2007

danW Shopify Advisor

try page_title instead.

Last edited 08:18PM, Apr 26, 2007

Eric Member

Thanks, page_title worked. Is there a place in the wiki that explains when it should be page.title vs page_title?

My other problem I’m having now is that a complex ‘if’ statement doesn’t seem to work—I have a statement like this:

{% if a = = b OR ( b = = c AND c = = d) %}

... but even when b = = c and c = = d, it doesn’t fire. Is that a limitation of the liquid langauge?

(p.s. sorry about the weird spacing, the forum is rejecting two equal signs next to each other, no idea why.)

Internet Themepark Inc. Los Angeles, California check out our latest project: http://teslamotors.com

09:10PM, Apr 26, 2007

tobi Shopify

the page object ( of which .title is a attribute ) is automatically available on urls with /pages/ in them. If the url references a certain page then shopify exports the page as a object just like a collection object is available on collection pages and a product on product pages. page_title is the actual title of the page which is usually equal to the entity which the page references such as product.title, page.title or ‘Welcome’ on the frontpage.

Liquid currently doesn’t support complex boolean expressions. this is a long standing feature request and i got at least 3 people who promised to contribute it to the open source project but nothing has materialized so far.

In many cases you can work around this limitation with nested ifs or with the case statement or with a combination out of both.

Tobias Lütke - Shopify CEO // http://twitter.com/tobi

login or Sign up for an account to reply.