Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Control scope

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

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

Page: 1 2 3
Go to End
Author Topic: 6899 Views
  • jam13
    Avatar
    121 Posts

    Control scope Link to this post

    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 %>

  • Fuzz10
    Avatar
    Community Member
    787 Posts

    Re: Control scope Link to this post

    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.

  • Ingo
    Avatar
    Forum Moderator
    801 Posts

    Re: Control scope Link to this post

    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.

  • jam13
    Avatar
    121 Posts

    Re: Control scope Link to this post


    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.

  • Double-A-Ron
    Avatar
    Community Member
    604 Posts

    Re: Control scope Link to this post

    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??

  • Willr
    Avatar
    Forum Moderator
    5511 Posts

    Re: Control scope Link to this post

    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 %>

  • Double-A-Ron
    Avatar
    Community Member
    604 Posts

    Re: Control scope Link to this post

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

    Cheers
    Aaron

  • Willr
    Avatar
    Forum Moderator
    5511 Posts

    Re: Control scope Link to this post

    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!.

    6899 Views
Page: 1 2 3
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.