Jump to:

3401 Posts in 979 Topics by 855 members

Template Questions

SilverStripe Forums » Template Questions » Help with side menu links

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 1651 Views
  • nick10
    Avatar
    Community Member
    1 Post

    Help with side menu links Link to this post

    I am in need of help in creating a side navigation bar in SilverStripe that would look somewhat similar to the one on this page: http://www.bgcg.org/index.php?PID=1&cid=1.

    so the goal is to have the top menu show like this
     

    red - home
    green - about us
    blue - contact
    red - news
    green - product
    .
    .
    .
    (continuing down, alternating red, green, and blue icons)

    we have this function (based on the one here:

    http://silverstripe.org/template-questions/show/265003?start=0):

    which basically gets us the data that we want into a variable.

     

    public function MyMenu(){

       $items = $this->Menu(1);

       $numItems = $items->Count();

       $rows = array();

       $it = $items->getIterator();
     

       for($i = 0; $i < $numItems; ++$i){

                $row = new DataObjectSet();

                $num =  $it->key();

                switch($num%3) {

                            case 0:

                                        $classColor = "spinner_red";
                            break;
                            case 1:
                                        $classColor = "spinner_green";
                            break;
                            case 2:
                                        $classColor = "spinner_blue";
                          break;
                }
     
                $row->push($it->current(), $it->key());
                $it->next();
     
                $rows[] = array('Row' => $row, 'Color' => $classColor);
       }
     
       return new DataObjectSet($rows);
             
    }
     
    and in our template we have

    <% control MyMenu() %>
       <% control Row %>
                <tr>
                            <td><a href="$Link" class="$Color">$MenuTitle</a><br/></td>
                </tr>
      <% end_control %>
    <% end_control %>

    I already have the classes set up in the CSS. The problem is that I need to somehow get access to that $Color variable in the php script in the template. How would I do that?

  • PolarAhmad
    Avatar
    Community Member
    1 Post

    Re: Help with side menu links Link to this post

    I think I've found a solution for you.

    In the PHP file change your function to this:

    public function MyMenu($level){

    $items = $this->Menu($level);
    $numItems = $items->Count();
    $MenuItems = array();
    $it = $items->getIterator();

    for($i = 0; $i < $numItems; ++$i){
          $MenuItem = new DataObjectSet();
          $menu = new CustomMenu();

          $num = $it->key();
          switch($num%3) {
              case 0:
                $classColor = "spinner_red";
             break;
             case 1:
                $classColor = "spinner_green";
             break;
             case 2:
                $classColor = "spinner_blue";
             break;
          }
          

          $menu->Title = $it->current()->MenuTitle;
          $menu->LinkURL = $it->current()->URLSegment;
          $menu->Color = $classColor;
          
          $MenuItem->push($menu);
    $it->next();
          
          $MenuItems[] = array('MenuItem' => $MenuItem);
    }

    return new DataObjectSet($MenuItems);
    }

    But to use this function you need to create a custum class (which you should place before the Controller class):

    class CustomMenu extends ViewableData{
       public $Title, $LinkURL, $Color;
       
    }

    and then you can use this in your template:

          <% control MyMenu(1) %>
             <% control MenuItem %>
                <tr>
                   <td><a href="$LinkURL" class="$Color" >$Title</a><br /></td>
                </tr>
             <% end_control %>
          <% end_control %>

    Hope that helps.

    1651 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.