Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Render SilverStripe template from external PHP code

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

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

Page: 1
Go to End
Author Topic: 3662 Views
  • gshegosh
    Avatar
    Community Member
    6 Posts

    Render SilverStripe template from external PHP code Link to this post

    Hi folks,

    I'm using SilverStripe as a CMS in my projects and my own PHP framework for dynamic stuff (such as getting lists of things from the database).

    What I would like to do is to "fetch" page header and footer, the menu, etc. from SilverStripe as a string that I could use in my code (which is outside SilverStripe infrastructure).

    So, basically I want to have a test.php file somewhere that would call some SilverStripe code and it would get renderered mysite/templates/Includes/Menu.ss etc.

    I've tried some approaches, but I got only so far that by extending a Page_Controller I was able to render some includes, but the menu was just <ul> </ul>

    Would anyone please point me in the right direction?

  • Willr
    Avatar
    Forum Moderator
    5511 Posts

    Re: Render SilverStripe template from external PHP code Link to this post

    this would be extremely complex to do. Basically because the template is so tied into the sapphire framework via SSViewer - the template parser.

    PHP framework for dynamic stuff (such as getting lists of things from the database)

    Why not use sapphire for your project?

  • gshegosh
    Avatar
    Community Member
    6 Posts

    Re: Render SilverStripe template from external PHP code Link to this post

    Would using sapphire help? How can I render a single include (such as Menu.ss) from sapphire controller? i got empty <ul></ul> when I tried to use renderWith() method

    OT: the "Post" button should be more visible in this form. My intuition told me to click Reply below after writing the post and so I lost all the text I wrote.

  • Willr
    Avatar
    Forum Moderator
    5511 Posts

    Re: Render SilverStripe template from external PHP code Link to this post

    Would using sapphire help?

    I was refering to, rather then using silverstripe as just a template parser and trying to get that to work it might be easier to use SilverStripe as your php framework.

  • matbtt
    Avatar
    Community Member
    3 Posts

    Re: Render SilverStripe template from external PHP code Link to this post

    Hi all,

    I came accross the same topic trying to use SilverStripe with an existing application. Tonight I was playing around and found a possible solution: a DecoratorPage. A page of this type can be used to decorate the result of your framework with the standard layout of your site without having a knowledge of the internals of SilverStripe. So, how to achive this:

    • - Create a new page type (see tutorial) plus controller
    • - Create a template within mysite/Layout/ and name it like the page type, extension is .ss. Add a placeholder, e.g. $contentFromMyFramework.
    • - Overwrite the init method of your controller, here you can place the initalisation stuff for your external framework, if required. In my case I'm using a MVC framework
    • - Implement an method called "index" with exactly one parameter $requestParam. Execute your external framework. The URLSegment of the request can be accessed with $this->dataRecord->URLSegment. Grab the result of your external functionality and return it within an array using key $contentFromMyFramework.
    • - Flush the caches and access the page via SilverStripe. You should see the result of your external framework within the output of SilverStripe.

    \n

    There are some problems within this approach, e.g. what happens with links within your external templates, furthermore you might have to change your external templates as header, menu and footer are rendered by SilverStripe, authentication etc. Actually I'm not sure whether this approach is usable, but maybe it helps.

    Why not use sapphire for your project?
    Protection of investment? If you already have an application it would not be wise to port it only because you need a CMS integration.

    Best regards, Mathias

  • Sam
    Avatar
    Administrator
    679 Posts

    Re: Render SilverStripe template from external PHP code Link to this post

    You could make a hidden page on your site called "skeleton".

    Set the content to "<-- custom framework content -->"

    Then, in your framework, request the skeleton page using curl or something (you can cache the result for performance), and do a str_replace() on the "<!-- custom framework content -->" string.

    3662 Views
Page: 1
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.