Render include file to variable

<?php
$GLOBALS['BufferedErrors']=Array();
function errorParse($errno, $errstr, $errfile, $errline, $errcontext) {
    $errorTypes = Array(
        E_ERROR => 'Fatal Error',
        E_WARNING => 'Warning',
        E_PARSE => 'Parse Error',
        E_NOTICE => 'Notice',
        E_CORE_ERROR => 'Fatal Core Error',
        E_CORE_WARNING => 'Core Warning',
        E_COMPILE_ERROR => 'Compilation Error',
        E_COMPILE_WARNING => 'Compilation Warning',
        E_USER_ERROR => 'Triggered Error',
        E_USER_WARNING => 'Triggered Warning',
        E_USER_NOTICE => 'Triggered Notice',
        E_STRICT => 'Deprecation Notice',
        E_RECOVERABLE_ERROR => 'Catchable Fatal Error'
    );
    $ret=(object)Array(
        'number'=>$errno,
        'message'=>$errstr,
        'file'=>$errfile,
        'line'=>$errline,
        'context'=>$errcontext,
        'type'=>$errorTypes[$errno]
    );
    $GLOBALS['BufferedErrors'][]=$ret;
    return false;
}
function parse($fileToInclude, $argumentsToFile=false) {
    $bufferedErrorStack = $GLOBALS['BufferedErrors'];
    set_error_handler('errorParse', error_reporting());
    $GLOBALS['BufferedErrors']=Array();
 
    if (!file_exists($fileToInclude))
        return '';
    if ($argumentsToFile === false)
        $argumentsToFile = Array();
    $argumentsToFile = array_merge($GLOBALS, $argumentsToFile);
    foreach ($argumentsToFile as $variableName => $variableValue)
        $$variableName = $variableValue;
    ob_start();
    include($fileToInclude);
    $ret = ob_get_contents();
    ob_end_clean();
 
    restore_error_handler();
    $errors = $GLOBALS['BufferedErrors'];
    $GLOBALS['BufferedErrors'] = $bufferedErrorStack;
    if (count($errors)>0) {
        $ret.='<ul class="error">';
        foreach ($errors as $error)
            $ret.=
                '<li>'.
                    '<b>'.$error->type.'</b>: '.
                    $error->message.
                    '<blockquote>'.
                        '<i>file</i>: '.$error->file.'<br />'.
                        '<i>line</i>: '.$error->line.
                    '</blockquote>'.
                '</li>';
        $ret.='</ul>';
    }
    return $ret;
}

Download this snippet

This script includes a PHP file - but render its output to a variable, rather than to the buffer. It's also set up to load the script with a variable set, and automagically loads globals into the script's namespace, making it an effective templating scheme. It also has error handling, so that you're not flying blind when using output buffering.

Twitter Twitter

5 Comments to “ Render include file to variable”

  1. Anonymous  on Sep 25, 2009

    X8KNTp <a href="http://tdznaeqnnjrh.com/">tdznaeqnnjrh</a>, [url=http://hhnrgaioqtpt.com/]hhnrgaioqtpt[/url], [link=http://fwfpvvxdmdma.com/]fwfpvvxdmdma[/link], http://jdmgkmrcxevo.com/

  2. Anonymous  on Nov 04, 2009

    klZVlz <a href="http://pzhzpeadndtw.com/">pzhzpeadndtw</a>, [url=http://btsfykrhbsmw.com/]btsfykrhbsmw[/url], [link=http://irjkpkclijle.com/]irjkpkclijle[/link], http://tpfsxdrgczhf.com/

  3. Anonymous  on Dec 28, 2009

    l1wS5x <a href="http://soqugqjudwux.com/">soqugqjudwux</a>, [url=http://rwcfxzvsivwh.com/]rwcfxzvsivwh[/url], [link=http://svngaarwpebr.com/]svngaarwpebr[/link], http://xjycomwkgksk.com/

  4. Anonymous  on Jan 13, 2010

    qQ2Xt9 <a href="http://eqhdypzypraz.com/">eqhdypzypraz</a>, [url=http://aygfsxspniwg.com/]aygfsxspniwg[/url], [link=http://hxiqhnxumffz.com/]hxiqhnxumffz[/link], http://icmfkyoesyrb.com/

  5. Anonymous  on Mar 14, 2010

    V1F3hw <a href="http://nsrbrbhjspmm.com/">nsrbrbhjspmm</a>, [url=http://hevaybgzsiex.com/]hevaybgzsiex[/url], [link=http://jaexsdrxihoa.com/]jaexsdrxihoa[/link], http://jpqazlqmkqnj.com/

Leave a Comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>