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   2089 Views

Avatar
Hammy

Community Member, 49 Posts

9 February 2009 at 3:17pm

Edited: 09/02/2009 3:28pm

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

Community Member, 787 Posts

10 February 2009 at 3:12am

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

See :

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

Avatar
Hammy

Community Member, 49 Posts

10 February 2009 at 7:05am

That was exactly what I needed!

Awesome stuff Fuzz10 - thanks for your help.

Avatar
Capt. Morgan

Community Member, 30 Posts

7 March 2009 at 12:23am

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