Autonav - can you edit the default view.php so it puts a class on the first link?
Permalink
I want to style the first link in my autonav, so I need something like the code in the header_menu template to put a class on the first link. Can I easily edit the view.php template (which is otherwise working nicely) to get this functionality, and if so how? I don't know enough php so am likely to mess it up if I try without help!!
(I tried just applying the header_menu template but it messed the nav up completely, not sure why, looked like my 2nd level sub-nav links were no longer appearing as nested lists or something.)
If someone could tell me which lines I need to put into view.php - the $isFirst bit I assume, but perhaps it's not that simple? - that would be brilliant, thank you.
(I tried just applying the header_menu template but it messed the nav up completely, not sure why, looked like my 2nd level sub-nav links were no longer appearing as nested lists or something.)
If someone could tell me which lines I need to put into view.php - the $isFirst bit I assume, but perhaps it's not that simple? - that would be brilliant, thank you.
Since you are talking about the Autonav, I am assuming you are talking about editing the view.php file withing this directory.
If that is the case, adding a class to the first, and only the first item, looks fairly quick and painless to accomplish.
I am not sure about the "isFirst" that you mention, but this could be done with simple counter.
1. create a counter variable right before the foreach loop that builds the nav. something like:
2. the next thing to do would be to check this value right before the code starts building the nav elements. the code you're looking for looks like this:
3. there is a set of if else statements that build the nav here. you need to check your counter variable to make sure that it's value is 0 at this point. if it is, you will build the nav element so it has your class. something like:
That, or something like it, will create the same link with an additional class the first time through the loop only.
Keep in mind there are other ways to accomplish, maybe even with built in functions of C5, but this is an quick and easy way to get your desired results. Also keep in mind that I believe it is best practice when working with C5 to not change code in the concrete directory and to actually add your changed code the blocks directory.
If that is the case, adding a class to the first, and only the first item, looks fairly quick and painless to accomplish.
I am not sure about the "isFirst" that you mention, but this could be done with simple counter.
1. create a counter variable right before the foreach loop that builds the nav. something like:
$counter = 0;
2. the next thing to do would be to check this value right before the code starts building the nav elements. the code you're looking for looks like this:
echo('<li class="nav-selected nav-path-selected"><a class="nav-selected nav-path-selected" ' . $target . ' href="' . $pageLink . '">' . $ni->getName() . '</a>');
3. there is a set of if else statements that build the nav here. you need to check your counter variable to make sure that it's value is 0 at this point. if it is, you will build the nav element so it has your class. something like:
if ($counter == 0) { echo('<li class="nav-selected nav-path-selected"><a class="nav-selected nav-path-selected YOURCLASS" ' . $target . ' href="' . $pageLink . '">' . $ni->getName() . '</a>'); } else { echo('<li class="nav-selected nav-path-selected"><a class="nav-selected nav-path-selected" ' . $target . ' href="' . $pageLink . '">' . $ni->getName() . '</a>'); }
That, or something like it, will create the same link with an additional class the first time through the loop only.
Keep in mind there are other ways to accomplish, maybe even with built in functions of C5, but this is an quick and easy way to get your desired results. Also keep in mind that I believe it is best practice when working with C5 to not change code in the concrete directory and to actually add your changed code the blocks directory.
It is not quite as simple as you're describing -- if you look at the default view.php template closely, you'll see that there are three different possibilities for how the menu item <li> could be outputted, not just one like your code suggests. This is one of the many (many) reasons the built-in template is terrible to code against.
Obviously I'm biased, but I agree with @mkly that you should use the custom template he links to. For a full explanation of how to use it, see:
http://c5blog.jordanlev.com/blog/2011/12/customizing-the-autonav-te...
Obviously I'm biased, but I agree with @mkly that you should use the custom template he links to. For a full explanation of how to use it, see:
http://c5blog.jordanlev.com/blog/2011/12/customizing-the-autonav-te...
Right, I was just trying to simplify my answer.
It just seems to me that creating a new template for this overkill when essentially the user wants to use what is there, just add a class to the first nav item.
There are only three options (if, else statements) for building out menu items so the user could check in those three items if this is the first run through the loop and execute according.
Like I said, there is definitely more than one way to go about this and for me, the simplest way of adding a unique class to the first nav item, is to do exactly what I did. maybe just a little more detail was needed.
Anyway, I hope the poster gets what they need from this discussion. Cheers.
It just seems to me that creating a new template for this overkill when essentially the user wants to use what is there, just add a class to the first nav item.
There are only three options (if, else statements) for building out menu items so the user could check in those three items if this is the first run through the loop and execute according.
Like I said, there is definitely more than one way to go about this and for me, the simplest way of adding a unique class to the first nav item, is to do exactly what I did. maybe just a little more detail was needed.
Anyway, I hope the poster gets what they need from this discussion. Cheers.
Thanks guys, I really appreciate all your answers. And more importantly you've helped me fix my problem!! But I think Jordan's template probably is the way forward, for future reference - did I read correctly [EDIT - on Jordan's blog, that is] that it should make it into the core soon?
nope.
Yup (probably) -- Andrew said they'd try to include it in 5.3.3
someday I'll find your optimism.
@mkly
And someday I'll find your pessimism :)
Really, what horrible has happened with you and the core team (although on some level I can understand your frustration)? ;)
And someday I'll find your pessimism :)
Really, what horrible has happened with you and the core team (although on some level I can understand your frustration)? ;)
@Mainio: I can't speak for @mkly... but I get the sense that we share some level of frustration in that other open source projects and communities we've been involved in are more collaborative in nature than C5.
I understand where the core team is coming from and what their reasoning is on this... but there will always be some friction when smart and opinionated people get together and have slightly differing agendas.
I understand where the core team is coming from and what their reasoning is on this... but there will always be some friction when smart and opinionated people get together and have slightly differing agendas.
@Mainio: I can't speak for @jordanlev... but I get the sense that we share some level of frustration in that other open source projects and communities we've been involved in are more collaborative in nature than C5.
Sure, and the reasons behind has been discussed over and over again...
@mkly: how meta
https://github.com/jordanlev/c5_clean_block_templates...