Question

As you will guess i'm new to php. Thanks for your help. I have this piece of code in a header tpl:

<div class="menu">
            <ul>                                                                         
                <li class="selected"><a href="#">home</a></li>
                <li><a href="#">carta</a></li>
                <li><a href="#">delicias artesanas</a></li>
                <li><a href="#">contacto</a></li>
                <li><a href="#">d&oacute;nde estamos</a></li>
            </ul>
        </div>

which I'd like to call with PHP function include. The thing is, I want to call this tpl, indicating which one of the li's will have the "selected" class applied, how can I do that? As I told you, easy peasy for anyone with basic PHP experience. THANK YOU.

Was it helpful?

Solution

You can base it off of a variable that is set in your calling (non-template) logic. For instance, in smarty you could do this in the PHP:

// Determine which section user is in and pass to tpl
$smarty->assign('location', 'home');

Then in your template you can just check that variable:

<li{if $location eq 'home'} class="selected"{/if}><a href="#">home</a></li>
<li{if $location eq 'carta'} class="selected"{/if}><a href="#">carta</a></li>

The keeps HTML out of your biz logic (PHP) and in your display logic (tpl).

OTHER TIPS

make a function for that:

function menu($selected = null) {
    echo '<ul class="menu">';
    $options = array('home', 'carta', 'delicias artesanas', 'contact', 'd&oacute;nde estamos');
    foreach ($options as $li) {
        echo '<li' . ($li == $selected ? ' class="selected"' : '').'><a href="#">'.$li.'</a></li>'."\n";
    }
    echo '</ul>';
}

and then just call it later:

menu('d&oacute;nde estamos');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top