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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Question about $TopURLSegment


Reply


6 Posts   987 Views

Avatar
micahsheets

Community Member, 165 Posts

5 February 2009 at 2:30pm

I know how to use $TopURLSegment, however what I really need at this point is <% if TopURLSegment = %>. I tried to make my own function to do this but I could not figure out how to write it or where to put it.

Avatar
Willr

Forum Moderator, 5513 Posts

5 February 2009 at 3:41pm

Sorry if I don't follow your question - you want to do something like

<% control Top %>
<% if URLSegment = mypage-url %>
// do stuff
<% end_if %>
<% end_control %>

?

Avatar
micahsheets

Community Member, 165 Posts

6 February 2009 at 7:14am

I understand what you are saying, but what I need is more like this

<% control Top %>
<% if URLSegment = mypage-url %>

// Get variable from Top

<% end_control %>
<% else %>

// Get variable from This

<% end_if %>

Avatar
Carbon Crayon

Community Member, 598 Posts

6 February 2009 at 1:37pm

Edited: 06/02/2009 1:39pm

What do you mean by 'Get variable from top/this'? are you using it in a function on the current page? If so you will need to do this stuff from the controller rather than the template, as you can't pass variables into the controller from the template.

Alternatively you could have the function on your mypage-url page type and call it from within the control block

Avatar
SalvaStripe

Community Member, 89 Posts

6 February 2009 at 9:40pm

Edited: 06/02/2009 9:43pm

you want to get some vars from the pages, right?
so, did you already created these vars like this options...

class PageWithVars extends Page {
static $db = array(
'Var1' => 'Text',
'Var2' => 'Text',
);
static $has_one = array(
);
function getCMSFields() {
$fields = parent::getCMSFields();

$fields->addFieldToTab('Root.Content.Main', new TextField('Var1'), 'Content');
$fields->addFieldToTab('Root.Content.Main', new TextField('Var2'), 'Content');
   
return $fields;
}
}

class PageWithVars_Controller extends Page_Controller {
}

With that you can get the vars in the .ss file with $Var1 and $Var2.
if your parent page has these vars too, you can get them simple by

$Parent.Var1
$Parent.Var2

#######################
Other way is to make a function in PageWithVars_Controller
(My example will get many Vars from a DB, and later you can use in .ss file [could be much helpful for someone.. ;)])

function MySuperVarFunction() {

   $messages = new DataObjectSet();
      
   $result = DB::query("SELECT * FROM Messages WHERE ToID = '".Member::currentUserID()."' AND Archiv = '0' ORDER BY Date DESC");
   if($result) {
      foreach($result as $value) {
         $Date = date("d.m.Y",$value['Date']);
         $Time = date("H:i",$value['Date']);
         $entries = array(
                        'ID' => $value['ID'],
                        'Title' => $value['Title'],
                        'Message' => nl2br(htmlspecialchars($value['Message'])),
                        'Datum' => $Date,
                        'Uhrzeit' => $Time,
                        'New' => $value['isNew'],
                        'Archiv' => $value['Archiv'],
                        'SendFrom' => $SendFrom,
                        'FID' => $FID
                        );
         $messages->push(new ArrayData($entries));
      }
   }
   return $messages;
}

And now you can do cool stuff in the .ss file ;)

<% control MySuperVarFunction %>
$Date - $Time<br >
$Title<br >
<% if New == 1 %><img src="mysite/images/new.jpg" ><% end_if %><br >
From <em>$SendFrom</em><br >
$Message
<% end_control %>

SalvaStripe

Avatar
micahsheets

Community Member, 165 Posts

13 February 2009 at 9:42am

In my post when I had "// Get variable from Top " or "// Get variable from This ", those were sudo code for maybe $Title where in <% control Top %> I know it would be the Title of the Top. But while controlling Top I was wondering hour to break out of the control without breaking the if statements. However I found that I can end a control without breaking the if statements.

<% control Top %>
<% if foo(bar) %>
<% end_control %>
Do stuff
<% else %>
Do other stuff
<% end_if %>

This way I can get some info about the Top scope but still deal with variables and content from the scope of the current page.

So in the end I kind of answered my own question, but I appreciate and learn from all the comments anyways.