21295 Posts in 5734 Topics by 2602 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 888 Views |
-
Including external file, html flow gone bananas

31 July 2009 at 1:34am
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,
-
Re: Including external file, html flow gone bananas

31 July 2009 at 7:44pm
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,
-
Re: Including external file, html flow gone bananas

3 August 2009 at 9:38pm
^^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?
-
Re: Including external file, html flow gone bananas

3 August 2009 at 10:17pm Last edited: 3 August 2009 10:18pm
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.
-
Re: Including external file, html flow gone bananas

3 August 2009 at 10:32pm
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,
| 888 Views | ||
|
Page:
1
|
Go to Top |


