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.

Archive /

Our old forums are still available as a read-only archive.

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

<% control name_of function %>


Go to End


5 Posts   1402 Views

Avatar
kusi

Community Member, 21 Posts

23 August 2008 at 1:48am

Edited: 23/08/2008 1:49am

Hi,

My problem is that I use php template and function there that returns for ex. "some text",
and in .ss file when i use <% some_kind_edit_function %>$name_of_function<% end.. %>
"some text" is always writed in the top of html page. Probably I could use html tags and css to position this, but some problems would stay. Has somebody had the same problem. knows how?I want It to be always in place where I wrote It in .ss file.....??

bless

Avatar
FlorianH

Community Member, 33 Posts

23 August 2008 at 2:13am

Actually if you've got a function in your Controller which returns a string, you can use "$name_of_function" in the associated template without the "<% %>" tags. However, I'm never sure to write that function in the Controller or into the Page so you got to have some fun with try and error.

And regarding to your Text positioned at the top of the page, I don't know if this is the case here, but this usually occcurs when having an echo in your Control-Function, so maybe you check out your PHP-code and your way to use that Control in your template.

FlorianH

Avatar
Willr

Forum Moderator, 5523 Posts

23 August 2008 at 12:14pm

yes if you are returning a string from a PHP function echoAString() then make sure it returns rather then echos the string.

FlorianH - all methods that you want to call from templates should be in the controller part of the class. As the Controller is the one that controls the templates. You can put methods in the Page part and call them from the Page_Controller if you need.

Avatar
Sean

Forum Moderator, 922 Posts

23 August 2008 at 4:23pm

Edited: 23/08/2008 4:31pm

Actually, I would recommend putting them in the model class. This is closer to the database and it's easier to manipulate data, while still being able to directly call those functions in the template.

Adding lots of data related methods to the controller gives it too much responsibility for managing data. Also, you run into some issues like having to call $this->dataRecord to get the model record in order to get data from it, when the method could just go on the model instead. The template can still see all those methods on the model class thanks to a handy system called ModelAsController.

This also means you can do this:

$page = DataObject::get_one('Page');
$page->MyMethod();

If MyMethod was on the Page class, then it's callable in the above fashion. If it's on the Page_Controller class, you can't call it. This makes it easier to re-use code! :)

Avatar
kusi

Community Member, 21 Posts

25 August 2008 at 10:24am

Edited: 25/08/2008 9:22pm

My problem was echo function. Thanks a lot. But I return It in function readfile() imported from place that source is not accesible. I will rather use other function to get data, but most important to me was that echo is in the top.Right function was implode().
.
bless