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

Render SilverStripe template from external PHP code


Go to End


6 Posts   4425 Views

Avatar
gshegosh

Community Member, 6 Posts

29 June 2008 at 9:07pm

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?

Avatar
Willr

Forum Moderator, 5523 Posts

29 June 2008 at 9:19pm

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?

Avatar
gshegosh

Community Member, 6 Posts

29 June 2008 at 9:28pm

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.

Avatar
Willr

Forum Moderator, 5523 Posts

29 June 2008 at 9:48pm

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.

Avatar
matbtt

Community Member, 3 Posts

2 July 2008 at 10:43am

Edited: 02/07/2008 10:56am

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

Avatar
Sam

Administrator, 690 Posts

7 July 2008 at 3:07pm

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.