Assigning an item to variable inside a case or if statement not working

Torey Heinz's Avatar

Torey Heinz

14 Jun, 2010 12:45 PM via web

Hey Guys,

We are working and creating two 'sub sites' each under a sub page of the home page. We are using a case statement in multiple places to differentiate between style sheets and logos, so that we can use the same templates. Which has been working great.

The problem we are having right now is we are trying to assign a blog to a variable inside our case statement and it's not getting set.
This is not working.

{% case item.section.path %}
  {% when '/voluntourism/' %}
    {{ '/voluntourism/news/' | item_from_path | assign_to: 'news'}}
  {% when '/trees-for-lives/' %}
    {{ '/trees-for-lives/news/' | item_from_path | assign_to: 'news'}}
{% endcase %} 

{% for article in news.recent_posts.5 %}
  {% include 'article_excerpt' for article %}
{% endfor %}

But if we don't use the case statement, this works.

{{ '/trees-for-lives/news/' | item_from_path | assign_to: 'news'}}

{% for article in news.recent_posts.5 %}
  {% include 'article_excerpt' for article %}
{% endfor %}
  1. 2 Posted by John Nunemaker on 14 Jun, 2010 03:27 PM

    John Nunemaker's Avatar

    Interesting. It is a variable scope thing in liquid. I'm adding something
    now to help you out. Will post back when finished.

  2. 3 Posted by Torey Heinz on 14 Jun, 2010 03:36 PM

    Torey Heinz's Avatar

    Great thanks!!!

  3. 4 Posted by John Nunemaker on 14 Jun, 2010 03:50 PM

    John Nunemaker's Avatar

    Ok, just deployed a new filter to production named assign_to_global.
    assign_to will always just assign to variables local to where you are.
    assign_to_global will assign to the global scope no matter where you are
    calling it. Below is the test case I used and confirmed with:

      {% case '/foo/' %}
        {% when '/foo/' %}
          {{ 'foo' | assign_to_global: 'global_variable' }}
        {% when '/bar/' %}
          {{ 'bar' | assign_to_global: 'global_variable' }}
      {% endcase %}

      {{ global_variable }}

    Give it a shot.

  4. 5 Posted by Torey Heinz on 14 Jun, 2010 04:38 PM

    Torey Heinz's Avatar

    You guys rock!!!

    That works.

  5. Torey Heinz closed this discussion on 14 Jun, 2010 05:20 PM.

Comments are currently closed for this discussion. You can start a new one.

Recent Discussions

17 May, 2012 10:45 PM
20 Mar, 2012 05:05 PM
26 Apr, 2012 01:40 AM
07 Mar, 2012 04:42 PM