Making an if statement for the current url

Ok, I give up. I’ve been struggling to figure this out and just can’t so I’m hoping someone can help.



I’ve created a horizontal menu and coded it in, but I want the appropriate menu item to be highlighted when it is the active thing. I don’t know how to describe what I want to do so I’ll show you.



This is how my menu looks now:







This is how I want it to look:







This is the code I am using, but it isn’t working quite right:


```php




```

You can see that I am using:

```php {if $current_url == 'lingcod-jigs.html'} ```

Am I using this wrong?

The URL is:

lingcod-jigs.html

Any help would be greatly appreciated.

Thank you,

Brandon

I “think” you’re going to have to get to the actual dispatch= syntax for the current_url. Since this is all dynamic, it needs to fall down to the lowest denominator which would be the dispatch syntax of dispatch=categories.view&category_id=7 or some such.



What is the value of $current_url in the context you are in (insert a {debug} into your template)?

Hey Tony, thanks for the reply. I’m not exactly sure if I’m following you though.



I just changed the statement to:


{if $current_url == 'index.php?dispatch=categories.view&category_id=1'}



Is that what you are talking about?



Also, what did you mean by:


[quote]What is the value of $current_url in the context you are in (insert a {debug} into your template)?[/quote]



Thanks and I really appreciate any help you can give.



Brandon

I was asking what the value of $current_url is within the template that you are trying to set the ‘active page’ for. The best way to derive this is to add [debug} to the bottom of the template and then post the value of ‘current_url’ here. I was mostly curious as to whether it was of the dispatch variety or whether it was set to the seo name of the page.



Without knowing what $current_url is, it’s hard to develop an ‘if’ statement related to it.

Well, I’ve never done the debug thing so this is something new. Pretty cool “feature”.



I don’t have a $current_url unfortunately. I do have a:


{$escaped_current_url}



It is set to:


"index.php%3Fdispatch%3Dcategories.vie..."



I’d assume that means:


index.php?dispatch=categories.view&category_id=1



I also have a:


{$seo_url}



It is set to:


Array (4)
page => "lingcod-jigs.html"
href => "/fin-cscart/lingcod-jigs.html"
query => ""
static => ""




Does any of that help?



Thanks again,



Brandon

So then with this info I would make your condition:


{if $config.current_url == "index.php?dispatch=categories.view&category_id=1" }
YOUR CODE HERE
{/if}




An alternative would be to use a PHP controller and to then just set a new smarty variable based on what the current page is. I.e.:


Registry::get('view')->assign('my_cat_name", fn_get_category_name($_REQUEST['category_id']) );




Then in your template simply match $my_cat_name against whatever you’e using to display your category names. When they match, apply your special styling (suggest it be a separate CSS class rather than an embedded style).

I’ve been messing around with the first option with no luck.



Where would the PHP controller go? Would I put it into the tpl file or somewhere else?



Thanks,



Brandon

Perhaps try:



{if $seo_url.page == "lingcod-jigs.html"}
YOUR CODE HERE
{else}
YOUR ELSE CODE
{/if}

...etc...




or



{if $escaped_current_url == "index.php%3Fdispatch%3Dcategories.view%26category_id=1"}
YOUR CODE HERE
{else}
YOUR ELSE CODE
{/if}

...etc...

The easiest way to go is just find out what the category id is for each menu, example and then just use:


{if $dispatch == 'index'}
css code here
{elseif $category.category_id == '5'}
css code here
{elseif $category.category_id == '6'}
css code here
{elseif $category.category_id == '7'}
css code here
{elseif $dispatch == 'auth'}
css code here
{/if}


Thanks - Sno

Well, I ended up using Adodric’s first suggestion and it works. It would be nice if there was an easier way of doing this though.



I tried both Tbirnseth’s and Sno’s ways, but neither worked. I’m not sure why Tbirneth’s didn’t, but I think Sno’s has something to do with the SEO addon, but I’m not sure.



This is what I ended up doing:


{if $controller == 'index'}
{include file="addons/my_changes/my_files/index.tpl"}
{elseif $seo_url.page == 'lingcod-jigs.html'}
{include file="addons/my_changes/my_files/categories/lingcod.tpl"}
{elseif $seo_url.page == 'lingcod-jigs/ling-jig-1.html'}
{include file="addons/my_changes/my_files/categories/lingcod.tpl"}
{elseif $seo_url.page == 'lingcod-jigs/ling-jig-2.html'}
{include file="addons/my_changes/my_files/categories/lingcod.tpl"}
{elseif $seo_url.page == 'lingcod-jigs/ling-jig-3.html'}
{include file="addons/my_changes/my_files/categories/lingcod.tpl"}
{elseif $seo_url.page == 'lingcod-jigs/ling-jig-4.html'}
{include file="addons/my_changes/my_files/categories/lingcod.tpl"}
{elseif $seo_url.page == 'lingcod-jigs/ling-jig-5.html'}
{include file="addons/my_changes/my_files/categories/lingcod.tpl"}
{elseif $seo_url.page == 'rockfish-jigs.html'}
{include file="addons/my_changes/my_files/categories/rockfish.tpl"}
{elseif $seo_url.page == 'rockfish-jigs/rock-jig-1.html'}
{include file="addons/my_changes/my_files/categories/rockfish.tpl"}
{elseif $seo_url.page == 'rockfish-jigs/rock-jig-2.html'}
{include file="addons/my_changes/my_files/categories/rockfish.tpl"}
{elseif $seo_url.page == 'rockfish-jigs/rock-jig-3.html'}
{include file="addons/my_changes/my_files/categories/rockfish.tpl"}
{elseif $seo_url.page == 'rockfish-jigs/rock-jig-4.html'}
{include file="addons/my_changes/my_files/categories/rockfish.tpl"}
{elseif $seo_url.page == 'rockfish-jigs/rock-jig-5.html'}
{include file="addons/my_changes/my_files/categories/rockfish.tpl"}
{elseif $seo_url.page == 'tuna-jigs.html'}
{include file="addons/my_changes/my_files/categories/tuna.tpl"}
{else}
{include file="addons/my_changes/my_files/blank_menu.tpl"}
{/if}




Because I only have a few categories, this works. One thing I would really like to improve is to make it so the products under the categories do the same thing as the category. As you can see, I’ve done that above. It would just be nice if it could be more dynamic or something.



The biggest thing about how I’ve done this is that this will only work as long as I only have a limited number of products and categories. If I start adding a lot more products I’ll have to figure out a different design.



Based upon my code above do you guys have any ideas on how I can clean this up and make it more dynamic or something?



Thanks for all your guys’ help.



Brandon

[quote name=‘brandonvd’]

Based upon my code above do you guys have any ideas on how I can clean this up and make it more dynamic or something?

Brandon[/QUOTE]



Try this:



{if $controller == 'index'}
{include file="addons/my_changes/my_files/index.tpl"}
{elseif $seo_url.page|stripos:"lingcod-jigs"}
{include file="addons/my_changes/my_files/categories/lingcod.tpl"}
{elseif $seo_url.page|stripos:"rockfish-jigs"}
{include file="addons/my_changes/my_files/categories/rockfish.tpl"}
{elseif $seo_url.page|stripos:"tuna-jigs"}
{include file="addons/my_changes/my_files/categories/tuna.tpl"}
{else}
{include file="addons/my_changes/my_files/blank_menu.tpl"}
{/if}

not sure why my code did’nt work, copied it right off a mod on my own site and changed the parameters, weird, anyways you did use my elseif idea :slight_smile:



This doesn’t achieve anything but when you have several different cats going to the same tpl which it looks like you are doing you can just do:


{elseif $seo_url.page == 'rockfish-jigs.html' || $seo_url.page == 'rockfish-jigs/rock-jig-1.html' || $seo_url.page == 'rockfish-jigs/rock-jig-2.html}
{include file="addons/my_changes/my_files/categories/rockfish.tpl"}




You may be able to code some elements into the manage category page to do this for you… I coded a Nested Blocks Addon which uses a similar method of using elements like checkboxes to display blocks within another block, the checkboxes enable me to position blocks anywhere on the product pages like central left, central middle, central right, central width ect…You can also use pull-down menus but I liked the checkboxes better. I’m thinking you can probably do the same thing to enable which tpl or css file to use if activated and you would be able to integrate the bulk edit feature to change the parameters more quickly.



What do you mean “so the products under the categories do the same thing as the category” can you provide a screenshot? Thanks - Sno

I tried Adodric’s idea, but it didn’t work. I haven’t really researched what stripos does so maybe there is a way.



I did the “Or” statements, but when I did it for one category it gave the same results for all the categories. If I only had on or statement it works, but when I do all five it doesn’t. Kind of weird.



I’d be curious to see what you already have Sno. Do you think you could send me the file(s)?



Also, as for the category question, you hit the nail on the head with your code you gave above, I just couldn’t figure out how to word it, sorry.



Anyways, I sure appreciate the time you guys have spent on this. Thanks.



Brandon

curious to know what “didn’t work” in using the php controller solution…

It’s a lot more robust in the long run because you’re only dependent on one variable in the template rather than a succession of if/then/else.



As you say, as you add categories and products you’ll have to modify your template for each. Same goes if you ever change SEO names…

The menu showed up fine, but the category that was chosen wasn’t orange. If you look at the first image in this post you will see that I have an orange background image for my menu. I just want the active category to be orange.



Hope that makes any sense, but I doubt it.



Brandon

[quote name=‘brandonvd’]I tried Adodric’s idea, but it didn’t work. I haven’t really researched what stripos does so maybe there is a way.[/QUOTE]



Hmm, try “stristr” in the place of all the “stripos” from my last post. You can also use “strstr” but “stristr” is the case insensitive version.

Thanks Adodric, that did it. I ended up using:



stristr



That makes my menu work great. I’m assuming that by using stristr it will make it so that anything that comes under a certain category will make the if statement true?



Basically any subcategory or product under lingcod-jigs for example will make my if statement true?



I sure wish I could explain myself better, but hopefully you get what I mean.



Anyways, thanks for your help I really appreciate it. I’m just glad to have this figured out. Now I can move on.



Brandon

[quote name=‘brandonvd’]

Basically any subcategory or product under lingcod-jigs for example will make my if statement true?

[/QUOTE]



Yes, exactly.



As long as you keep the SEO url the way it is by listing the parent category urls in it. If you did change your url formatting, as long as their is a common string in all of the related categories then you can always use it. You could just use “ling”, “rock”, or “tuna” also, which might slightly speed it up since it is less to check for.