2

I have a simple PHP function that outputs HTML.

<?php
function get_header() {
?>
<div id="header">
  <div class="page-width">
  <!-- And a lot more HTML after this line. -->
<?php
}
?>

So, when I call get_header(), the function outputs the HTML.

What is the simplest option to tweak this function to return the HTML as a string? Do I need to create a wrapper around this function? In other words, I'd like to be able to do e.g. var html_string = get_header_wrapper(), where html_string contains all the HTML above.

One thing I could think of is to duplicate the function and make it return a string. However, that would be so inefficient because it introduces a lot of code duplicate.

<?php
function get_header_wrapper() {
  var ret = <<<EOD
  <div id="header">
    <div class="page-width">
    <!-- And a lot more HTML after this line. -->
  ...
  EOD;

  return ret;
}
?>
moey
  • 10,587
  • 25
  • 68
  • 112
  • 2
    What's wrong with the second code block? (aside from the fact the EOD; should not be prepended with any whitespaces. – Madara's Ghost Jan 04 '12 at 17:08
  • Why can't you modify the existing function? – Jason McCreary Jan 04 '12 at 17:08
  • @Truth: The second block of code will work just fine. However, I was looking for a solution where I don't have to duplicate that chunk of HTML code into a different function. Sorry, I wasn't clear that I have no privilege to modify the `get_header()` directly. Thanks. – moey Jan 04 '12 at 17:15
  • Could be useful: [what is output buffering?](http://stackoverflow.com/questions/2832010/what-is-output-buffering) – hakre Aug 04 '12 at 09:24

3 Answers3

8

You can make use of output bufferingDocs to get the output of that function:

ob_start();
get_header();
$html = ob_get_clean();

If you need that more than once, you can wrap it into a function of it's own:

/**
 * call a function and return it's output as string.
 * 
 * @param callback $function
 * @param array $arguments (optional)
 * @param var $return (optional) the return value of the callback
 * @return string function output
 */
function ob_get_call($function, array $arguments = array(), &$return = NULL)
{
    ob_start();
    $return = call_user_func_array($function, $arguments);
    $buffer = ob_get_clean();
    return $buffer;
}

Usage:

$html = ob_get_call('get_header');

As the answer is that popular today, here is another function to get the output of an include:

/**
 * include a file and return it's output as string.
 * 
 * @param string $file
 * @param array $variables (optional) keys as variable names and values as variable values
 * @param var $includeReturn (optional) the return value of the include
 * @return string function output
 */
function ob_get_include($file, array $variables = array(), &$includeReturn = NULL)
{
    $includeFilename = $file;
    unset($file);
    extract($variables);
    unset($variables);
    ob_start();
    $includeReturn = include($includeFilename);
    return ob_get_clean();
}

Usage:

include.php:

<div class="greeting">
    Hello <em><?php echo htmlspecialchars($name); ?></em>!
</div>

Using:

$variables = array(
    'name' => 'Marianne',
);
$html = ob_get_include('include.php', $vars);

Related:

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836
1

Use output buffering:

<?php
function get_header() {
  ob_start();
  <div id="header">
    <div class="page-width">
    <!-- And a lot more HTML after this line. -->
  ...
  $content = ob_get_contents();
  ob_end_clean();
  return $content;
}
?>

You can even do some processing on the string before returning it. OB rocks!

Mathieu Dumoulin
  • 12,126
  • 7
  • 43
  • 71
0

The best action to get the content of a html-content function is the following and then it should be at your hand:

function html_content(){
ob_start();?>
    <div class="some-div" id='the_id'>html text goes here
    </div>
<?php 
return ob_get_clean();
}

In this method, you can easily copy paste the needed html tags and content to your codes and easily use them.

I've used ob_start(); before the start of html tags and at the end I used return ob_get_clean();

Ebrahim
  • 1,740
  • 2
  • 25
  • 31