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.

Template Questions

Cointroller function inside control loop in template


Go to End
Reply

9 Posts   4245 Views

Avatar
maciszek

15 December 2010 at 10:03pm (Last edited: 15 December 2010 10:04pm), Community Member, 4 Posts

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

16 December 2010 at 3:18am Community Member, 4 Posts

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

16 December 2010 at 8:46am Forum Moderator, 1796 Posts

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

<% if Top.checkSomhing %>

...right?

Avatar
maciszek

16 December 2010 at 8:10pm Community Member, 4 Posts

Right.

Thank you.
Pawel

Avatar
spankmaster79

16 January 2011 at 8:13am Community Member, 46 Posts

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

17 January 2011 at 12:51am Forum Moderator, 1095 Posts

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

17 January 2011 at 1:05am (Last edited: 17 January 2011 1:07am), Community Member, 46 Posts

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

17 January 2011 at 1:20am (Last edited: 17 January 2011 1:33am), Forum Moderator, 1095 Posts

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