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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Template Questions /

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

Using Variable in < % control Page() %> ??


Go to End


3 Posts   3613 Views

Avatar
SalvaStripe

Community Member, 89 Posts

17 December 2008 at 2:32am

hey Guys,

i want to create a new pagetype, that loads just the $Content of another page.
so, the new pagetype has a new text DB field "showContent"

<?php
class CreatePopup extends SiteTree {
	static $db = array(
	   'showContent' => 'Text'
	);
	static $has_one = array(
   );

	function getCMSFields() {
		$fields = parent::getCMSFields();
		$fields->addFieldToTab('Root.Content.Main', new TextField('showContent'), 'Content');
		return $fields;
	}
}

class CreatePopup_Controller extends ContentController {
	function init() {
		parent::init();
		
		Requirements::themedCSS("layout");
		Requirements::themedCSS("typography");
		Requirements::themedCSS("form");
	}
}
?>

in my "CreatePopup.ss" File i can write

Hello i am loading Content from http://www.yoursite.com/$showContent/

Then there is no error and the Content of "$showContent" is shown.
But now, i want to use this code..

<% control Page($showContent/) %>$Content<% end_control %>

So this PageType shoud show just the $Content of another Page. ("$showContent" contains the URLSegment of the page what should be loaded). But when i use this Variable in Control Page, i get this ERROR:

Parse error: syntax error, unexpected '}' in /tmp/silverstripe-cache-srv-www-vhosts-domain.com-httpdocs/.cache.srv.www.vhosts.domain.com.httpdocs.themes.domain.templates.CreatePopup.ss on line 53 

Can someone Help me?

p.s.:
i tried to write a function in CreatePopup.php, but i'm not going on with it.. just many errors..

	function showControlContent($showContent) {
		$existingCont = DataObject::get_one("SiteTree", "URLSegment = '$showContent'");
		if($existingCont) {
			$makeControlPageContent = "<% control Page(".$showContent.") %>$Title<% end_control %>";
		return $makeControlPageContent;
		}
	}

Avatar
SalvaStripe

Community Member, 89 Posts

17 December 2008 at 2:58am

Edited: 17/12/2008 4:28am

hey boys ;)

I just put the "CreatePopup" pagetyp as childs of the pages..
so the "CreatePopup" pagetypes just make

$Parent.Content

so i dont need variable, dont need extra DB field and its better sortet i think.. (that content what should be shown is popup must have an popupchild ;) )

P.S.: why is it good, that Variables dont work in <% control Page(..) %> (or code segments like this).. they did not work just even with " /$variable " or " /$variable "..

want to clear this fact to understand it!

Attached Files
Avatar
(deleted)

Community Member, 473 Posts

17 December 2008 at 7:14am

You've found one of the limits of the template engine. It passes everything inside the parenthesises as a string to the method.

A better way of doing what you were doing would be to use a has_one to SiteTree, something like:

<?php 
class CreatePopup extends SiteTree { 
   static $db = array( 
   ); 
   static $has_one = array( 
'showContent'=>'SiteTree',
);

   function getCMSFields() { 
      $fields = parent::getCMSFields(); 
      $fields->addFieldToTab('Root.Content.Main', new TreeDropdownField('showContentID', 'showContent', 'SiteTree'), 'Content'); 
      return $fields; 
   } 
}

class CreatePopup_Controller extends ContentController { 
   function init() { 
      parent::init(); 
       
      Requirements::themedCSS("layout"); 
      Requirements::themedCSS("typography"); 
      Requirements::themedCSS("form"); 
   } 
} 
?>

Then in your template you can use

 Hello i am loading Content from $showContent.AbsoluteLink

and, to get the content
$showContent.Content