Jump to:

23379 Posts in 18298 Topics by 2867 members

General Questions

SilverStripe Forums » General Questions » Includes from php instead of template

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

Page: 1 2
Go to End
Author Topic: 3775 Views
  • micahsheets
    Avatar
    Community Member
    164 Posts

    Includes from php instead of template Link to this post

    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?

  • CrazyCoders
    Avatar
    Community Member
    32 Posts

    Re: Includes from php instead of template Link to this post

    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!

  • micahsheets
    Avatar
    Community Member
    164 Posts

    Re: Includes from php instead of template Link to this post

    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?

  • Willr
    Avatar
    Forum Moderator
    5490 Posts

    Re: Includes from php instead of template Link to this post

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

  • RonnieH
    Avatar
    Community Member
    14 Posts

    Re: Includes from php instead of template Link to this post

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

  • Willr
    Avatar
    Forum Moderator
    5490 Posts

    Re: Includes from php instead of template Link to this post

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

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

  • RonnieH
    Avatar
    Community Member
    14 Posts

    Re: Includes from php instead of template Link to this post

    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?

  • Willr
    Avatar
    Forum Moderator
    5490 Posts

    Re: Includes from php instead of template Link to this post

    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

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