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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Template Questions /

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

Cointroller function inside control loop in template


Go to End


9 Posts   8362 Views

Avatar
maciszek

Community Member, 4 Posts

15 December 2010 at 10:03pm

Edited: 15/12/2010 10:04pm

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

Avatar
maciszek

Community Member, 4 Posts

16 December 2010 at 3:18am

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

Pawel

Avatar
swaiba

Forum Moderator, 1899 Posts

16 December 2010 at 8:46am

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

<% if Top.checkSomhing %>

...right?

Avatar
maciszek

Community Member, 4 Posts

16 December 2010 at 8:10pm

Right.

Thank you.
Pawel

Avatar
spankmaster79

Community Member, 46 Posts

16 January 2011 at 8:13am

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

Avatar
martimiz

Forum Moderator, 1391 Posts

17 January 2011 at 12:51am

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

Avatar
spankmaster79

Community Member, 46 Posts

17 January 2011 at 1:05am

Edited: 17/01/2011 1:07am

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

Avatar
martimiz

Forum Moderator, 1391 Posts

17 January 2011 at 1:20am

Edited: 17/01/2011 1:33am

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

Go to Top