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.

Template Questions

Referencing Page control variables from within a dataobject?


Reply

6 Posts   1803 Views

Avatar
Erin

2 September 2010 at 4:41pm (Last edited: 8 September 2010 6:19am), Community Member, 26 Posts

I'm trying to build a 2-level nav where the 2nd level link is comprised of the URL from the 1st level and an anchor identifier placed by a dataobject.
In short,

<% control Menu(1) %>
   <ul class="$URLSegment $LinkOrSection">
<li><a href="$Link" class="$Title" title="$Title">$Title<!--[if IE 7]><!--></a><!--<![endif]-->
   <div class="link_sub">
       <ul class="sub">
               <% if Meals %>
         <% control Meals %>
         <% control ItemCategories %>
          <li><a href="$Link#$Name" title="$Name">$Name</a></li>
          <% end_control %>
                  <% end_control %>


I can pull $Name from ItemCategories, but can't find a way to grab $Link from the Menu(1) controller. Do I need to write a custom method just to pull $Link from the page related to the dataobject?

Avatar
Erin

8 September 2010 at 6:19am Community Member, 26 Posts

Changed the topic to one that I think might make more sense.

Avatar
Martijn

8 September 2010 at 8:08am Community Member, 271 Posts

Try $Top.Link

Avatar
Erin

10 September 2010 at 2:46am Community Member, 26 Posts

Unfortunately, this just gives every 2nd level link a reference to the current page, not the parent page in the menu.

<% control Menu(1) %>
<% if LinkOrSection="link" %>
   <ul class="$URLSegment $LinkOrSection">
<li><a href="$Link" class="$Title" title="$Title">$Title<!--[if IE 7]><!--></a><!--<![endif]-->
<!--[if lte IE 6]><table><tr><td><![endif]-->            
   <div class="link_sub">
   <ul class="sub">
         <% if Meals %>
<% control Meals %>
<% control ItemCategories %>
<li><a href="$Top.Link" title="$Name">$Name</a></li>
<% end_control %>
         <% end_control %>

Avatar
Erin

17 September 2010 at 2:51pm Community Member, 26 Posts

I think I'm getting closer. They key, near as I can see it, is to write a function for the ItemCategory class that will find the ID of the parent DataObject Meal (Menu), then find the parent Page (Menu), then - from that - get the URLSegment. This is as far as I've gotten:

   function MenuItemLink(){
      $MealsID = $this->MealsID;
      $Menu = DataObject::get('Menu', "MealsID = '{$MealsID}'");
}


But $Menu is returned as a DataObjectSet. Shouldn't it just be 1 DataObject?

Avatar
TotalNet

17 September 2010 at 3:40pm Community Member, 181 Posts

DataObject::get will always return a DataObjectSet even if there's just one DO in it.
Given your code you could use [url=http://api.silverstripe.org/trunk/sapphire/model/DataObject.html#methodget_by_id]DataObject::get_by_id("Menu", $MealsID)[/url] or DataObject::get_one() if you wanted to add more to the query.
hth