Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Template Questions /

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

Help with side menu links


Go to End


2 Posts   2638 Views

Avatar
nick10

Community Member, 1 Post

19 August 2009 at 11:40am

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?

Avatar
PolarAhmad

Community Member, 1 Post

22 August 2009 at 2:35am

Edited: 22/08/2009 2:39am

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.