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

Executing a php script on a certain page?


Go to End


7 Posts   2447 Views

Avatar
plm

Community Member, 9 Posts

22 October 2010 at 6:10am

This may sound like a stupid question, but how do I get a silverstripe page to include php? The documentation says all content is stored in mysite folder, but there's nothing pertinent to the page I've created there. It seems like this should be very simple, but I can't figure it out for the life of me.

Avatar
Willr

Forum Moderator, 5523 Posts

22 October 2010 at 5:41pm

Your template cannot include PHP. You can include a variable like $Foo and then in your controller (eg Page_Controller) you can define a function Foo() {} which does something. For example

function Foo() {
return "Hi";
}

Having $Foo in your template will return "Hi";

Avatar
plm

Community Member, 9 Posts

23 October 2010 at 11:42am

Edited: 23/10/2010 11:43am

Thanks for your reply. Do I have to do something special to get the templates to link with the .php? I tried doing alot of different things with my TestPage.ss file in themes/templates/tutorial/Layout/ but I can't get it to affect a page of my type TestPage at all.

Avatar
Willr

Forum Moderator, 5523 Posts

23 October 2010 at 4:20pm

Make sure the temple 'TestPage.ss' is located in the correct theme location. themes/blackcandy/templates/Layout/TestPage.ss should be the path if you're editing the blackcandy theme.

Also double check to make sure your page type is a TestPage in the CMS and you have run a ?flush=1.

Avatar
plm

Community Member, 9 Posts

26 October 2010 at 5:40am

Thanks again for the reply. I now can execute the php code by calling the function I defined in the .php from the .ss file. But I cannot get it to output the results under the Content area, instead they output as the very first thing, before the header or menus, no matter where I call the function in the .ss file, how can I do this?

Avatar
Willr

Forum Moderator, 5523 Posts

26 October 2010 at 9:17am

This is probably because you are using echo or print. Instead you should use return to pass the value back to the template like my previous example.

Avatar
plm

Community Member, 9 Posts

28 October 2010 at 8:35am

I followed your advice and have the php working correctly (I think). It appears to generate the correct html code, but the form which it generates fails to send user inputs to the appropriate script. I've gone over it and everything is in the proper directories, but for some reason the same php code that generates a working form wont submit correctly now that I've put it in silverstripe. (The only thing that was changed was echo). Is there some measure I need to take to enable my form?