Jump to:

23459 Posts in 18906 Topics by 2877 members

General Questions

SilverStripe Forums » General Questions » Including external file, html flow gone bananas

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
Go to End
Author Topic: 1116 Views
  • Junglefish
    Avatar
    Community Member
    104 Posts

    Including external file, html flow gone bananas Link to this post

    Hi

    I need to 'include' a PHP file into a content page. (The file is an LDAP search programme, but that's irrelevant for now).

    I've added a new function to \mysite\code\Page.php:

       function Trombinoscope(){
          include('myPath\myFile.php');
       }

    and I've added a call to this function in \themes\blackcandy\templates\layout\Pass.ss

          ...
    <h2>$Title</h2>
          $Content
          $Form
          $PageComments
          $Trombinoscope
    ...

    All good. The template gets 'included' and the functionality is fine.

    Problem is, $Trombinoscope appears on the rendered page as the very first thing in the html flow, even before the <!DOCTYPE declaration.

    Can anybody point me in the right direction?

    Thanks,

  • Junglefish
    Avatar
    Community Member
    104 Posts

    Re: Including external file, html flow gone bananas Link to this post

    Some further info...

    Firstly, apologies for the typo in the page name; I of course meant: \themes\blackcandy\templates\layout\Page.ss

    Now, if I amend the code slightly to this:

          <h2>$Title</h2>
          $Content
          $Form
          $PageComments
          $Trombinoscope(Any old value)

    and this:

       function Trombinoscope($myValue) {
          include('myPath\myFile.php');
          return $myValue;
       }

    the string "Any old value" appears in the correct place on the page, ie. after the PageComments, yet the 'included' file (myPath\myFile.php) still appears before the <!DOCTYPE declaration.

    So I'm assuming I need to 'do something else', possibly with the return value of the function, but can't figure out what.

    All comments appreciated,

  • Junglefish
    Avatar
    Community Member
    104 Posts

    Re: Including external file, html flow gone bananas Link to this post

    ^^bump^^

    Really? Has no-one seen this problem before..? C'mon, I'm obviously doing something wrong, or I've missed a step. Can anyone advise?

  • banal
    Avatar
    Community Member
    901 Posts

    Re: Including external file, html flow gone bananas Link to this post

    Hi
    You can't just include a php file and then assume it will output it's content where you want. AFAIK, your "Trombinoscope" function is called by the SSViewer when it populates the template. Since your script most likely uses echo, the output will be sent to the browser at the time of the function call. I guess, the function calls occur before the whole template data is being assembled. Thus, the content appears at top of the page.

    The $Trombinoscope variable in the template will be filled with the return value of the corresponding function, which is in your case: nothing, or as you noticed, the contents of $myValue.

    There might be a solution to this though. PHP allows you to capture all output using the "Output control functions" (http://ch.php.net/manual/en/ref.outcontrol.php). I suggest, you try something like this:

    function Trombinoscope(){
       ob_start();
       include('myPath\myFile.php');
       $data = ob_get_contents();
       ob_end_clean();
          
       return $data;
    }

    This will capture all output from myFile.php to the $data variable. The function then returns the captured data which is exactly what the template engine needs.

  • Junglefish
    Avatar
    Community Member
    104 Posts

    Re: Including external file, html flow gone bananas Link to this post

    Mr Banal

    That, my friend, works perfectly!

    Do you know if that little trick is recorded anywhere in the SS documentation or tutorials? If not, it certainly should be!

    Thanks a bunch,

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