Jump to:

3439 Posts in 1028 Topics by 869 members

Template Questions

SilverStripe Forums » Template Questions » Referencing Page control variables from within a dataobject?

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

Page: 1
Go to End
Author Topic: 1790 Views
  • Erin
    Avatar
    Community Member
    26 Posts

    Referencing Page control variables from within a dataobject? Link to this post

    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?

  • Erin
    Avatar
    Community Member
    26 Posts

    Re: Referencing Page control variables from within a dataobject? Link to this post

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

  • Martijn
    Avatar
    Community Member
    271 Posts
  • Erin
    Avatar
    Community Member
    26 Posts

    Re: Referencing Page control variables from within a dataobject? Link to this post

    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 %>

  • Erin
    Avatar
    Community Member
    26 Posts

    Re: Referencing Page control variables from within a dataobject? Link to this post

    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?

  • TotalNet
    Avatar
    Community Member
    181 Posts

    Re: Referencing Page control variables from within a dataobject? Link to this post

    DataObject::get will always return a DataObjectSet even if there's just one DO in it.
    Given your code you could use DataObject::get_by_id("Menu", $MealsID) or DataObject::get_one() if you wanted to add more to the query.
    hth

    1790 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.