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.

Archive /

Our old forums are still available as a read-only archive.

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

Control scope


Go to End


21 Posts   8706 Views

Avatar
jam13

121 Posts

6 October 2007 at 5:40am

Can anyone tell me if it's possible to do something like this:

<% control MyControl %>
$MyValue - $MyPageValue
<% end_control %>

where $MyPageValue is a value available _outside_ the control?

If it's not (I can't find a way) then how is one supposed to access values external to a control? You can't pass them in like this:

<% control MyControl($MyPageValue) %>
$MyValue - $MyPageValue
<% end_control %>

Avatar
Fuzz10

Community Member, 791 Posts

8 October 2007 at 2:23am

hi Jam,

Good question ; I had the same problem , you can find my post here :
http://www.silverstripe.com/silverstripe-development/flat/5649

Ended up working around it with big and nasty "IF" blocks around the controls.

Please let us know if you find an answer.

Avatar
Ingo

Forum Moderator, 801 Posts

8 October 2007 at 7:08am

you're right, this is currently not possible. i've always worked around this problem by customizing the passed data with the parent scope.

php:

function Members() {
  $members = DataObject::get('Member');
  return $members->customise(array(
    'ParentScope' => $this
  ));
}

template

<% control Members %>
$FirstName
$ParentScope
<% end_control %>

its ugly, but it works :)

p.s.: you can gain a bit of insight how your template is actually parsed by adding ?showtemplate=1 to the URL.

Avatar
jam13

121 Posts

8 October 2007 at 10:38pm


its ugly, but it works :)

:S

I really wish I could use Smarty with SS. It's got (IMHO) much nicer syntax, and more flexibility when you need it.


p.s.: you can gain a bit of insight how your template is actually parsed by adding ?showtemplate=1 to the URL.

Thanks - that's useful.

Avatar
Double-A-Ron

Community Member, 607 Posts

14 June 2008 at 10:59pm

Can someone please explain this a little clearer? It seems to be a pretty important issue. (I spent an hour trying to work out why a Page type var was not available within a control). That member example doesn't make sense to me at all. I mean, in what context is that function being called??

Avatar
Willr

Forum Moderator, 5523 Posts

15 June 2008 at 10:27am

I think you can access the functions available on the current pages controller by using $Top -

--
When you’re inside a control loop in your template, and want to reference methods on the current controller you’re on, breaking out of the loop to get it, you can use $Top to do so. For example:

$URLSegment
<% control News %>
$URLSegment <!-- may not return anything, as you're requesting URLSegment on the News objects -->
$Top.URLSegment <!-- returns the same as $URLSegment above -->
<% end_control %>

Avatar
Double-A-Ron

Community Member, 607 Posts

15 June 2008 at 11:22am

Thanks for the tip Willr. I just tested this and it works perfectly.

Cheers
Aaron

Avatar
Willr

Forum Moderator, 5523 Posts

15 June 2008 at 11:37am

Edited: 15/06/2008 11:38am

The limitation of this is it gets the topmost, and I dont believe theres a way to get only a Parent element. For example consider -

<% control Member %>
$Firstname // returns John
<% control Projects %>
$Parent.Firstname // returns '' as no firstname defined on Projects 'Parent' in the dataobject sense, not a templating sense.
$Top.Firstname // returns '' as no firstname defined on controller,
$Firstname // returns '' as no firstname defined on a Project
<% end_control %>
<% end_control %>

For this sort of thing, Ingos method works well. The template parser is pretty simple in what it can achieve and its one area that on the core-devs list there was a bit of a discussion about a couple of months back on how we could improve it, and this would be one thing that I would be happy to have!.

Go to Top