Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Smarty 3.0.8 ==> 3.1.33 Assign Issue

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Language: German
View previous topic :: View next topic  
Author Message
Grizzly
Smarty Pro


Joined: 15 Apr 2011
Posts: 172
Location: Germany

PostPosted: Fri Jan 04, 2019 10:03 pm    Post subject: Smarty 3.0.8 ==> 3.1.33 Assign Issue Reply with quote

Hallo zusammen,

AnrDaemon wird mich für diese Frage wahrscheinlich Köpfen, aber ich verzweifel langsam.

Ich bin dabei Smarty 3.0.8 auf smarty 3.1.33 zu (1:1 Code) migrieren (ja, schande über mich, aber es ging nicht früher) und habe dabei ein gewaltiges Problem mit dem Assignen von Variablen. Eine Variable die angeblich assigned ist, ist es im Template nicht und ich weiß nicht warum.

Ich habe ein Singleton für meine Smarty-Instance und ein selbstgeschriebenes "Function"-Plugin.

Meine Templates sehen so aus:

-> master.tpl
--->a.tpl
--->b.tpl
--->....tpl
---> pagelayout.tpl
------> module.tpl

Ich rufe in dem module.tpl dieses "Function"-Plugin auf. Dieses holt sich das singleton und assigned eine Variable.

Laut dem Debugger ist diese auch im Smarty-Scope vorhanden, aber trotzdem hilft meine

Code:
{if isset($additionalScripts)}


Abfrage nicht, da diese angeblich nicht vorhanden ist in der master.tpl


Hier noch ein Bild. Links die master.tpl und rechts die Debug-Konsole...



EDIT:

Ich habe jetzt was herausgefunden.

Alle Seiten erben bei mir von eine Klasse "Page". Wenn ich in dieser Klasse die "append"-Methode einmal ausführe mit dem additionalScripts Parameter, dann werden auch auf einmal die anderen Parameter hinzugefügt. Aber warum? Es hat doch in Smarty 3.0.8 auch funktioniert. Was hat sich geändert und kann man das irgendiwe umgehen?

EDIT 2:

Okay, ich habe es nun, aber wie kann ich es beheben? Es liegt anscheinend daran, dass das "append"/"assign" NACH dem display("master.tpl") ausgeführt wird. Kann man das irgenwie umgehen?
Back to top
View user's profile Send private message Visit poster's website
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Tue Jan 08, 2019 2:24 pm    Post subject: Reply with quote

If I understand your description right…
You're using template function to assign variables back to the template?
I can see why it was changed, as this have a possibility of corrupting the template scope, which is hard to detect/debug.
You can do better if you write a repository kind of function/object which would make the data available directly from it.
Back to top
View user's profile Send private message
Grizzly
Smarty Pro


Joined: 15 Apr 2011
Posts: 172
Location: Germany

PostPosted: Tue Jan 08, 2019 3:20 pm    Post subject: Reply with quote

I am not sure if you understood me and I understood you correct.

Currently I found out, that all is working fine, when I assign/append a "dummy" value to this variable BEFORE the "display"-Methode will be executing. When I do it after, not even one is appended.

My current Logic looks like this:


  • MySmarty extends Smarty and is a singleton
  • Page-Class (Abstract) displayes the master.tpl (via MySmarty-Singleton)
  • ContentPage extends Page-Class and displayes pagelayout.tpl (via MySmarty-Singleton)
  • Somewhere in a template or subtemplate the Function-PlugIn is called and this plugin use the MySmarty-Singleton instance to append the variable value instead of Smarty_Template_Data $template (but with this varibale it is not working too)


My Question:
Is it possible to assign and render the Varibale AFTER the display-Method was executed or is it possible to assign a Variable in "Global"-Scope or a better solution?

BTW: I did not understand your suggestion. Could you explain it (maybe with a code example)
Back to top
View user's profile Send private message Visit poster's website
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Thu Jan 10, 2019 3:43 pm    Post subject: Reply with quote

You can't really make Smarty a singleton, it's easily broken by calling ->createTemplate().
As to the code example.
Instead of
Code:
function xxx($smarty, $params)
{
$smarty->assign(...$params)
}

{xxx stuff}
{$stuff}


do something akin to

Code:
class ReadRepositoryProxy
{
  function access(...$params)
  {
    …
    return $data;
  }

  …
}

$proxy = new ReadRepositoryProxy(…);

$smarty->registerPlugin('modifier', 'access', [$proxy, 'access'], …);

{"stuff"|access:params}
Back to top
View user's profile Send private message
Grizzly
Smarty Pro


Joined: 15 Apr 2011
Posts: 172
Location: Germany

PostPosted: Thu Jan 10, 2019 6:56 pm    Post subject: Reply with quote

Thank you for the code, but it does not work.

My "Workaround" is now to create "dummy" assignements (without value) before the

Code:
$smarty->display("master.tpl")


is executed. But it is not really nice.

Is it Possible to assign a Variable in the master.tpl (or in child-template) AFTER the

Code:
$smarty->display("master.tpl")


is executed? This is working fine in Smarty 3.0.0.8
Back to top
View user's profile Send private message Visit poster's website
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Fri Jan 11, 2019 9:15 am    Post subject: Reply with quote

It was just a concept Smile Not an actually working code.
If you can show your attempted implementation and outline your issues, maybe we can make it work?
Back to top
View user's profile Send private message
Grizzly
Smarty Pro


Joined: 15 Apr 2011
Posts: 172
Location: Germany

PostPosted: Fri Jan 11, 2019 9:58 am    Post subject: Reply with quote

Quote:
It was just a concept Smile Not an actually working code.


Yes, I know it. The Code in general is working, but did not fix my issue.

I guess I got it now (this thread had help me https://www.smarty.net/forums/viewtopic.php?p=75689):


I removed the "singleton" in MySmarty-Class and removed all Code-References to get the Instance via Static Method. In the next step I had change the "BasePage"-Class and create a new MySmarty-Instance and create the masterpage-template:

Code:

$this->SmartyInstance = new MySmarty();
$this->SmartyInstance->assign("Page", $this);
$this->Page = this->SmartyInstance->createTemplate($this->MasterPage);
$this->Page->display();


I had override the standard Smarty-Method "display" with following code:


Code:

public function display($template = null, $cache_id = null, $compile_id = null, $parent = null)
   {
      $tpl = $this->createTemplate($template, $cache_id, $compile_id, $parent ? $parent : $this, false);
      $tpl->display();
   }


And no, it is possible to execute the original Smarty-Display-Method

Code:
public function smartyDisplay($template = null, $cache_id = null, $compile_id = null, $parent = null)
{
   parent::display($template, $cache_id, $compile_id, $parent)
}


Now it should working fine. (Not tested now)

BTW: My Page-Instance is a singleton, not smarty. And because of all pages are inherits from "BasePage" we can use the Smarty-Instance and the Template-Instance Smile

Code:

//In custom Page-Class extends BasePage
$this->Page->assign(...); //TemplateInstance
$this->SmartyInstance //Smarty Instance
Back to top
View user's profile Send private message Visit poster's website
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Fri Jan 11, 2019 7:11 pm    Post subject: Reply with quote

That also works. I use it to embed blocks not directly relevant to page contents. Like external resources.
Back to top
View user's profile Send private message
Grizzly
Smarty Pro


Joined: 15 Apr 2011
Posts: 172
Location: Germany

PostPosted: Fri Jan 11, 2019 8:38 pm    Post subject: Reply with quote

Thank you for your help AnrDaemon.

BTW: Are you one of the smarty developer?
Back to top
View user's profile Send private message Visit poster's website
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Mon Jan 14, 2019 4:30 pm    Post subject: Reply with quote

Not really, no. I'm helping where I can, but every time I get into real templating code, I get lost very quick >.>
Though I haven't tried any serious debugging since I've got into VS Code.
Back to top
View user's profile Send private message
Grizzly
Smarty Pro


Joined: 15 Apr 2011
Posts: 172
Location: Germany

PostPosted: Wed Jan 16, 2019 7:44 am    Post subject: Reply with quote

Oh okay.

Nice, another Visual Studio User Smile I like.

I am not a PHP developer, but C# and VS is my daily tool.

For my CMS I use smarty and as an IDE I use Visual Studio Enterprise with the "PHP Tools for Visual Studio" it is really nice (available as "Preview" for VS Code as well). I can recommend this tool
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Language: German All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP