0

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>
Mortimer
  • 300
  • 5
  • 11
  • Why are you mixing placeholders like `{{ title }}` _and_ PHP variables/code? You usually use placeholders to _remove_ PHP from templates. Use one or the other. Also, creating temporary files that you include and then remove is _very_ inefficient. Just leave them as php files and include them directly. – M. Eriksson Jan 18 '20 at 13:04
  • When you say "global variables", do you mean the super global `$_COOKIE`? Or do you mean some other variable? Please edit your question to show us _all_ relevant code (including the method signature), a more detailed explanation of the issue, example of how the code is called/used, example data, expected output and what you're currently getting. The question is currently too unclear. – M. Eriksson Jan 18 '20 at 13:11
  • @MagnusEriksson 1. {{ title }} is placeholder for title of widget. And it's replaced with title passed to function by user. I think it's the most convenient and read able way. If you know a better way please share and show answer don't just point out. 2. If I were to _just_ include file then I wont be able to replaced the {{ title }}. I f I were to get the content and replace the title then `echo` file the php code wont get parsed. So I create new file with changed title and then include it so the php will get parsed. – Mortimer Jan 18 '20 at 14:26
  • 1
    If you go with my suggestion in my first comment, you wouldn't have `{{ title }}` in the widget. You would have `= $title ?>` instead (just stick to PHP instead of mixing it with placeholders), which means that you _could_ "just include" the file. – M. Eriksson Jan 18 '20 at 23:04
  • Ok mate, how do imagine this would work? Cuz I can't see it. If I had $title variable in each widget (on page might be tons of the same widget but different variations) then I must have "global" $title which I would change for each widget (I have more placeholders then {{ title }}). So if you could provide any example how this might work, I would be delighted. – Mortimer Jan 19 '20 at 10:57
  • So, in what way is `$dic` a "global variable"? If you call a method, you should pass the variables needed in that method. Also, where does the `$title` variable you're using in the method come from since you don't seem to pass it to the method? You should read through [variable scopes](https://www.php.net/manual/en/language.variables.scope.php) in the manual. – M. Eriksson Jan 19 '20 at 11:41
  • Never mind mate I figure it out. All thanks to your = ?>. Give me a minute and I give will write answer to my own question. – Mortimer Jan 19 '20 at 12:23
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/206223/discussion-between-mortimer-and-magnus-eriksson). – Mortimer Jan 19 '20 at 13:08

1 Answers1

1

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.

Mortimer
  • 300
  • 5
  • 11