tags:

views:

47

answers:

3

I'm currently using this for a nav list to highlight the active page:

<?= ($current_page == 'thema_list' ? 'class="on"' : '') ?>

Extending this to check another page would be:

<?= ($current_page == 'thema_list' || $current_page == 'thema_edit' ? 'class="on"' : '') ?>

Is there a way to avoid this repetition?

+2  A: 

For 2 values, this is probably the fastest method for the amount of time it takes PHP to process it, but you can use in_array if you have a lot of values:

<?= (in_array($current_page, array('thema_list', 'thema_edit')) ? 'class="on"' : '') ?>

Also check out this question regarding some speed tests related to in_array vs just checking for each value individually.

Darryl Hein
A: 

You could use an array with in_array()

$matches = array("thema_list", "thema_edit", "thema_other");
if (in_array($current_page, $matches)) {
    echo 'class="on"'
}
willoller
A: 

could you put them in an array and then use:

if(in_array($current_page, $array_values))
{
    echo 'class on';
}
Evernoob