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

Including external file, html flow gone bananas


Reply

5 Posts   1129 Views

Avatar
Junglefish

31 July 2009 at 1:34am Community Member, 104 Posts

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,

Avatar
Junglefish

31 July 2009 at 7:44pm Community Member, 104 Posts

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,

Avatar
Junglefish

3 August 2009 at 9:38pm Community Member, 104 Posts

^^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?

Avatar
banal

3 August 2009 at 10:17pm (Last edited: 3 August 2009 10:18pm), Community Member, 901 Posts

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.

Avatar
Junglefish

3 August 2009 at 10:32pm Community Member, 104 Posts

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,