Support the ongoing development of Laravel.io →
Configuration Views Packages
Last updated 2 years ago.
0

Have you tried View::share('key','value') ?

Last updated 2 years ago.
0

usm4n said:

Have you tried View::share('key','value') ?

Hi usmn4n,

thank you for response.

No, I didn't try it that way, I am new in laravel. Still learning. But believe that won't work. Because I want to set Current URL. Will give it a try. In my case I want to set menu item active, and I am doing it like this :

<li>{% if('about' == current_url)%} class="active"{% endif %}</li>

I am using twig and I am trying to do it via Twig. Want to learn how can to add a macro, function in Laravel 4.

I have done it via config file, but that is not the right way.

Last updated 2 years ago.
0

OK, normaly to set an active class i do it like this:

<li class="{{ (strpos(URL::current(), URL::to('about'))!== false) ? 'active' : '' }}">
Last updated 2 years ago.
0

I have a series of small helper function that does this for me:

function active_url_strict($path, $class = 'active')
{
    return Request::is($path) ? $class : '';
}
function active_url($path, $class = 'active')
{
    return (strpos(URL::current(), $path) !== false) ? $class : '';
}

Example usage:

<li class=" {{ active_url('about') }}">
Last updated 2 years ago.
0

Hey, you can also use my Twig bridge: https://github.com/barryvdh/laravel-twigbridge

You can get the Twig instance with app('twig')

$twig = app('twig');
$twig->addGlobal('current_url', Request::path());

You can also use the config file to add a helper function, either by callback or just the function name (or method@Class). Or you can add the Request facade to the globals, so you can use Request.path(), URL.current() or Request.is('mylink') in your twig templates.

'functions' => array(
    'active_url',   // Use this if the function already exists
    'active_url_cb' => function($path, $class = 'active'){
            return (strpos(URL::current(), $path) !== false) ? $class : '';
        },
),
'facades' => array(
    'Request',
    'URL',
)
Last updated 2 years ago.
0

Hi guys, thank you. Eventually I have switched back to blade, and I didn't have any more problems. Hope blade will become in the future good like twig is.

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.