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.

General Questions

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Includes from php instead of template

Go to End

10 Posts   3862 Views


23 June 2009 at 5:26am Community Member, 165 Posts

Is there a way to use an Action to include a file from the includes folder using logic in the php instead of the template? Im not sure if I can just include the file by reading it into a variable and then having a function to put the contents of the variable on the template. I could do this in normal php code but I was wondering if there were built in methods in the api for doing this?


23 June 2009 at 5:49am Community Member, 32 Posts

I'm not sure cause MVC is pretty new to me, i still have a lot of tests to understand this but... you would need to create some kind of function in the page controller. For example:

function IncludeSomeStuff(){

And in your template you do:


And it should output the information. Note that your include file will be in the context of the function so you need to globalize some stuff to access it outside the function or you use $this if it's something you need in the script that is provided by the page_controlller!


23 June 2009 at 6:53am (Last edited: 23 June 2009 6:54am), Community Member, 165 Posts

Here is something I found in another thread.

It seems that at least at one point it would work to use a statement like this in the ss file.

<% include {$URLSegment}_sidebar %>

however doing this doesn't include the file although the $URLSegment is parsed and replaced. So you end up with <% include bob_sidebar %> in the output. instead of the included file. Does anyone know how to make this syntax work in the ss template files? Or was this functionality removed?


23 June 2009 at 8:45am Forum Moderator, 5511 Posts

<% include {$URLSegment}_sidebar %>

Won't currently work as the template parser only does 1 sweep - so the on the parse as you have noticed it just parses the $URLSegment. In order to do a sidebar based on url you would have to do it in the PHP or use an <% if URLSegment = blah %><% include blah_sidebar %><% else_if %>......


1 July 2009 at 4:34am Community Member, 14 Posts

But surely there must be some way to conditionally include pieces other than such a monstrous method of a hundred 'if' statements, not only cluttering stuff, but making it far more tedious and intransparant to expand your options.

(I don't think inclusion based on url is a good idea, but there are other applications where you'd want to have some global variable determine which file is included.)

I'm having the same problem right now... I have a site with about twelve (and this might increase later on) different sidebars, and I have a Enum-value attached to my pages in the database. And the value is basically a selector for which sidebar the page should use/include, however it won't actually do so.

I suppose the 'if' solution would be workable, but if it can be avoided I'd rather not commit such a sin...

Isn't it somehow possible to parse the variable before it reaches the include, and get it in there in a way that the parses does properly handle the include? Or to just force a second sweep for part of the code? (I don't know much about coding at all, so forgive me if my suggestion/question smacks of ignorance.)


1 July 2009 at 9:06am Forum Moderator, 5511 Posts

such a monstrous method of a hundred 'if' statements, not only cluttering stuff

Using a switch/case statement in PHP would be faster.


3 July 2009 at 3:26am Community Member, 14 Posts

That might work if I just wanted to put in some .php, not for .ss though. Or am I mistaken here?

P.S. So I guess Silverstripe has no way whatsoever of doing this?


3 July 2009 at 2:49pm Forum Moderator, 5511 Posts

No templates do not support case statements so else ifs are the best in the template, though the template is designed to keep lots of the logic like this in the php rather then templates

Go to Top