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.

Archive

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

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

Control scope


Go to End
Reply

21 Posts   6909 Views

Avatar
jam13

6 October 2007 at 5:40am 121 Posts

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

8 October 2007 at 2:23am Community Member, 787 Posts

hi Jam,

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

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

8 October 2007 at 7:08am Forum Moderator, 801 Posts

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

8 October 2007 at 10:38pm 121 Posts


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

14 June 2008 at 10:59pm Community Member, 604 Posts

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

15 June 2008 at 10:27am Forum Moderator, 5511 Posts

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

15 June 2008 at 11:22am Community Member, 604 Posts

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

Cheers
Aaron

Avatar
Willr

15 June 2008 at 11:37am (Last edited: 15 June 2008 11:38am), Forum Moderator, 5511 Posts

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