if conditional working very strange
Posted: 21 August 2007 05:29 AM   [ Ignore ]  
Grad Student
Rank
Total Posts:  31
Joined  09-26-2006

In a comments template of mine up at the very top, before I use any entries tags like:

{exp:comment:entries weblog="news" sort="{sort}" paginate="both" limit="3"}


I have the following code:

{if segment_4 == "desc"}

<p>Assigning variable sort to: desc</p>
{assign_variable:sort="desc"}

{if
:else}

<p>Assigning variable sort to: asc</p>
{assign_variable:sort="asc"}

{
/if}

<p>Even though segment 4 is: {segment_4}</p>

<
p>Sort is still: {sort}</p>

The 1st output if {segment_4} is NOT “desc” is “Assigning variable sort to: asc” as it should be because the {if:else} catches and outputs the “<p>Assigning variable sort to: asc</p>” 

However the next line “{assign_variable:sort=“asc”}” in the {if:else} does not process even though the first line(the HTML) processes.  In fact it is the “{assign_variable:sort=“desc”}” that processes as evidence by the last line in the output “<p>Sort is still: {sort}</p>” which process to “Sort is still: desc” and if I change “{assign_variable:sort=“desc”}” to something like “{assign_variable:sort=“xxx”}” it will indeed output {sort} as “xxx” even though it says “Assigning variable sort to asc”.

In other words, in what should be either:

<p>Assigning variable sort to: desc</p>
{assign_variable:sort="desc"}

OR

<p>Assigning variable sort to asc</p>
{assign_variable:sort="asc"}

based on if the if conditional is met, actually pulls the <p>...</p> HTML from one and processes the {assign_variable} tag from the other if the if conditional is NOT met.  (It works correctly if the if conditional IS met by {segment_4} being “desc” and outputs “Assigning variable sort to: desc”)

Obviously my gripe is I can’t get my {sort} variable to store “asc” even though my if conditional is NOT met (I want “asc” to be the deault, and {sort} to turn over to “desc” only if it is explicitly in segment_4 of my url).

By the way, when {sort} is used in the rest of my template, it behaves as it was outputted “Sort is still: desc” making my comments descending and if I force {sort} to become “asc” by changing

<p>Assigning variable sort to: desc</p>
{assign_variable:sort="desc"}

to

<p>Assigning variable sort to: desc</p>
{assign_variable:sort="asc"}

so both if and else assign “asc” to {sort} it behaves as you would expect it to changing the order of my comments to ascending and reporting that “<p>Sort is still: asc</p>”

There must be something stupid that I’m missing, not to mention I just spun my head (and probably yours) in circles trying to explain my problem. confused

Profile
 
 
Posted: 21 August 2007 06:36 AM   [ Ignore ]   [ # 1 ]  
Research Assistant
RankRankRank
Total Posts:  597
Joined  10-22-2006

Your problem is due to the parse order of the tags.

Advanced conditionals are parsed after the assign_variable-tag.

If you use simple conditionals it ought to work, though. Try this

{if segment_4 == "desc"}
<p>Assigning variable sort to: desc</p>
{assign_variable:sort="desc"}
{
/if}

{if segment_4
== "asc"}
<p>Assigning variable sort to: asc</p>
{assign_variable:sort="asc"}
{
/if}

{if sort
== ""}
{assign_variable
:sort="asc"}
{
/if}

<p>Segment 4 is: {segment_4}</p>

<
p>Sort is: {sort}</p>

Profile
 
 
Posted: 21 August 2007 07:07 AM   [ Ignore ]   [ # 2 ]  
Grad Student
Rank
Total Posts:  31
Joined  09-26-2006

Your right silenz!  Thx.  It now works great.  By the way, where can I find (in the docs if available) the parse order of things, and some hints to potential problems in this regard, so I may avoid similar confusion in the future?

Profile
 
 
Posted: 21 August 2007 07:15 AM   [ Ignore ]   [ # 3 ]  
Moderator
Avatar
RankRankRankRankRankRankRank
Total Posts:  23538
Joined  05-20-2002

Check the wiki- it’s got a rundown on parse order that should be handy.

 Signature 

AKA rob1

Help Request TipsPro Network

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 1149, on July 16, 2007 09:33 AM
Total Registered Members: 65026 Total Logged-in Users: 42
Total Topics: 82116 Total Anonymous Users: 17
Total Replies: 441305 Total Guests: 193
Total Posts: 523421    
Members ( View Memberlist )
Newest Members:  meenoiYang.JianuoioitsukiNathan HammondalexcigadamstaneckiLucas Mayscybermilltstitt