use compact with add_var_to_front
This commit is contained in:
@@ -26,18 +26,6 @@ include_once(dirname(__FILE__) . '/utils/create_html.php');
|
|||||||
|
|
||||||
function main_shortcode() {
|
function main_shortcode() {
|
||||||
|
|
||||||
# # send styles files by name, without extension .css
|
|
||||||
# add_css_to_front( array(
|
|
||||||
# "mystyle",
|
|
||||||
# ));
|
|
||||||
#
|
|
||||||
# # send scripts files by name, without extension .js
|
|
||||||
# add_scripts_to_front( array(
|
|
||||||
# "myscript",
|
|
||||||
# "myscript2",
|
|
||||||
# "myscript3",
|
|
||||||
# ));
|
|
||||||
|
|
||||||
add_files_to_front( array(
|
add_files_to_front( array(
|
||||||
"mystyle.css",
|
"mystyle.css",
|
||||||
"myscript.js",
|
"myscript.js",
|
||||||
@@ -45,17 +33,14 @@ function main_shortcode() {
|
|||||||
"myscript3.js",
|
"myscript3.js",
|
||||||
));
|
));
|
||||||
|
|
||||||
|
|
||||||
$myvar_1 = "I am one";
|
$myvar_1 = "I am one";
|
||||||
$myvar_2 = "I am two";
|
$myvar_2 = "I am two";
|
||||||
# maybe a future version of php will allow to
|
# compact creates an array containing the variables and their value
|
||||||
# get the name of a variable :
|
# as key => value
|
||||||
# https://wiki.php.net/rfc/nameof
|
# from an array of variables names as strings
|
||||||
# but for now i have to declare both the variable and its name
|
add_var_to_front( compact(
|
||||||
# (more : https://stackoverflow.com/q/255312/9497573)
|
"myvar_1",
|
||||||
add_var_to_front( array(
|
"myvar_2",
|
||||||
"myvar_1" => $myvar_1,
|
|
||||||
"myvar_2" => $myvar_2,
|
|
||||||
));
|
));
|
||||||
|
|
||||||
return create_html( array(
|
return create_html( array(
|
||||||
|
|||||||
@@ -31,7 +31,14 @@ function add_files_to_front($files_arr) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# this function expect an array of key => value
|
||||||
|
# with the key being name of the variable, like this :
|
||||||
|
# 'my_var' => 'value',
|
||||||
|
# simpler way to do it is to use compact when calling the function :
|
||||||
|
# add_var_to_front(compact("var1", "var2", "var3"));
|
||||||
function add_var_to_front($var_array) {
|
function add_var_to_front($var_array) {
|
||||||
|
extract($var_array);
|
||||||
|
|
||||||
foreach ($var_array as $key => $var)
|
foreach ($var_array as $key => $var)
|
||||||
{
|
{
|
||||||
$js_var = 'const ' . $key . ' = ';
|
$js_var = 'const ' . $key . ' = ';
|
||||||
|
|||||||
@@ -1,10 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
function template($file) {
|
|
||||||
$template_dir_path = plugin_dir_path(__DIR__).'html/templates/';
|
|
||||||
include($template_dir_path.$file);
|
|
||||||
}
|
|
||||||
|
|
||||||
# using ob_start() and ob_get_clean()
|
# using ob_start() and ob_get_clean()
|
||||||
# allows to have php expansion inside the html loaded
|
# allows to have php expansion inside the html loaded
|
||||||
# in opposition to the methode file_get_contents()
|
# in opposition to the methode file_get_contents()
|
||||||
|
|||||||
Reference in New Issue
Block a user