【问题标题】:PHP-CSS-HTML applying a class to a tag through php with templatesPHP-CSS-HTML 通过带有模板的 php 将类应用于标签
【发布时间】:2011-01-05 23:25:10
【问题描述】:

你会猜到我是 php 新手。谢谢你的帮助。 我在标题 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>

我想用 PHP 函数调用包括。 问题是,我想调用这个 tpl,表明哪一个 li 将应用“选定”类,我该怎么做? 正如我告诉你的,对于任何有基本 PHP 经验的人来说都很容易。 谢谢。

【问题讨论】:

    标签: php class include task-parallel-library


    【解决方案1】:

    您可以将其基于在调用(非模板)逻辑中设置的变量。例如,在 smarty 中,您可以在 PHP 中执行此操作:

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

    然后在您的模板中,您可以检查该变量:

    <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>
    

    将 HTML 排除在您的业务逻辑 (PHP) 和显示逻辑 (tpl) 之外。

    【讨论】:

    • 最后,我使用了 {include file="tpl/menu.tpl" location="home"}
    【解决方案2】:

    为此创建一个函数:

    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>';
    }
    

    然后稍后再调用它:

    menu('d&oacute;nde estamos');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-08
      • 2011-02-03
      • 1970-01-01
      • 1970-01-01
      • 2019-11-25
      • 1970-01-01
      相关资源
      最近更新 更多