Capsule::parse PHP Method

parse() public method

Fetches the results of a tempalte parse and either returns the string or writes results to a specified output file.
public parse ( string $template, string $outputFile = null, boolean $append = false ) : string
$template string The template filename (relative to templatePath or absolute).
$outputFile string If specified, contents of template will also be written to this file.
$append boolean Should output be appended to source file?
return string The "parsed" template output.
    function parse($template, $outputFile = null, $append = false)
    {
        // main work done right here:
        // hopefully this works recursively ... fingers crossed.
        ob_start();
        try {
            $this->display($template);
        } catch (Exception $e) {
            ob_end_flush();
            // flush the output on error (so we can see up to what point it parsed everything)
            throw $e;
        }
        $output = ob_get_contents();
        ob_end_clean();
        if ($outputFile !== null) {
            $outputFile = $this->resolvePath($outputFile, $this->outputDirectory);
            $flags = null;
            if ($append) {
                $flags = FILE_APPEND;
            }
            if (!file_put_contents($outputFile, $output, $flags) && $output != "") {
                throw new Exception("Unable to write output to " . $outputFile);
            }
        }
        return $output;
    }