Akelos Framework v1 forum archive. This forum is no longer maintained. To report bugs please visit https://github.com/akelos/akelos/issues
    • CommentAuthorkkrzyzak
     

    Hi. I would like to pass class name for link via ruby-like syntax link_to('Foo', :controller=>'bar', :action=>'test', :class=>'current') But this snippet don't works (pass class as part of route). So, first question is how to pass class name to link. Second is, how to pass class name, only for "current" link( curent is link to action, which is actual rendered)

    •  
      CommentAuthorbermi
     

    kkrzyzak, the expected behaviour of the Sintags you mention is

    http://example.com/bar/test/?class=current
    

    Could you clarify your question?

    • CommentAuthorkkrzyzak
     

    well- I would like to have <a href="http://example.com/bar/test" class="current">Foo</a> - that's my first question. Second is, how to add class="current" only if rendered action equals :action from link_to function

    •  
      CommentAuthorbermi
     

    Oops! I misread, the output I mentioned is for url_for. Sorry.

    In order to get the link with the class attribute, :class => 'current' needs to be the last parameter in the helper.

    <%= link_to( 'Foo', {:controller=>'bar', :action=>'test'}, {:class=>'current'}) %>
    

    or

    <%= link_to 'Foo', {:controller=>'bar', :action=>'test'}, :class=>'current' %>
    

    To condition the class you'll need to code a helper function like

    function link_to_action($name, $url_options, $html_options)
    {
        if($this->_controller->getActionName() == AkInflector::underscore($name)){
            $html_options['class'] = empty($html_options['class']) ? 'current' : $html_options['class'].' current';
        }
        $this->_controller->url_helper->link_to($this->t($name), $url_options, $html_options);
    }
    

    which you can insert into a layout helper and include it on you application controller.