Code snippets for symfony 1.x


Refine Tags

Snippets tagged "linkto"

create styled navigation for current view / highlight active link v.2

I have rewritten my original link_to_styled helper to accommodate more common scenarios. This helper will append a class name to the link if the current route matches the one present in the link.

You can specify within the $style_options parameter to match only the module name rather than the entire route. This is useful for top-level navigation links that correspond to your modules.

You can also specify a tag that will encapsulate your link and receive the class name rather than the <a> tag if the active-state requirements are met.

Additionally, any existing class names given to your links are maintained rather than overwritten with the active class name.

As others have suggested, I'm sure it could still be improved to allow for partial route matching, perhaps excluding query parameters.

NOTE: Thanks to Fabian Lange's suggestion, I reordered the parameters so that the style parameters come before the less-used html options.


in lib/helper/StyleHelper.php:

 * Automatically adds a class name to the link if the link route matches
 * the current module or route.
 * <b>Style Options:</b>
 * - 'class' - the class name to append to the link, defaults to 'current'
 * - 'module_only' - append class name if the current module and link module match
 * - 'tag' - an html to encapsulate the link, which will receive the class name rather than the <a> tag
 * <b>Examples:</b>
 * <code>
 *   echo link_to_styled('Events', 'event/list', 'class=active', 'id=event_link', );
 *   if the current route is 'event/list', then:
 *     => <a href="path/to/event/list/action" id="event_link" class="active">Events</a>
 *   echo link_to_styled('Events', 'event/list', 'class=active module_only=true tag=li', 'id=event_link');
 *   if the current module matches the module present in the route, then:
 *     => <li class="active"><a href="path/to/event/list/action" id="event_link">Events</a></li>
 * </code>
 * @param  string name of the link, i.e. string to appear between the <a> tags
 * @param  string 'module/action' or '@rule' of the action
 * @param  array additional style options
 * @param  array additional HTML compliant <a> tag parameters
 * @return string XHTML compliant <a href> tag
 * @see    link_to
function link_to_styled($text, $route = '', $style_options = '', $options = '') {
    static $context, $current_module_name;
    $options = _parse_attributes($options);
    $style_options = _parse_attributes($style_options);
    if (!isset($style_options['class']))
        $style_options['class'] = 'current';
    if (isset($style_options['module_only']))
        if (!isset($context))
            $context = sfContext::getInstance();
        if (!isset($current_module_name))
        $current_module_name = $context->getModuleName();
    list($current_route_name, $params) = $context->getController()->convertUrlStringToParameters($route);
        $is_current = $params['module'] == $current_module_name;
        $current_route = sfRouting::getInstance()->getCurrentInternalUri();
        $is_current = $current_route == $route;
    if (isset($style_options['tag']))
        $tag_options = (true === $is_current) ? array('class'=>$style_options['class']) : array();
        $return_string = content_tag($style_options['tag'], link_to($text, $route, $options), $tag_options);
        if (true === $is_current)
            $options['class'] = isset($options['class']) ? $options['class'] . ' ' . $style_options['class'] : $style_options['class'];
        $return_string = link_to($text, $route, $options);
    return $return_string;

and in the template:

<?php use_helper('Style') ?>
<ul id="navlist">
<!-- highlight this link if we are within the "events" module -->
<li><?php echo link_to_styled('My Events', 'events/list', 'module_only=true') ?></li>
<!-- or you could add the style to the li tag -->
<?php echo link_to_styled('Event List', 'events/list', 'tag=li class=active') ?></li>
by scott meves on 2007-02-24, tagged css  helper  linkto  navigation