Laravel中Twitter引导导航的自动活动类 [英] Automatic Active Class For Twitter Bootstrap Navigation in Laravel

查看:72
本文介绍了Laravel中Twitter引导导航的自动活动类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

和大多数人一样,我正在使用Twitter Bootstrap作为我目前在Laravel中开发的网站.到目前为止,我很喜欢将Laravel用作等同于Rails的PHP,但是我想知道是否有更好的方法来制作导航栏.我试图确保我的导航栏li标记自动获取活动的类,所以我要这样做:

Like most people, I'm using Twitter Bootstrap for the site I'm currently developing in Laravel. So far I'm enjoying using Laravel as a PHP equivalent to Rails, but I'm wondering if there's a better way to do the navigation bars. I'm attempting to make sure that my navbar li tags get the active class automatically, so I have this:

<ul class="nav nav-pills">
@if(URL::current() . "/" == URL::route('home'))
    <li class="active">
@else
    <li>
@endif
    <a href="{{ route('home') }}">Home</a>
</li>
@if(URL::current() == URL::route('about'))
    <li class="active">
@else
    <li>
@endif
    <a href="{{ route('about') }}">About</a>
</li>
</ul>

暂时还好.但是,当我有了菜单和更多的导航功能后,它将很难调试和处理.

This'll be fine, for now. But when I've got menus, and more navigations it'll end up looking awful to debug and deal with.

Laravel是否有类似于Gem的宝石 Tabulous 或处理导航中通常更好的方法Laravel?

Is there anything akin to the Gem Tabulous for Laravel or a generally nicer way to deal with navigation in Laravel?

推荐答案

看看 Bootstrapper 软件包,它有很多可以帮助您使用Twitter Bootstrap的工具.在您的示例中,您可以这样构建导航:

Take a look at the Bootstrapper package, it has lots of tools to help you with Twitter Bootstrap. In your example, you could build the navigation like this:

Navigation::pills(array(
    array(
        'label'  => 'Home',
        'url'    => URL::route('home'),
    ),
    array(
        'label'  => 'About',
        'url'    => URL::route('about'),
    )
));

还有一种速记方法,不太冗长,我不太喜欢,但是可以使用:

There's also the shorthand method, less verbose, which I don't particularly like, but can be used:

Navigation::pills(
    Navigation::links(array(
        array('Home', URL::route('home')),
        array('About', URL::route('about')),
    ))
);

请务必注意,在两个示例中, Bootstrapper都会自动处理active,将当前请求URL与传递给该项目的URL进行比较.但是,如果您希望为此指定一个不同的条件,则只需为简短方法传递active值或数组中的第三个值即可.示例:

It's important to note that in both examples, Bootstrapper takes care of the active class automatically, comparing the current request URL against the one passed to the item. If, however, you wish to specify a different condition for this to apply, simply pass an active value, or a third value in the array, for the shorter method. Example:

Navigation::pills(array(
    array(
        'label'  => 'Home',
        'url'    => URL::route('home'),
    ),
    array(
        'label'  => 'About',
        'url'    => URL::route('about'),
        'active' => true,
    )
));

Navigation::pills(
    Navigation::links(array(
        array('Home', URL::route('home')),
        array('About', URL::route('about'), true),
    ))
);

这篇关于Laravel中Twitter引导导航的自动活动类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆