PluginProbe
Force Refresh / 2.1.0
Force Refresh v2.1.0
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 / library / custom / function-handlebars.php

function-handlebars.php in Force Refresh 2.1.0, at library/custom/function-handlebars.php

98 lines 3.0 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", "library/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
18 // If we're adding handlebars, then we need to make sure we are also including the handlebars framework
19 add_script("force-refresh-handlebars", "/node_modules/handlebars/dist/handlebars.min.js");
20
21 // Get the directory of the plugin
22 $directory = get_force_refresh_plugin_directory();
23
24 // Get the location of the Handlebars template
25 $file_location = $directory . FORCE_REFRESH_HANDLEBARS_DIRECTORY . $src;
26
27 if (file_exists($file_location)){
28
29 $handlebar_contents = file_get_contents($file_location);
30
31 echo '<script id="' . $id . '" type="text/x-handlebars-template">';
32 echo $handlebar_contents;
33 echo '</script>';
34 }
35
36 else {
37 error_log("Handlebars template ($file_location) doesn't exist");
38 }
39 }
40
41 /**
42 * Function to render handlebars in PHP. Users the Handlebars framework (via Composer).
43 *
44 * @param string $template_name The name of the template
45 * @param array $replacements_array The replacements
46 * @param boolean $return Whether to return the HTML or print it
47 *
48 * @return string The HTML
49 */
50 function render_handlebars($template_name, $replacements_array = array(), $return = false){
51
52 // Get the directory of the plugin
53 $directory = get_force_refresh_plugin_directory();
54
55 // Add the template directory to the replacements
56 $replacements_array["template_directory_uri"] = get_template_directory_uri();
57
58 $file_location = $directory . FORCE_REFRESH_HANDLEBARS_DIRECTORY . $template_name;
59
60 $file_directory = dirname($file_location);
61
62 if (file_exists($file_location)){
63
64 // Get the file contents
65 $handlebar_contents = file_get_contents($file_location);
66
67 $php = ZordiusLightnCandy\LightnCandy::compile(
68 $handlebar_contents, array(
69 'flags' => ZordiusLightnCandy\LightnCandy::FLAG_RENDER_DEBUG | ZordiusLightnCandy\LightnCandy::FLAG_HANDLEBARSJS
70 )
71 );
72
73 // Get the render function
74 $renderer = ZordiusLightnCandy\LightnCandy::prepare($php);
75
76 $rendered_html = $renderer($replacements_array);
77
78 // If return is true, then return the HTML
79 if ($return){
80
81 return $rendered_html;
82 }
83
84 // Otherwise, echo it
85 else {
86
87 echo $rendered_html;
88
89 }
90
91 }
92
93 else {
94 error_log("Unable to locate handlebars: " . $file_location);
95 }
96 }
97
98 ?>