#41 - HTML Partial in PHP

Data: 2018-12-28 12:00 - PHP

Function to include a HTML partial to output. The model variable can be accessed inside the partial.

<?php
class HTML {
    public static function partial($name, $model) {
        $name = Utils::cleanFilename($name);
        $partial = __SHARED_VIEWS_DIR__ . "/$name.php";
        if (!file_exists($partial))
            trigger_error("Could not find partial '$name'.", E_USER_ERROR);
        ob_start();
        include $partial;
        $output = ob_get_contents();
        ob_end_clean();
        return $output;
    }
}

Previous snippet | Next snippet