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

How to access values outside of control


Reply

4 Posts   2048 Views

Avatar
Hammy

9 February 2009 at 3:17pm (Last edited: 9 February 2009 3:28pm), Community Member, 49 Posts

How do I get the value $Link to work inside a control?

Template Section.ss:

   $Link
   <ul>
      <% control FilterColourList %>
         <li><a href="$Link/$ColourName">$ColourName</a></li>
      <% end_control %>
   </ul>

The first $Link in the template above works fine but when it is inside the control it does not work - it is blank (I assume this is because it is not a field named Link in FilterColourList/FilterColourType.php).

Is there a way to access the field $Link from Section inside the control of FilterColourList?

Just in case - here is the php code aswell:

Section.php

<?php

class Section extends Page {
   
   public static $db = array(
      'Tagline' => 'Text'
   );
   
   public static $has_many = array(
      'Detail' => 'Detail'
   );
   
   function getCMSFields() {
      
      $fields = parent::getCMSFields();
      
      $fields->addFieldToTab('Root.Content.Main', new TextareaField('Tagline'), 'Content');      
      $fields->removeFieldFromTab('Root.Content.Main', "MenuTitle");
      $fields->removeFieldFromTab('Root.Content.Main', "Content");
      
      return $fields;
   }
}

class Section_Controller extends Page_Controller {
   
   function FilterColourList() {
      $dataObj = 'FilterColourType';
      $filter = '';
      $sort = 'ColourName ASC';
      $join = '';
      $limit = '';
      $ColourList = DataObject::get($dataObj, $filter, $sort, $join, $limit);
      return ($ColourList) ? $ColourList : false;
   }
}

?>

FilterColourType.php:

<?php

class FilterColourType extends DataObject {

   static $db = array(
      'ColourName' => 'Text'
   );
   
   public static $belongs_many_many = array(
      'Detail' => 'Detail'
   );

   function getCMSFields_forPopup() {
      $fields = new FieldSet();
      $fields->push( new TextField( 'ColourName', 'Colour' ) );
      return $fields;
   }
}

?>

Avatar
Fuzz10

10 February 2009 at 3:12am Community Member, 787 Posts

To access the "parent-context" .. Use $Top ...

See :

http://doc.silverstripe.com/doku.php?id=built-in-page-controls&s=page%20controls

Avatar
Hammy

10 February 2009 at 7:05am Community Member, 49 Posts

That was exactly what I needed!

Awesome stuff Fuzz10 - thanks for your help.

Avatar
Capt. Morgan

7 March 2009 at 12:23am Community Member, 30 Posts

Aww, super. How I have been looking for this. Deserves a bump not to fall out of the first page.