Jump to:

3445 Posts in 1031 Topics by 872 members

Template Questions

SilverStripe Forums » Template Questions » Cointroller function inside control loop in template

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

Page: 1 2
Go to End
Author Topic: 4198 Views
  • maciszek
    Avatar
    Community Member
    4 Posts

    Cointroller function inside control loop in template Link to this post

    Got some problem and cannot find solution on forum and google with SS.

    In Page Controller I've got function like:

    function checkSomhing()
    {
    return true;
    }


    and I would like to use it in template:

    <% if checkSomhing %>
    test
    <% end_if %>

    All works well. But there is case that I need to use it inside another 'control' eg. displaing menu:

    <% control MainMenu %>
    ...
    <% if checkSomhing %>
    test
    <% end_if %>
    <% end_control %>

    And 'checkSomhing' doesn't work. I think I know why, because control loop is looking for 'checkSomhing' in $MainMenu object. It's clear. But I can not find solution to get it work. Need something like:

    <% if this.checkSomhing %>


    or some other solution to call controller function inside control loop.

    Than you in advance.
    Pawel

  • maciszek
    Avatar
    Community Member
    4 Posts

    Re: Cointroller function inside control loop in template Link to this post

    OK, I found this solution - http://silverstripe.org/template-questions/show/11730?start=0
    It's not perfect but it's ok.

    Pawel

  • swaiba
    Avatar
    Forum Moderator
    1792 Posts

    Re: Cointroller function inside control loop in template Link to this post

    Hi, just to check, the answer is....

    <% if Top.checkSomhing %>

    ...right?

  • maciszek
    Avatar
    Community Member
    4 Posts

    Re: Cointroller function inside control loop in template Link to this post

    Right.

    Thank you.
    Pawel

  • spankmaster79
    Avatar
    Community Member
    46 Posts

    Re: Cointroller function inside control loop in template Link to this post

    Can you tell me what you did to make this work with <% Top.dosomething %>??? I read the whole post you refered to and there is A no solution to the problem and B nothing that explains "Top".

    For me it gives Parse error: syntax error, unexpected '}' in /bla/bla

    I've tried putting my function in Controller and Model now....

  • martimiz
    Avatar
    Forum Moderator
    1086 Posts

    Re: Cointroller function inside control loop in template Link to this post

    http://doc.silverstripe.org/built-in-page-controls?s=page%2520controls

    Top refers to the top-most controller In most cases that will be your page. From whithin a control that refers to another object, you only have access to the properties/methods of that particular object. Top will give you access to the Page itself. Example (some page template)

    $Name <-- page name
    <% control SomeObject %>
    $Name <-- SomeObject name
    $Top.Name <-- page name
    <% end_control %>

    Mind: in a nested control situation, Top still refers to the top-most controller...

    And it's <% if Top.dosomething %>

  • spankmaster79
    Avatar
    Community Member
    46 Posts

    Re: Cointroller function inside control loop in template Link to this post

    Hi marimiz,

    ok I understood now... I hope... so

    <% control SomeObject %>
    <% control SomeOtherObject %>
    $Top.Name <-- would still be Page name and not SomeObject name?
    <% end_control %>
    <% end_control %>

    Ok, I put my function now in my SomeObject class and it get's called. But now I want to pass a value from the template to the function. How would I do that?

    <% control SomeObject %>
    <% if checkForSomething(Top.Name) %> <-- this doesn't seem to work
    yeah checked
    <% end_if %>
    <% end_control %>

    If I just return the submited value to the function and print it in the template it prints "Top.Name" and not the value of the property. Isn't this possible? Using $Top.Name breaks it completely and gibes PHP errors

    thx spanky

  • martimiz
    Avatar
    Forum Moderator
    1086 Posts

    Re: Cointroller function inside control loop in template Link to this post

    I don't think you can use variables as function params in a control structure like that. So if you just want to check for the existence of a pagename, you 'd do this:

    <% if Top.Name %>

    If you 'd want to do some extra checks on the page name, you'd do this:

    function Name() {
    // do some checks
    return $CheckedName;
    }

    <% if Top.Name %> <-- remains the same...

    [edit] within a <% Top.Name %> control you leave out the $ sign, everywhere else in the template use $Top.Name

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