| 1 |
<?php |
| 2 |
|
| 3 |
namespace JordanLeven\Plugins\ForceRefresh; |
| 4 |
|
| 5 |
use LightnCandy as ZordiusLightnCandy; |
| 6 |
|
| 7 |
// The location of the handlebars directory relative to the plugin root |
| 8 |
define("FORCE_REFRESH_HANDLEBARS_DIRECTORY", "/dist/handlebars/"); |
| 9 |
|
| 10 |
/** |
| 11 |
* Function used to add a handlebars template to the DOM (to be used by JavaScript). |
| 12 |
* |
| 13 |
* @param string $id The ID used to identify the handlebars template |
| 14 |
* @param string $src The location of the handlebars template relative to the predefined handlebars template directory |
| 15 |
*/ |
| 16 |
function add_handlebars($id, $src){ |
| 17 |
// Get the directory of the plugin |
| 18 |
$directory = get_force_refresh_plugin_directory(); |
| 19 |
|
| 20 |
// Get the location of the Handlebars template |
| 21 |
$file_location = $directory . FORCE_REFRESH_HANDLEBARS_DIRECTORY . $src; |
| 22 |
|
| 23 |
if (file_exists($file_location)){ |
| 24 |
|
| 25 |
$handlebar_contents = file_get_contents($file_location); |
| 26 |
|
| 27 |
echo '<script id="' . $id . '" type="text/x-handlebars-template">'; |
| 28 |
echo $handlebar_contents; |
| 29 |
echo '</script>'; |
| 30 |
} |
| 31 |
|
| 32 |
else { |
| 33 |
error_log("Handlebars template ($file_location) doesn't exist"); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Function to render handlebars in PHP. Users the Handlebars framework (via Composer). |
| 39 |
* |
| 40 |
* @param string $template_name The name of the template |
| 41 |
* @param array $replacements_array The replacements |
| 42 |
* @param boolean $return Whether to return the HTML or print it |
| 43 |
* |
| 44 |
* @return string The HTML |
| 45 |
*/ |
| 46 |
function render_handlebars($template_name, $replacements_array = array(), $return = false){ |
| 47 |
|
| 48 |
// Get the directory of the plugin |
| 49 |
$directory = get_force_refresh_plugin_directory(); |
| 50 |
|
| 51 |
// Add the template directory to the replacements |
| 52 |
$replacements_array["template_directory_uri"] = get_template_directory_uri(); |
| 53 |
|
| 54 |
$file_location = $directory . FORCE_REFRESH_HANDLEBARS_DIRECTORY . $template_name; |
| 55 |
|
| 56 |
$file_directory = dirname($file_location); |
| 57 |
|
| 58 |
if (file_exists($file_location)){ |
| 59 |
|
| 60 |
// Get the file contents |
| 61 |
$handlebar_contents = file_get_contents($file_location); |
| 62 |
|
| 63 |
$php = ZordiusLightnCandy\LightnCandy::compile( |
| 64 |
$handlebar_contents, array( |
| 65 |
'flags' => ZordiusLightnCandy\LightnCandy::FLAG_RENDER_DEBUG | ZordiusLightnCandy\LightnCandy::FLAG_HANDLEBARSJS |
| 66 |
) |
| 67 |
); |
| 68 |
|
| 69 |
// Get the render function |
| 70 |
$renderer = ZordiusLightnCandy\LightnCandy::prepare($php); |
| 71 |
|
| 72 |
$rendered_html = $renderer($replacements_array); |
| 73 |
|
| 74 |
// If return is true, then return the HTML |
| 75 |
if ($return){ |
| 76 |
|
| 77 |
return $rendered_html; |
| 78 |
} |
| 79 |
|
| 80 |
// Otherwise, echo it |
| 81 |
else { |
| 82 |
|
| 83 |
echo $rendered_html; |
| 84 |
|
| 85 |
} |
| 86 |
|
| 87 |
} |
| 88 |
|
| 89 |
else { |
| 90 |
error_log("Unable to locate handlebars: " . $file_location); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
?> |