I have created class for "creating" widgets. You call static function, pass variables and have your sweet widget.
For now it worked because I have been using variables created in widget file. But now I tried to use some "global" variable and it doesn't see it.
By "global" I mean my defined global variables (not phps) like $dic which is an object of dictionary class.
Why is that? I reallllyyy don't want to create those variables in each widget.
I think it's because I am creating temporary file. (I need to replace {{ title }}
with actual title so I'm getting widget code, replacing title, create new tmp file with replaced tiltle and include this, then delete)
Global variable:
$dic = new Dictionary(isset($_COOKIE["language"]) ? htmlspecialchars($_COOKIE["language"]) : _LANG); // THE GLOBAL VARIABLE
Widget code:
<span>{{ title }}</span>
<form action="<?php echo Path::GetCurrentURL(); ?>" method="post">
<?php // for some reason it doesn't see any global variables so you have to create then once more in widgets which drives me nuts ugh?>
<input type="submit" name="logoutAdm" value="<?php $dic->Translate("Log out"); ?>">
</form>
Include function:
{
$path = Path::Widgets("ShopPanelTitle.php");
if (file_exists($path)) {
$widget = file_get_contents($path);
$widget = str_replace("{{ title }}", $title, $widget);
$pathTmp = Tools::TMPName(".php",Path::TMP(""));
echo $pathTmp;
$file = fopen($pathTmp, "w+");
fwrite($file,$widget);
fclose($file);
// for some reason it doesn't see any global variables so you have to create then once more in widgets
include $pathTmp;
unlink($pathTmp);
}
}
How I call the function:
<?php Widgets::ShopPanelTitle($dic->Translate("Main",true)) ?>
There is no more relevant code. If you want to see all the code which is used, the question would get awfully long and would get sued for revealing company secrets :/.
Path::Widgets - return path to widget folder
Tools::TMPName - return random name
What I get:
<span>Title</span>
<form action="currentPage.php" method="post">
</form>
What I want to get:
<span>Title</span>
<form action="currentPage.php" method="post">
<input type="submit" name="logoutAdm" value="Log out">
</form>
Thanks to Magnus Eriksson help I found out how stupid my problem was.
I replaced my {{ title }} placeholder with $title and noticed it works just fine. So the problem was with scope, I had to tell function to not use local $dic variable but to "look out" for "global" $dic.
Widget code:
public static function ShopPanelTitle($title)
{
global $dic;
$path = Path::Widgets("ShopPanelTitle.php");
if (file_exists($path)) {
$title = $dic->Translate($title,true);
include $path;
} else {
Tools::JSLog("Widget file " . $path . " doesn't exist.");
}
}
Widget:
<span><?= $title ?></span>
<form action="<?php echo Path::GetCurrentURL(); ?>" method="post">
<input type="submit" name="logoutAdm" value="<?= $dic->Translate("Log out"); ?>">
</form>
Widget Call:
<?php Widgets::ShopPanelTitle("Main") ?>
So I guess I have some reading to do in topic of variable scope.
Once more thanks to Magnus Eriksson, very helpful.