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

access the variables from a parent out of an <% control ?


Reply

4 Posts   1009 Views

Avatar
benni91

29 July 2011 at 7:16am (Last edited: 29 July 2011 7:18am), Community Member, 72 Posts

Hey @ all,

If got a problem.

i build this page

Reference.ss

<% control Screens %>
   <div class="refimg">
      <a href="$Screen.url" rel="lightbox-$Title" title="$Description">$Screen.setWidth(430)</a>
   </div>
<% end_control %>
<div class="clear"></div>

Reference.php

<?php
class ReferencePage extends Page {
      
   static $allowed_children = 'none';
   static $default_parrent = 'referenzen';
   static $can_be_root = false;

   public static $db = array(
      'Date' => 'Date',
      'Category' => 'Text',
      'Site' => 'Text'
   );

   public static $has_one = array(
      'Client' => 'ClientObject',
      'Preview' => 'Image'
   );
   
   public static $has_many = array(
      'Screens' => 'ScreenObject'
   );

   function getCMSFields() {
      
      $fields = parent::getCMSFields();
      
      $fields->addFieldToTab('Root.Content.Main', $date = new DateField('Date', 'Release Date'), 'Content');
      $date->setConfig('showcalendar', true);
      
      $fields->addFieldToTab('Root.Content.Main', new TextField('Category','Web/Print/Hosting etc.'), 'Content');
      $fields->addFieldToTab('Root.Content.Main', new TextField('Site','Link zur Online Referenz (Website wenn online)'), 'Content');
      
      $clients = DataObject::get('ClientObject')->toDropDownMap('ID', 'Name');
      $fields->addFieldToTab('Root.Content.Main', new DropdownField('ClientID', 'Client', $clients, $this), 'Content');
      
      $fields->addFieldToTab('Root.Content.Screens', new ImageDataObjectManager($this, 'Screens', 'ScreenObject', 'Screen', array('Description' => 'Beschreibung'), 'getCMSFields_forPopup'));
      
      $fields->addFieldToTab("Root.Content.Preview", new ImageField('Preview', 'Preview Image 276px x 140px'));
      
      return $fields;
      
   }

}
class ReferencePage_Controller extends Page_Controller {

}

?>

and

ScreenObject

<?php
class ScreenObject extends DataObject {

   public static $db = array(
      'Description' => 'Text'
   );

   public static $has_one = array(
      'Screen' => 'Image',
      'ReferencePage' => 'ReferencePage'
   );

   public function getCMSFields_forPopup() {

return new FieldSet(
new TextField('Description', 'Beschreibung'),
new ImageField('Screen', 'Screenshot')
);

}
   
}

?>

I'm trying to get the page title of my ReferencePage
(<a href="$Screen.url" rel="lightbox-$Title" title="$Description">$Screen.setWidth(430)</a>)

But i just get the image title.

if i try it like this

<% control Screens %>
   <div class="refimg">
      <a href="$Screen.url" rel="lightbox-<% control Parent %>$Title<% end_control %>" title="$Description">$Screen.setWidth(430)</a>
   </div>
<% end_control %>
<div class="clear"></div>

it also didn't work :/

any solutions?

Thanks Benni

Avatar
zenmonkey

29 July 2011 at 7:36am Community Member, 528 Posts

You can use $Parent.Title or $Top.Title to temporally escape out of the control.
http://doc.silverstripe.org/sapphire/en/reference/built-in-page-controls#top

Avatar
benni91

29 July 2011 at 8:05am Community Member, 72 Posts

Thx, $Top.Title works :)
$Parent.Title didn't

Avatar
geoffmcqueen

8 May 2014 at 5:55am Community Member, 11 Posts

The syntax now is "$Up.Title" instead of $Parent. I'm sure it made sense to someone to change something that already worked and lead to confusion and re-writing of templates...