display_helper : makes links active according to current controller - very tiny piece of code |
[eluser]benoa[/eluser]
Hello, I wanted to share this tiny helper that I called display_helper.php, that contains only one function at the moment, that is doing one thing : return a css class suffix (by default "-active") when the current controller (uri->segment(1) by default) is the one provided as a parameter. Here's the code : Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); How to use : Code: <div id="nav"> It is a very tiny little piece of nothing code but almost every website needs it so I thought it could be good for some people around ![]() If you have any comments about it, I'm all ears! Have a nice day! B
[eluser]trice22[/eluser]
Hello and thanks for the contribution. From my perspective I'd rather go with a body ID/class, though for this purpose. http://css-tricks.com/id-your-body-for-g...ecificity/ —trice
[eluser]benoa[/eluser]
Thank you Trice, that's a smarter approach I wasn't aware of. I'll embrace it in my next project ![]()
[eluser]Philipp Gérard[/eluser]
For all those considering to use this implementation: Don't. It's inflexible as hell, better write yourself a helper that uses anchor() and dynamically adds a subclass if the controller/action is active. I'll post my implementation once I've written it.
[eluser]Philipp Gérard[/eluser]
Hey benoa, I didn't have the chance to write the function properly, but the following (from scratch) works and surely shows what I meant. However, I am not 100% sure if it works properly in every environment (w/wo .htaccess etc.), needs more testing. Any feedback appreciated! Best wishes, Philipp Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); Usage: Code: <?= active_anchor('/user/login', 'Login'); ?> Returns: Code: <a href="http://yourdomain.com/user/login.html" class="active">Login</a>
[eluser]benoa[/eluser]
It looks like a good solution. I have to try it when I got a chance. It feels like a lot of code though, for this small functionality. I'd rather use the body class instead.
[eluser]Philipp Gérard[/eluser]
Well, sum up the useless function calls in your views and you'll end up with much more unwanted code ![]() |
Welcome Guest, Not a member yet? Register Sign In |