Assigning an item to variable inside a case or if statement not working
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 %}
Comments are currently closed for this discussion. You can start a new one.
2 Posted by John Nunemaker on 14 Jun, 2010 03:27 PM
Interesting. It is a variable scope thing in liquid. I'm adding something
now to help you out. Will post back when finished.
3 Posted by Torey Heinz on 14 Jun, 2010 03:36 PM
Great thanks!!!
4 Posted by John Nunemaker on 14 Jun, 2010 03:50 PM
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.
5 Posted by Torey Heinz on 14 Jun, 2010 04:38 PM
You guys rock!!!
That works.
Torey Heinz closed this discussion on 14 Jun, 2010 05:20 PM.