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.
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.
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";
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.
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.
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?
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.
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?