Jump to:

3460 Posts in 1064 Topics by 739 members

Data Model Questions

SilverStripe Forums » Data Model Questions » 2.4: Access to variables in control loop possible?

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

Page: 1
Go to End
Author Topic: 674 Views
  • LarsE
    Avatar
    Community Member
    8 Posts

    2.4: Access to variables in control loop possible? Link to this post

    Hello dear community,

    I want to access the content of a variable which is given in the URL segment

    e.g. www.mysite.com/mypage/?country=germany

    where country is the variable and germany is the content.

    I added a public function in the page controller like this:

    public function returncountry()
    {
    return (isset($_REQUEST['country'])) ? $_REQUEST['country'] :
    false;
    }

    Now I can access the content of the variable in the template with

    <% if returncountry %>
    $returncountry
    <% else %>
    Sorry, no country selected
    <% end_if %>

    So far, everything works fine.

    Now I want to filter a certain table of the database within
    a control loop with the content of this variable, but this doesn't work.

    Within the control loop, the content of the variable is empty.

    The following example code in the template

    <p>$returncountry</p>
    <% control myFunction %>
    <p>$returncountry</p>
    <% end_control %>
    <p>$returncountry</p>

    gives this result:

    germany
    (blank line)
    germany

    How can I access userdefined variables within a control loop?

    Thanks for your support!

  • Willr
    Avatar
    Forum Moderator
    5508 Posts

    Re: 2.4: Access to variables in control loop possible? Link to this post

    Use $Top.FunctionName

  • LarsE
    Avatar
    Community Member
    8 Posts

    Re: 2.4: Access to variables in control loop possible? Link to this post

    Hi Willr,

    thank you so much. It works!

    674 Views
Page: 1
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.