PluginProbe
Force Refresh / 2.1.2
Force Refresh v2.1.2
3.2.1 3.2.0 3.1.2 3.1.1 3.1.0 trunk 1.0.0 1.1.0 1.1.1 1.1.2 1.2.0 2.0.0 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.10.0 2.10.1 2.10.2 2.11.0 2.11.1 2.12.0 All 54 releases
force-refresh / includes / function-handlebars.php

function-handlebars.php in Force Refresh 2.1.2, at includes/function-handlebars.php

94 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 ?>