|
Integrating Content From Third Party Script into
Miraserver Templates
A common question which is asked is how to go about integrating content
from a third-party PHP script into your Miraserver-driven website. The
procedure for this is laid out below.
It should be noted that this procedure may vary somewhat depending on
the script you are trying to integrate.
Okay, let's say you have a PHP script. That script generates it's own
output, and when you run that script directly in your web browser, you
get the desired output. The trick is to bring this output into your Miraserver
template system.
The Miraserver template system works through the use of template variables.
A template variable is located inside of a template, and is assigned a
value dynamically when a user visits your website. Since most third-party
PHP scripts echo output to the browser directly, they are not set up to
be assigned to a template variable. If you simply include the PHP script
in your "phpinclude" template, the output will appear at the
very top of the page. What needs to be done here is to use PHP's output
buffering capability to capture the content of the PHP script and assign
it to a variable.
Using your phpinclude template in Miraserver, let's integrate a theoretical
script called last10.php. Let us assume that last10.php is designed to
pull the latest 10 threads from your vBulletin database (or any forum
software) and output the thread list to the screen. You have modified
last10.php so that it's output looks right, but you need to pull it into
your Miraserver-powered website. So, include the following code into your
phpinclude template:
ob_start();
require("/full/path/to/last10.php");
$last10 = ob_get_contents();
ob_end_clean();
mira_select_db($db_name,$db);
What this code does is starts output buffering, grabs the content of
last10.php, assigns it to a PHP variable called $last10, and cleans the
output buffer. The last line is intended to re-select the Miraserver database.
This last line is not always necessary. It would not be necessary if the
third-party script has no database activity or is querying the same database
which contains Miraserver. Our example, though, assumes we are connecting
to vBulletin, and in order to do that, that script is connecting to and
selecting the VB database. In order to continue operating Miraserver after
including last10.php, we must re-select the Miraserver database.
After running the above code in your phpinclude template, you have the
output of last10.php assigned to a variable called $last10. Now all you
need to do is assign that variable to a template variable and you are
set to go. The template assignment (in version 1.0 or prior) must be made
in the PHP file itself. So, open the PHP file in a text editor (article.php,
index.php, etc) and, anywhere after
$tpl->prepare();
assign the variable with the following:
$tpl->assign("last10",
$last10);
Now, anywhere in your templates (for example the main template), you
can insert the variable and it will render your 10 latest threads.
|