Hello Guys,
I'm looking for a reviews, corrections, comments or feedback for my first package: mohannadnaj/active-state. I don't know if it fit to be considered as a solution to this problem or not.
Quoting from the Readme:
Installation:
composer require mohannadnaj/active-state
Problem:
$current_page = 'contact';
<li class="<?= $current_page == 'home' ? 'active' : '' ?>"><a href="#home">Home</a></li>
<li class="<?= $current_page == 'about' ? 'active' : '' ?>"><a href="#about">About</a></li>
<li class="<?= $current_page == 'contact' ? 'active' : '' ?>"><a href="#contact">Contact</a></li>
Solution:
The package will load the methods set_active, get_active and is_active, set_active will set a given settings to a static variable inside the Mohannadnaj\Active class, this settings answer the questions: what is the active item? what should it return if the check is passed? or should it return boolean? and what is the key you want to attach all this settings to?
The above example can be translated to:
set_active('navbar', 'about');
<li class="<?= is_active('navbar', 'home'); ?>"><a href="#home">Home</a></li>
<li class="<?= is_active('navbar', 'about'); ?>"><a href="#about">About</a></li>
<li class="<?= is_active('navbar', 'contact'); ?>"><a href="#contact">Contact</a></li>
the set_active method will consider the first argument set_active('navbar' , ...) as a setter for the active element we want to catch later, that is 'about'.
usage examples:
set_active('navbar', 'index');
set_active('sidebar', 'info');
is_active('navbar','index'); // return "active"
is_active('navbar','about'); // return null
is_active('sidebar','info'); // return "active"
is_active('sidebar','warning'); // return null
get_active('navbar'); // return 'index';
get_active('sidebar'); // return 'info';
set_active('navbar3', 'register',['true'=>'is-active-css-class', 'false'=> 'not-active']);
is_active('navbar3', 'register'); // return 'is-active-css-class'
is_active('navbar3', 'login'); // return 'not-active'