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
Reply


10 Posts   4022 Views

Avatar
micahsheets

Community Member, 165 Posts

23 June 2009 at 5:26am

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?

Avatar
CrazyCoders

Community Member, 32 Posts

23 June 2009 at 5:49am

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(){
include('file.php');
}

And in your template you do:

$IncludeSomeStuff

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!

Avatar
micahsheets

Community Member, 165 Posts

23 June 2009 at 6:53am

Edited: 23/06/2009 6:54am

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?

Avatar
Willr

Forum Moderator, 5513 Posts

23 June 2009 at 8:45am

<% 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 %>......

Avatar
RonnieH

Community Member, 14 Posts

1 July 2009 at 4:34am

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

Avatar
Willr

Forum Moderator, 5513 Posts

1 July 2009 at 9:06am

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

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

Avatar
RonnieH

Community Member, 14 Posts

3 July 2009 at 3:26am

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?

Avatar
Willr

Forum Moderator, 5513 Posts

3 July 2009 at 2:49pm

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