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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Passing variables from controller to template


Go to End


4 Posts   5445 Views

Avatar
stinkytofu

Community Member, 17 Posts

8 January 2009 at 12:58am

Edited: 08/01/2009 2:10am

I feel really stupid asking this question, but if I want to make some changes to an existing variable in the controller before it is rendered in the template, such as $Content or $Title or a custom field such as $AuthorName for example, how can I do this? I just can't seem to find instructions on how to do this in the tutorials or the forums. Can someone point me to the right page that explains how to accomplish this?

If someone tells me to RTFM, I'm really sorry that I'm such an idiot, but I just cannot find any information about this anywhere on Google or the Silverstripe tutorials/forums, and I tried really really hard. So would very much appreciate it if you can point me to the right URL.

Thanks!

Avatar
(deleted)

Community Member, 473 Posts

8 January 2009 at 8:07am

Just create a method with the same name as the variable that you want to use, ie:

function AuthorName() {
return $this->Author()->Name;
}

Avatar
Kleinforstkoenig

Community Member, 3 Posts

4 February 2009 at 10:32pm

Hi,

i think i have the same problem as stinkytofu,
and im not that familiar with silverstripe and the mvc framework.

I want to check $MenuTitle string-length to define another Menu CSS class before its been replaced by its Content.
I want to try it by creating a new php-method into my pagetype controller, but i i dont know how to assign $MenuTitle to this funcion

does anybody got a solution for this problem?

greetings from germany.
Sebastian

Avatar
(deleted)

Community Member, 473 Posts

5 February 2009 at 7:40am

function MenuTitleLength() {
return strlen($this->MenuTitle) > length;
}

Then you can use <% if MenuTitleLength %>...<% end_if %> in your template.

The comparison must be done in the controller as the template language doesn't allow inequalities.