![]() |
display_helper : makes links active according to current controller - very tiny piece of code - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22) +--- Thread: display_helper : makes links active according to current controller - very tiny piece of code (/showthread.php?tid=24701) |
display_helper : makes links active according to current controller - very tiny piece of code - El Forum - 11-17-2009 [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 display_helper : makes links active according to current controller - very tiny piece of code - El Forum - 11-17-2009 [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-greater-css-control-and-specificity/ —trice display_helper : makes links active according to current controller - very tiny piece of code - El Forum - 11-17-2009 [eluser]benoa[/eluser] Thank you Trice, that's a smarter approach I wasn't aware of. I'll embrace it in my next project ![]() display_helper : makes links active according to current controller - very tiny piece of code - El Forum - 11-27-2009 [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. display_helper : makes links active according to current controller - very tiny piece of code - El Forum - 11-27-2009 [eluser]jeanv[/eluser] very useful! Thanks for sharing display_helper : makes links active according to current controller - very tiny piece of code - El Forum - 11-27-2009 [eluser]benoa[/eluser] @jeanv : you're welcome ![]() @Philipp Gérard : your idea sounds interesting... Any code yet? display_helper : makes links active according to current controller - very tiny piece of code - El Forum - 11-27-2009 [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> display_helper : makes links active according to current controller - very tiny piece of code - El Forum - 11-27-2009 [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. display_helper : makes links active according to current controller - very tiny piece of code - El Forum - 11-28-2009 [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 ![]() |