Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » how to use php in theme?

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

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

Page: 1 2
Go to End
Author Topic: 3144 Views
  • jbk
    Avatar
    Community Member
    11 Posts

    how to use php in theme? Link to this post

    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!

  • StuM
    Avatar
    Community Member
    56 Posts

    Re: how to use php in theme? Link to this post

    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

  • grilldan
    Avatar
    Community Member
    135 Posts

    Re: how to use php in theme? Link to this post

    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.

  • jbk
    Avatar
    Community Member
    11 Posts

    Re: how to use php in theme? Link to this post

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

  • jbk
    Avatar
    Community Member
    11 Posts

    Re: how to use php in theme? Link to this post

    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 ;)

  • StuM
    Avatar
    Community Member
    56 Posts

    Re: how to use php in theme? Link to this post

    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

  • jbk
    Avatar
    Community Member
    11 Posts

    Re: how to use php in theme? Link to this post

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

  • StuM
    Avatar
    Community Member
    56 Posts

    Re: how to use php in theme? Link to this post

    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

    3144 Views
Page: 1 2
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.