Skip to content

Instantly share code, notes, and snippets.

@jxxe
Created September 23, 2020 03:20
Show Gist options
  • Save jxxe/7f6c55f938ba75eb64b5e2d1d6425853 to your computer and use it in GitHub Desktop.
Save jxxe/7f6c55f938ba75eb64b5e2d1d6425853 to your computer and use it in GitHub Desktop.
PHP Templates
<?php
$data = file_get_contents('template.html');
$data = preg_replace_callback( '/{(.*)}/', 'replacementCallback', $data );
function replacementCallback($subject) {
$subject = 'return ' . trim( $subject[1] );
if( substr($subject, -1) !== ';' ) {
$subject .= ';';
}
$subject = eval($subject);
return $subject;
}
echo $data;

This is just a proof of concept for PHP-enabled HTML files. Instead of <?php and ?> tags in a PHP file, you could use {} in an HTML file. This would allow you to create static HTML templates that use PHP functions. I'm not sure why you would want to do this instead of using normal PHP, but you could!

Sample HTML:

<p>Today's date is { date('F j, Y') }</p>

And uh..that's the only use I can think of.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment