(PHP) How to pass the previous INCLUDE / REQUIRE calls into child files? - php

I'm creating my own Templating Script by using the ob_get_contents() as a core method. By using it, it can render out the other files, calling from the one single file.
Just like, lets assume we have 4 files:
index.php
header.html
footer.html
functions.php
index.php will call and render the contents of other files (2 html files here). By using the following codes:
//index.php
function render($file) {
if (file_exists($file)) {
ob_start();
include($file);
$content = ob_get_contents();
ob_end_clean();
return $content;
}
}
echo render('header.html');
echo render('footer.html');
But (for example) when header.html contains a call include('functions.php'), the included file (functions.php) can't be used again in footer.html. I mean, i have to make a include again in footer.html. So here, the line include('functions.php') have to be containing in both of files.
How to include() a file without calling it again from child files?

When you use ob_start() (Output buffering), you end up only with the output of the file, meaning file executed the output is returned by ob_get_content(). As only output is returned that other file is unaware of the includes.
So the answer is: you can't do it with output buffering. Or include your files before ob_start with include_once.

That could work like something like this:
//index.php
function render($file) {
if(!isset($GLOBALS['included'])) {
$GLOBALS['included'] = array();
}
if (!in_array($file, $GLOBALS['included']) && file_exists($file)) {
ob_start();
include($file);
$content = ob_get_contents();
ob_end_clean();
$GLOBALS['included'][] = $file;
return $content;
}
}
echo render('header.html');
echo render('footer.html');
Alternatively you could use include_once (include_once $file;)and PHP will do it for you.
Though I suggest you just make sure the file loading structure is in such shape that these events never happen.

Related

PHP: Can You Pass Include Through a Function?

Is it at all possible to include a file through a function? I don't mean returning the contents as a string. I'd like to create my own custom include method, but right now it doesn't seem possible.
For instance, PHP's include method outputs file contents. I'd like to output file contents, but run them through another function first.
For example:
function include_filter($file){
include($file);
}
include_filter($file);
This example does not use additional functionality, because my primary question here is how to include through a function at all. You can see the issue.
If I include through a function such as include_filter, the code will be included locally inside the function only, not at the line the function was called (line 4 in the example).
The include statement does not output the file contents. However, it does evaluate the file you're including, so if that file contains any echo statements then yes, those are executed the moment you include that file.
You can use an output buffer to catch that output and do something with it before re-echo'ing it:
included_file.php:
<?php
echo "This came from an included file.";
main_file.php:
<?php
function include_filter($path) {
ob_start();
include $path;
$output = ob_get_contents();
ob_end_clean();
echo str_replace("an included file", "a filter function", $output);
}
include_filter("included_file.php");
// will output "This came from a filter function."
Demo: https://3v4l.org/2XkL7

Return html response from PHP page in to variable

I am trying to generate an email with some HTML that is created via another PHP file.
Email generating file is run by a cron running every hour.
Another file exists that generates the HTML required for the email.
The HTML generating file does not have a function that I can call, for instance:
$emailBody = generateHTML($id);
The HTML generating file was designed to be included on a page that you wished to display the HTML, for instance:
include "htmlGenerator.php";
My question is: How can I get what the htmlgenerator.php file returns in to a variable, ready to be pushed in to an email.
Apologies if this is not very clear, I will be happy to answer any questions.
Thanks in advance!
If I understood what you said, you can use output buffering,
so that you can get the output of htmlGenerator.php
For example:
ob_start();
// as an example
echo "Hello World";
// or in our case
include "htmlGenerator.php";
$result = ob_get_contents();
ob_end_clean();
You can also create a simple function like this:
function include_output($filename)
{
ob_start();
include $filename;
$contents = ob_get_contents();
ob_end_clean();
return $contents;
}
$mycontent = include_output("htmlGenerator.php");
Read the php documentation for further details.
Check out the built-in functions ob_start and ob_get_clean.
You can then do something like:
ob_start();
include( "file.php");
$var = ob_get_clean();

How to get multiple PHP files output into a single a file for further usage of those output?

I am trying to get the output of multiple PHP files into a single PHP file where I can save them into different variables for further usage.
For a single PHP file, I used include and it works well. But for multiple files I don't know what to do.
Do you have any experiences or advices on how to achieve this ?
I had three php files called a.php,b.php and c.php. Now in each file i am echoing an array as output. For first php file i done like below to save the output of that in fourth php file called d.php
ob_start();
include_once('a.php');
$output= ob_get_clean();
echo "<pre>";print_r($output);
Now what to do for getting second and third php files outputs.
I think this is what you want
<?php
$string1 = get_include_contents('somefile1.php');
$string2 = get_include_contents('somefile2.php');
function get_include_contents($filename) {
if (is_file($filename)) {
ob_start();
include $filename;
return ob_get_clean();
}
return false;
}
?>

Merge / include PHP files with PHP?

In my local environment I have many small PHP files in different folders. I use "include" or "require once" to run them together.
I'm going to upload my little project to the web but I only want to upload it as one PHP file. I need to merge the files together.
I found a nice function that makes an include as a string.
function get_include_contents($filename)
{
if (is_file($filename)) {
ob_start();
if(file_exists($filename)) include $filename;
$contents = ob_get_contents();
ob_end_clean();
return $contents;
}
return false;
}
It gives me the HTML result, not the PHP code. I need to get the PHP code.
function get_include_contents($filename)
{
if (is_file($filename) && is_readable($filename)) {
return file_get_contents($filename);
}
return false;
}
include is in effect running the code within $filename. Instead, you should use file_get_contents(), which will return the raw contents of the file, without parsing them.

Set included PHP and HTML content to a variable

I am working on a small MVC "framework", which will simply provide a more organized workspace for me, as I've found I love CakePHP but only for it's organization. Now, I'm trying to replicate the layouts (which contain <?php echo $title_for_layout; ?> and <?php echo $content_for_layout; ?>,) and I'm curious how I would be able to load a mixed PHP and HTML file... save what would normally be outputted to the browser to a variable... set the variables mentioned inside the variable, and then output the layout variable and have PHP use the variables inside it to setup the layout exactly how I want it.
Any ideas?
Capture the buffer output to a string with the output buffer control functions.
$string = getIncludeContents('template.php');
function getIncludeContents($filename) {
if (is_file($filename)) {
ob_start();
include $filename;
$contents = ob_get_contents();
ob_end_clean();
return $contents;
}
return false;
}
Example taken from.

Categories