Skip to content

Instantly share code, notes, and snippets.

@geomorillo
Forked from ziadoz/halt_compiler.php
Created September 28, 2018 03:43
Show Gist options
  • Select an option

  • Save geomorillo/80ea53b8d0ee64c941659e924335ff59 to your computer and use it in GitHub Desktop.

Select an option

Save geomorillo/80ea53b8d0ee64c941659e924335ff59 to your computer and use it in GitHub Desktop.

Revisions

  1. @ziadoz ziadoz revised this gist Apr 1, 2013. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion halt_compiler.php
    Original file line number Diff line number Diff line change
    @@ -31,7 +31,6 @@ function parse_template($template, $data) {
    $output = parse_template($template, array(
    'title' => 'Hello, World!',
    'paragraph' => 'This is an exciting paragraph of text.',

    ));

    echo $output;
  2. @ziadoz ziadoz revised this gist Apr 1, 2013. 1 changed file with 15 additions and 15 deletions.
    30 changes: 15 additions & 15 deletions halt_compiler.php
    Original file line number Diff line number Diff line change
    @@ -9,28 +9,28 @@
    * http://php.net/manual/en/function.halt-compiler.php
    */
    function get_halt_data() {
    return file_get_contents(__FILE__, false, null, __COMPILER_HALT_OFFSET__);
    return file_get_contents(__FILE__, false, null, __COMPILER_HALT_OFFSET__);
    }

    function create_tmp_template($data) {
    $tmpfile = tempnam(sys_get_temp_dir(), 'halt_compiler_template');
    file_put_contents($tmpfile, $data);
    return $tmpfile;
    $tmpfile = tempnam(sys_get_temp_dir(), 'halt_compiler_template');
    file_put_contents($tmpfile, $data);
    return $tmpfile;
    }

    function parse_template($template, $data) {
    $data = (array) $data;
    ob_start();
    extract($data);
    include $template;
    return ob_get_clean();
    $data = (array) $data;
    ob_start();
    extract($data);
    include $template;
    return ob_get_clean();
    }

    $template = create_tmp_template(get_halt_data());

    $output = parse_template($template, array(
    'title' => 'Hello, World!',
    'paragraph' => 'This is an exciting paragraph of text.',
    'title' => 'Hello, World!',
    'paragraph' => 'This is an exciting paragraph of text.',

    ));

    @@ -43,11 +43,11 @@ function parse_template($template, $data) {
    <!DOCTYPE>
    <html>
    <head>
    <meta charset="utf-8" />
    <title><?php echo $title; ?></title>
    <meta charset="utf-8" />
    <title><?php echo $title; ?></title>
    </head>
    <body>
    <h1><?php echo $title; ?></h1>
    <p><?php echo $paragraph; ?></p>
    <h1><?php echo $title; ?></h1>
    <p><?php echo $paragraph; ?></p>
    </body>
    </html>
  3. @ziadoz ziadoz revised this gist Apr 1, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion halt_compiler.php
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@
    /**
    * The __halt_compiler() function will stop the PHP compiler when called.
    * You can then use the __COMPILER_HALT_OFFSET__ constant to grab the contents of the PHP file after the halt.
    * In this example a simple PHP template is stored after the halt, to allow simple separation of logic from templating.
    * In this example a PHP template is stored after the halt, to allow simple separation of logic from templating.
    * The template is stored in a temporary file so it can be included and parsed.
    *
    * See: https://github.com/bobthecow/mustache.php/blob/dev/src/Mustache/Loader/InlineLoader.php
  4. @ziadoz ziadoz created this gist Apr 1, 2013.
    53 changes: 53 additions & 0 deletions halt_compiler.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,53 @@
    <?php
    /**
    * The __halt_compiler() function will stop the PHP compiler when called.
    * You can then use the __COMPILER_HALT_OFFSET__ constant to grab the contents of the PHP file after the halt.
    * In this example a simple PHP template is stored after the halt, to allow simple separation of logic from templating.
    * The template is stored in a temporary file so it can be included and parsed.
    *
    * See: https://github.com/bobthecow/mustache.php/blob/dev/src/Mustache/Loader/InlineLoader.php
    * http://php.net/manual/en/function.halt-compiler.php
    */
    function get_halt_data() {
    return file_get_contents(__FILE__, false, null, __COMPILER_HALT_OFFSET__);
    }

    function create_tmp_template($data) {
    $tmpfile = tempnam(sys_get_temp_dir(), 'halt_compiler_template');
    file_put_contents($tmpfile, $data);
    return $tmpfile;
    }

    function parse_template($template, $data) {
    $data = (array) $data;
    ob_start();
    extract($data);
    include $template;
    return ob_get_clean();
    }

    $template = create_tmp_template(get_halt_data());

    $output = parse_template($template, array(
    'title' => 'Hello, World!',
    'paragraph' => 'This is an exciting paragraph of text.',

    ));

    echo $output;
    unlink($template);

    __halt_compiler();

    <!-- Layout Template -->
    <!DOCTYPE>
    <html>
    <head>
    <meta charset="utf-8" />
    <title><?php echo $title; ?></title>
    </head>
    <body>
    <h1><?php echo $title; ?></h1>
    <p><?php echo $paragraph; ?></p>
    </body>
    </html>