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

how to use php in theme?


Go to End


11 Posts   4013 Views

Avatar
jbk

Community Member, 11 Posts

9 September 2008 at 7:37pm

hi,
i´ve a problem. i want to use php-code in my theme (page.ss) but, when if i use php i got only a blank site.

but i need php, what can i do? THX!

Avatar
StuM

Community Member, 59 Posts

9 September 2008 at 7:51pm

As far as I know, you can't. Create a function in your page controller class that returns the result of your php code, then place $FunctionName in the template

Avatar
grilldan

Community Member, 135 Posts

9 September 2008 at 7:57pm

You can use StuM's answer if you know what your doing, otherwise you could use an iframe linking to the php script, BUT iframes are the devil, so I'd suggest going with StuM's answer.

Avatar
jbk

Community Member, 11 Posts

9 September 2008 at 7:57pm

hey StuM,
thank you for your answer. in which file i must create my function? ;)

Avatar
jbk

Community Member, 11 Posts

9 September 2008 at 8:02pm

oh no iframes! it´s not a problem for me to create an own function. i only wants to know in which php-file i have to create my own function. thats all ;)

Avatar
StuM

Community Member, 59 Posts

9 September 2008 at 8:06pm

Edited: 09/09/2008 8:09pm

In mysite/code/Page.php, if you've created it. If not, create one with the following:

<?php

class Page extends SiteTree {
        static $db = array();
        static $has_one = array();
}

class Page_Controller extends ContentController {
        function init() {
                parent::init();
        }

        function MyCustomPHPCode()
        {
               return 'blah blah blah';
        }
}

save it as mysite/code/Page.php

then place $MyCustomFunction in your template

I think that's the correct way to achieve it, I'm a SS n00b still and that's how I've been doing it

you may have to run yoursite/db/build?flush=1 to get it working

Avatar
jbk

Community Member, 11 Posts

9 September 2008 at 8:15pm

oh its doesn´t work. i got no output, whats wrong?

Avatar
StuM

Community Member, 59 Posts

9 September 2008 at 8:30pm

Edited: 09/09/2008 8:31pm

Sorry, I put you wrong above, the template variable should be the same as the function name ($MyCustomPHPCode)

I just tried placing php code in the template myself, and it did work(although printed before the html started), so maybe something else is broken

try placing

Director::set_environment_type("dev");

in mysite/_config.php and see if that gives you any hints

Go to Top