Echo dynamic content in the middle of a page based on various conditions defined in the head

StackOverflow https://stackoverflow.com/questions/17951765

  •  04-06-2022
  •  | 
  •  

Pergunta

I am trying to dynamically echo some predefined template ('template-file-for-output.php') filled with some data (from the $var array) on a specific place in a number of pages (as an example, the page 'page.php').

Basically my goal is to have a system, in which

  • I set up the logic (in the 'logic.php' file), the functions needed (in 'functions.php'), the template (in 'template-file-for-output.php')

  • with which my colleagues can create whatever page (just as in 'page.php' for the sake of the example) they want with their content and their HTML as they wish and only need to include the functions.php and logic.php files at the beginning of their file, and the echo statement to have the dynamic content where they want it to appear.

The problem I'm having is that when I test this example and try to achieve this in 'page.php', I always get the content before the custom HTML from the page. I suppose it has to do with output buffering and this include in the outputContent function, I tried other things but without success.

Here the contents of the files:

  • logic.php:
$submitOK = false;

if ($submitOK === true) {
    /** No errors, output the error free content */
    $output = outputContent($var);
} else {
    /** Errors, output the content with errors */
    $output = outputContent($var, $errors);
}
  • functions.php:
function outputContent($var, $errors = null)
{
    extract($var);

    ob_start();
    include 'template-file-for-output.php';
    $output = ob_get_contents();
    ob_get_clean();
    return $output;
}
  • template-file-for-output.php:
<p>Some content with tags and so on, filled in by some values of the $var array.</p>
<p>Example with the $example variable extracted from $var <?php echo $example; ?></p>
<p>Another variable also from $var <?php echo $anotherVariable; ?>
  • page.php:
<?php

include 'logic.php';
include 'functions.php';

?>
<!DOCTYPE html>
<html>
    <head><title>A page of the site</title></head>
    <body>
        <p>Something interesting (hopefully).</p>
        <?php echo $output; ?>
    </body>
</html>
Foi útil?

Solução

Change ob_get_contents to ob_get_clean, ob_get_contents gets the contents of the buffer but leaves it intact. Your Previous code got the buffer assigned it a variable then, flushed the buffer to output.

function outputContent($var, $errors = null)
{
    extract($var);

    ob_start();
    include 'template-file-for-output.php';
    $output = ob_get_clean();
    return $output;
}

Outras dicas

Setting aside the point that there are currently templating systems that have already solved this problem quite effectively ...

I would try not include the template file, but rather read the file with file_get_contents and then echo it out inside the output buffering section.

function outputContent($var, $errors = null)
{
    extract($var);

    ob_start();
    echo file_get_contents('template-file-for-output.php');
    $output = ob_get_clean();
    return $output;
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top