2 of 3
2
Trouble using a variable as a parameter
Posted: 03 January 2008 02:51 PM   [ Ignore ]   [ # 19 ]  
Grad Student
Avatar
Rank
Total Posts:  44
Joined  02-16-2005
Lisa Wess - 03 January 2008 02:43 PM

Also, your conditional is a bit off:

{if segment_3 == "C{category_id}" OR "{category_id}"


should be

{if segment_3 == "C{category_id}" OR segment_3 == "{category_id}"


etc.  You need to explicitly declare your comparisons.  That’s demonstrated in the docs on conditionals

I think you’re misunderstanding my intentions (though maybe my clarifying it will lead us to my mistake). The first test is for a category page, the second test is for an entry page (where I can’t get the category_id from segment_3, so I’m getting it from the cookie):

{if segment_3 == "C{category_id}" OR "{category_id}" == "<?php echo $_COOKIE['catcookie']; ?>" }

Profile
 
 
Posted: 03 January 2008 02:55 PM   [ Ignore ]   [ # 20 ]  
Moderator
Avatar
RankRankRankRankRankRankRankRank
Total Posts:  31272
Joined  05-14-2004

Ok, but you’re not telling ExpressionEngine what you are testing against in those ORs.  I don’t know, neither does ExpressionEngine - you need to be explicit.  In English, assuming category ID 3:

If segment 3 is C3

OR ???????? is category_id

OR ???????? is that cookie

What is ????? - you have to be explicit.

 Signature 
Profile
MSG
 
 
Posted: 03 January 2008 02:55 PM   [ Ignore ]   [ # 21 ]  
Grad Student
Avatar
Rank
Total Posts:  44
Joined  02-16-2005
Lisa Wess - 03 January 2008 02:41 PM

My own concern was with the segment, since that is a user modifiable input.  I would definitely make sure to be careful with your cookies too and make sure that you’re sanitizing whatever input can come from them, to keep your site safe.

Thanks. I see the point about the segment vulnerability. When I grab the category segment for the cookie, I’ll add some validation.

Profile
 
 
Posted: 03 January 2008 03:04 PM   [ Ignore ]   [ # 22 ]  
Grad Student
Avatar
Rank
Total Posts:  44
Joined  02-16-2005
Lisa Wess - 03 January 2008 02:55 PM

Ok, but you’re not telling ExpressionEngine what you are testing against in those ORs.  I don’t know, neither does ExpressionEngine - you need to be explicit.  In English, assuming category ID 3:

If segment 3 is C3

OR ???????? is category_id

OR ???????? is that cookie

What is ????? - you have to be explicit.

Sounds like I’m doing something wrong, so let’s get to the bottom of it. Here’s my logic: Since I’m in a weblog:entries loop, I’m assuming that “{category_id}” is the category assigned to the current entry in the loop. Yes, I know that entries can belong to more than one category (I’ve seen you post about this before), but in that case why is there this documented EE syntax?:

{exp:weblog:entries}{categories}{category_id}{/categories}{/exp:weblog:entries}

At any given point in the weblog:entries loop, EE knows what the category is (or categories are). I want that info. Isn’t the above syntax the way to get it?

Profile
 
 
Posted: 03 January 2008 03:16 PM   [ Ignore ]   [ # 23 ]  
Moderator
Avatar
RankRankRankRankRankRankRankRank
Total Posts:  31272
Joined  05-14-2004

Oh, you know what - I totally mis-read your conditional.  I didn’t see the ==, I saw another OR before the cookie.  Sorry about that :(

Did you try using brackets to contain your conditional statements?

{if (segment_3 == "C{category_id}") OR ("{category_id}" == "<?php echo $_COOKIE['catcookie']; ?>") }

I’m not sure that’ll help but it might.  I’d also make sure you test each of those conditionals separately to find out which one is failing.

{if segment_3 == "C{category_id}"}
{if
"{category_id}" == "<?php echo $_COOKIE['catcookie']; ?>"}

But at this point you really need to isolate where the problem is (not including my eyesight =/) so that you can isolate and troubleshoot.

 Signature 
Profile
MSG
 
 
Posted: 03 January 2008 09:49 PM   [ Ignore ]   [ # 24 ]  
Grad Student
Avatar
Rank
Total Posts:  44
Joined  02-16-2005

OK, I seem to have solved the problem. Thanks for your help. Here’s the report.

I’m reversing myself on the necessity of setting a cookie with PHP to get persistent awareness of category even when you’re on an entry page. A cookie technically works, but it has mysterious flakiness that I couldn’t solve. And of course, users can disable cookies.

I’m also retracting my earlier statement that “I see no need to embed templates.” It’s true that my two dynamic menus are never used anywhere else on the site, but the ability to pass “embed variables” appears to be the only way to get early-parsed variables to work inside EE tags. (I’d hope to see that remedied in EE 2.)

I used the Fresh Variables module to define two variables:

current_category = {exp:weblog:entries limit='1'}{categories}{category_id}{/categories}{/exp:weblog:entries}

current_entry
= {exp:weblog:entries limit='1'}{entry_id}{/exp:weblog:entries}

Then I embedded the two menus in the main template with these lines:

{embed="csl_embeds/clients_menu" my_cat="{current_category}" my_entry="{current_entry}"}
{embed
="csl_embeds/projects_menu" my_cat="{current_category}" my_entry="{current_entry}"}

And finally, in the two embedded templates, I built the menu lists with this for Clients:

<div id="clients_menu">
<
ul>
{exp:weblog:categories weblog="projects" style="linear"}
    {if segment_3
== "C{category_id}" OR "{category_id}" == "{embed:my_cat}" }
        
<li class="nav_active">{category_name}</li>
    
{if:else}
        
<li><a href="{path=site/projects}">{category_name}</a></li>
   
{/if}
{
/exp:weblog:categories}
</ul>
<!--
close clients_menu --></div>

...and this for Projects:

<div id="projects_menu">
<
ul>
{exp:weblog:entries weblog="projects" category="{embed:my_cat}" dynamic="off" orderby="date" sort="asc"}
    {if
(segment_3 == "{entry_id}") OR ("{entry_id}" == "{embed:my_entry}") }
        
<li class="subnav_active">{title}</li>
    
{if:else}
        
<li><a href="{permalink="site/projects"}">{title}</a></li>
    
{/if}
{
/exp:weblog:entries}
</ul>
<!--
close projects_menu --></div>

The demo will not be up for very long, but you can see it showing the projects in each category, and just as importantly maintaining “menu highlight” in both Client (category) and Project (weblog entry) menus, regardless of whether you’re in a category or an entry page.

Hope it helps. Thanks.

Profile
 
 
Posted: 03 January 2008 11:23 PM   [ Ignore ]   [ # 25 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  602
Joined  06-29-2005

You read my mind! I just logged on to post about having the same idea about using embeds to solve the problem! Thanks for the elegant solution already worked out! This is deserving of creating a Wiki entry, I’d say! The solution is simple and provides an answer for a common menuing situation.

(I added my first wiki entry the other night on gallery relationships, and it was very easy to simply copy and paste my post and adapt it for first-time readers.)

This is really a very nice solution, I can’t emphasize that enough! The demo works flawlessly! Be sure to send up links to the final page when it’s ready!

Thanks for getting me to discover how to use PHP and especially the substr string function within a template and how to echo a PHP variable inside the exp:weblog:entries tag! I’m sure that and the other string functions will get plenty of use (probably worthy of its own wiki entry.)

Terry

Profile
 
 
Posted: 04 January 2008 01:35 AM   [ Ignore ]   [ # 26 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  602
Joined  06-29-2005

I have a question. How do I keep the {current_category} fresh variable used for embed:my_cat from always reflecting the most current published entry? (Which is what it is currently doing! That is, I THINK it is that… perhaps that variable isn’t doing anything in the embed, and this is default behavior of EE to show the most recent entry as the category_id if none has been explicitly selected.) I’m not sure about the fresh variable assignment here working upon the initial loading of the page. After a link is selected, then the segment variable is active for the conditionals in the embeds… What is the my_cat variable actually doing?
[EDIT:Hmmm… well for one thing, it is passing which “automatically” current page is assigned by EE - based on most recent post date from what I can figure. How to manually assign that one? Make an entry “Sticky” in the edit/publish page? (does seem to work)]

And for some reason, the Projects list is showing all recent projects no matter which category (client) I choose, except for a few of the categories (it is being whacky...) [EDIT - that had something to do with categories which had no posts in them.)

Oh, and in the embed code:

{embed="csl_embeds/clients_menu" my_cat="{current_category}" my_entry="{current_entry}"}

You don’t need to pass my_entry="{current_entry}, since clients_menu doesn’t use it.

Terry

Profile
 
 
Posted: 04 January 2008 09:00 AM   [ Ignore ]   [ # 27 ]  
Grad Student
Avatar
Rank
Total Posts:  44
Joined  02-16-2005
tbritton - 04 January 2008 01:35 AM

I have a question. How do I keep the {current_category} fresh variable used for embed:my_cat from always reflecting the most current published entry? (Which is what it is currently doing! That is, I THINK it is that… perhaps that variable isn’t doing anything in the embed, and this is default behavior of EE to show the most recent entry as the category_id if none has been explicitly selected.) I’m not sure about the fresh variable assignment here working upon the initial loading of the page. After a link is selected, then the segment variable is active for the conditionals in the embeds… What is the my_cat variable actually doing?
[EDIT:Hmmm… well for one thing, it is passing which “automatically” current page is assigned by EE - based on most recent post date from what I can figure. How to manually assign that one? Make an entry “Sticky” in the edit/publish page? (does seem to work)]

And for some reason, the Projects list is showing all recent projects no matter which category (client) I choose, except for a few of the categories (it is being whacky...) [EDIT - that had something to do with categories which had no posts in them.)

Oh, and in the embed code:

{embed="csl_embeds/clients_menu" my_cat="{current_category}" my_entry="{current_entry}"}

You don’t need to pass my_entry="{current_entry}, since clients_menu doesn’t use it.

Terry

Terry, you were very helpful in this whole process. Thanks. I had seen that supersusie comment about using PHP to get segments, but didn’t jump on it the way you did. True, I had been assuming that everything I needed to do would be doable in a straightforward fashion with native EE tags.

Your questions about exactly how and why this is working are good ones. And I don’t have the answers. This is actually my first attempt to build something with EE (after much painful experience with other systems). I’d been looking at EE since pMachine days, and finally decided to use it in a real-world job and make it work. So, I had to find a working solution, and it does bother me that I can’t explain why it works. I’d love it if people familiar with EE internals would supply the why and how—because, frankly, some of the syntax and concepts don’t make great sense, even to me. Why indeed do those current_category and current_entry Fresh Variables of mine actually work? It would be nice to see it documented.

Also, this little intro to EE that I’ve just put myself through makes me suspect that all this may change in EE 2. I say that because I was trying to achieve a pretty simple and straightforward interactivity goal, and it got quite convoluted and mysterious. I encountered too much documentation that basically boiled down to, “variables don’t work the way programmers think they will.” I think EE needs more and better ways to pass parameters.

Yes, every category needs at least one entry. Practically speaking, that’s not a problem for this particular end-user.

You’re right that “current_entry” isn’t needed in the embedded Client menu. It’s a development holdover that I forgot to remove.

Cheers.

Edit: P.S. I do like EE and intend to keep using it. From what I’ve seen Paul post on the EE site, it’s about to cross a significant chasm.

Profile
 
 
Posted: 04 January 2008 09:24 AM   [ Ignore ]   [ # 28 ]  
Grad Student
Avatar
Rank
Total Posts:  44
Joined  02-16-2005
tbritton - 03 January 2008 11:23 PM

This is deserving of creating a Wiki entry, I’d say! The solution is simple and provides an answer for a common menuing situation.

(I added my first wiki entry the other night on gallery relationships, and it was very easy to simply copy and paste my post and adapt it for first-time readers.)

If/when I actually understand why/how it works, I might do that wiki entry. Right now, I don’t feel that I know what I’m talking about, even though the solution appears to be working well enough to deploy.

Yes, weblog relationships to photo galleries are nice. All the images in the Projects section of the site I’m discussing here are done that way. The end-user will be very happy to be able to pull in an image with a simple drop-down menu in the weblog entry form.

Profile
 
 
Posted: 05 January 2008 12:51 AM   [ Ignore ]   [ # 29 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  602
Joined  06-29-2005

Well, here’s my analysis. First, the ClientsAndProjectsPage_Template code:

{!-- First use the Fresh Variables module to define two variables:

current_category = {exp:weblog:entries limit='1'}{categories}{category_id}{/categories}{/exp:weblog:entries}

current_entry
= {exp:weblog:entries limit='1'}{entry_id}{/exp:weblog:entries}

Then embed the two menus inside the main template with these lines
:
--
}
Current Category {current_category} {
!-- This used just for testing purposes --}

{embed
="template_group/_embedded_clients_menu_template" my_cat="{current_category}"}

{embed
="template_group/_embedded_projects_menu_template" my_cat="{current_category}" my_entry="{current_entry}"}

{
!-- The following displays an entry's content resulting from selecting a "project" entry --}

{exp:weblog:entries weblog="weblog" limit="1"}
{summary}
{body}
{/exp:weblog:entries}

What is most interesting here is how the Fresh Variables we’ve created “deliver” just what is needed—the current_category and current_entry—completely externally from the template per se, yet in response to what is called upon by the links and resulting from their own contribution. It is a recursive seeming thing. Fresh Variables must parse VERY early!

Then the “client” code, placed into a template we have called _embedded_clients_menu_template:
(I use underscores instead of periods to indicate a hidden template in my config.php file, because some server hosts do not allow uploading “traditionally” hidden files starting with periods. Yahoo is one such host.)

<div id="clients_menu">
<
h1>Clients Menu</h1>
<
ul>
{exp:weblog:categories weblog="weblog" style="linear" show_empty="no" category_group="1"}
    {if segment_3
== "C{category_id}" OR "{embed:my_cat}" == "{category_id}" }
        
<li class="nav_active">{category_name}</li>
    
{if:else}
        
<li><a href="{path=template_group/ClientsAndProjectsPage_Template}">{category_name}</a></li>
   
{/if}
{
/exp:weblog:categories}
</ul>
<!--
close clients_menu --></div>

I used show_empty="no" to eliminate any empty categories, which muck things up here. category_group="1" is included to narrow down what group is displayed, and could display other groups with digits separated by pipes: category_group="1|4" for instance.

Then the project code, which we have called _embedded_projects_menu_template:

<div id="projects_menu">
<
h1>Projects Menu</h1>
<
ul>
{exp:weblog:entries weblog="weblog" category="{embed:my_cat}" dynamic="off" sort="desc"}

    {if
(segment_3 == "{entry_id}") OR ("{embed:my_entry}" == "{entry_id}") }
        
<li class="subnav_active">{title}</li>
    
{if:else}
        
<li><a href="{permalink="template_group/ClientsAndProjectsPage_Template"}">{title}</a></li>
    
{/if}
{
/exp:weblog:entries}
</ul>
<!--
close projects_menu --></div>

I did find that our Fresh Variables were definitely grabbing the most recent entry to the weblog to get its category_id and entry_id information initially upon page load (that is, when no category/segment_3 or entry/segment_3 exist in the URL). Also, once an entry is selected from among the “projects” listing, the entry id makes implicit the category id, and that info is being fed to our Fresh Variable ({current_category} which becomes my_cat). That’s why the test for my_entry and my_cat are always needed, especially after entries are selected, to keep the category info up to date (in our _embedded_clients_menu_template).

I had to make an entry “sticky” in the edit page (in Options) in order to force a desired particular entry and its associated category to be the top one, so that the topmost link being displayed was also the one showing as already selected and current. I wonder if there is another way in our scenario to get the Fresh Variables we used to receive some other default initial setting? Does a mess occur if an entry has multiple categories assigned to it? Probably it takes the lowest numbered one… need to test that, or simply avoid that situation!

After we start actually clicking on “client” links, though, EE reads from the URL and acts just like dynamic is turned on! This indicates that our permalinks and path= statements are working right (always dynamically, generated from the database), and that our test for segment_3 is functioning like it should be, reading from the URL to determine if the dynamically generated listing is also the current listing, while keeping categories in entries in our awareness through the my_cat test, so the “client” (actually the category) continues to be shown as the active one when the url is showing an entry_id now instead of a category_id in segment_3.

Much of what is going on is what dynamic="on" makes happen in a typical situation behind the scenes, but we are performing that test explicitly, and manually, returning the same results while keeping things under our control so that the “clients” list stays constant irrespective of the URL, yet the “projects” list changes based upon “client” selected, and the links show their “current link” status.

I still say that this is elegant enough to merit a Wiki entry. Maybe somebody will drop by and clear up any errors in my analysis here.

Terry

Profile
 
 
Posted: 05 January 2008 02:20 PM   [ Ignore ]   [ # 30 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  602
Joined  06-29-2005

Perhaps a good name for the Wiki entry would be:

Dynamically Generated Client/Project Menus System with Current-page Highlighting

or something along those lines?

Michael Boyink has also explored multi-tier navigation techniques mostly utilizing CSS assignments quite a bit at his site. But none of these perform the extra features which spacewalk’s solution accomplishes.

Terry

Profile
 
 
Posted: 05 January 2008 02:35 PM   [ Ignore ]   [ # 31 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  602
Joined  06-29-2005

It just occurred to me that the Fresh Variables were not necessary after all - the {exp:weblog:entries} code could have been coded directly into the embed instruction longhand.

That is,

{embed="template_group/_embedded_clients_menu_template" my_cat="{current_category}"}

{embed
="template_group/_embedded_projects_menu_template" my_cat="{current_category}" my_entry="{current_entry}"}

which used the Fresh Variables of {current_category} and {current_entry} could have also been rendered as:

{embed="template_group/_embedded_clients_menu_template" my_cat="{exp:weblog:entries limit='1'}{categories}{category_id}{/categories}{/exp:weblog:entries}"}

{embed
="template_group/_embedded_projects_menu_template" my_cat="{exp:weblog:entries limit='1'}{categories}{category_id}{/categories}{/exp:weblog:entries}" my_entry="{exp:weblog:entries limit='1'}{entry_id}{/exp:weblog:entries}"}

Using the fresh variables makes the code easier to read, but perhaps wasn’t needed?

[Edit: confirmed. This still works fine! So, our using the Fresh Variables somewhat obscured our seeing the mechanism really at work here, and it turns out that it all worked simpler than I thought.]

Terry

Profile
 
 
Posted: 05 January 2008 02:52 PM   [ Ignore ]   [ # 32 ]  
Grad Student
Avatar
Rank
Total Posts:  44
Joined  02-16-2005

I agree that this basic technique could be useful to a lot of people who want to do multi-tier dynamic navigation. Pretty common need. And therefore a Wiki article (tutorial, really) would be nice. I hesitate to do it right now, however, because it sounds like EE2 is in internal testing already, and I wonder if it will obviate this whole technique. Maybe wait for v.2 and see if the article is still relevant?

I thought your analysis was good. Yes, this is basically a method for emulating dynamic="on" (caring about what’s in the URL) while maintaining a stable listing (not caring about what’s in the URL).

I did a quick test of multiple categories; sure enough, it fails: I assigned two Clients (categories) to a Project entry. The result: if the entry is sticky, neither of the two Clients produces an associated Project listing (it’s empty), though all other clients work OK. If it’s not a sticky entry, it gets listed for both categories but the behavior is flaky: the entry listing for one of the Clients disappears when clicked, just as it would with dynamic="on"; the entry listing for the other Client remains, but the Client doesn’t get highlighted; and that was just with the quickest of tests. It’s probably broken in other ways too.

It’s probably possible to knock together a variant that would let an entry be listed for 2 or more categories, and when that entry was chosen all its related categories would be highlighted in the cat listing. Less obviously useful, though, and not a high priority for me at the moment.

Good point on using show_empty="no" for the categories listing.

Profile
 
 
Posted: 05 January 2008 02:58 PM   [ Ignore ]   [ # 33 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  602
Joined  06-29-2005

spacewalk: Did you see my entry on Fresh Variables above?

EllisLab isn’t planning on making any announcements one way or another until March, so a Wiki article would still do many much good.

I assumed multiple categories would break everything!!! Heh…

Terry

Profile
 
 
Posted: 05 January 2008 02:58 PM   [ Ignore ]   [ # 34 ]  
Grad Student
Avatar
Rank
Total Posts:  44
Joined  02-16-2005
tbritton - 05 January 2008 02:35 PM

It just occurred to me that the Fresh Variables were not necessary after all - the {exp:weblog:entries} code could have been coded directly into the embed instruction longhand.

.  .  .

Using the fresh variables makes the code easier to read, but perhaps wasn’t needed?

Yes, but using the Fresh Variables module gets you more than just readability: You have a variable that you can use in multiple embedded templates, and its code is maintained in only one place. Much better, I think.

Profile
 
 
Posted: 05 January 2008 03:00 PM   [ Ignore ]   [ # 35 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  602
Joined  06-29-2005
spacewalk - 05 January 2008 02:58 PM
tbritton - 05 January 2008 02:35 PM

It just occurred to me that the Fresh Variables were not necessary after all - the {exp:weblog:entries} code could have been coded directly into the embed instruction longhand.

.  .  .

Using the fresh variables makes the code easier to read, but perhaps wasn’t needed?

Yes, but using the Fresh Variables module gets you more than just readability: You have a variable that you can use in multiple embedded templates, and its code is maintained in only one place. Much better, I think.

Agreed!

Found post where it is mentioned no announcements till SXSW show in March.

Terry

Profile
 
 
Posted: 14 January 2008 12:03 AM   [ Ignore ]   [ # 36 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  602
Joined  06-29-2005

I’ve discovered a FANTASTIC re-purposing of the client/projects links technique while setting up my site to display my short stories and serialized novel, namely using it for story/chapter link display! This is the ultimate solution, allowing bookmarking by the reader of the chapter they’d left off in, and I’ve added simple pagination and a technique for page bookmarking as well. I’ll detail how I did it in a later post as I work it out to perfection.

Thanks, spacewalk, for setting me off in the right direction with your explorations!

Terry

Profile
 
 
   
2 of 3
2
 
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 10:33 AM
Total Registered Members: 61008 Total Logged-in Users: 15
Total Topics: 73778 Total Anonymous Users: 8
Total Replies: 398021 Total Guests: 483
Total Posts: 471799    
Members ( View Memberlist )