17488 Posts in 4473 Topics by 1978 members
| Go to End | Next > | |
| Author | Topic: | 6350 Views |
-
Control scope

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 %> -
Re: Control scope

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/5649Ended up working around it with big and nasty "IF" blocks around the controls.
Please let us know if you find an answer.
-
Re: Control scope

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.
-
Re: Control scope

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.
-
Re: Control scope

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??
-
Re: Control scope

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 %> -
Re: Control scope

15 June 2008 at 11:22am
Thanks for the tip Willr. I just tested this and it works perfectly.
Cheers
Aaron -
Re: Control scope

15 June 2008 at 11:37am Last edited: 15 June 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!.
| 6350 Views | ||
| Go to Top | Next > |





