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

{assign} scope set via option rather than in every {assign}

 
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 -> General
View previous topic :: View next topic  
Author Message
cyber
Smarty Regular


Joined: 08 Jun 2006
Posts: 51

PostPosted: Tue Nov 24, 2015 5:13 am    Post subject: {assign} scope set via option rather than in every {assign} Reply with quote

I have a small disaster on my hands. Mea Culpa. Quite a few months ago I changed to the then most recent Smarty version from a rather old version and everything appeared to work fine. Although, I did not see that I had broken my pages' meta data. I have been experiencing SEO issues but only now discovered that I screwed up the meta data.

In every template (and there are LOTS of them) I assign the title and description etc. : {assign var="title" value="BlahBlahBlah"}
This variable would be carried over to the last template loaded (index.tpl) where it would be used. However that doesn't seem to be the case anymore.

I looked up {assign} and notice the use of scope="global" (among others), which in this case fixes the issue.

This must not have been present yet in the old Smarty version that I was using, hence the problems now.

To avoid changing 100's of templates, also 'owned' by translators etc. I was wondering if I can't set the scope to 'global' after I make an instance of the Smarty Class ?

Then I don't have to edit all tpl files !

A speed response would be appreciated !
Thanks
Back to top
View user's profile Send private message
cyber
Smarty Regular


Joined: 08 Jun 2006
Posts: 51

PostPosted: Tue Nov 24, 2015 5:46 am    Post subject: Reply with quote

PS. after creation of the instance I assign general values, which normally were overwritten via {assign}, but not anymore:

$smarty->assign('title', "standard title");
$smarty->assign('description', "standard description");

I was hoping I could pass 'global' here somehow already, so that other templates would know the variable exists already and is global ... if it works that way ... but that doesn't seem to be the case
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Wed Nov 25, 2015 5:26 am    Post subject: Reply with quote

{assign} creates a variable which is known in the current template and in all sub templates included with {include}. This behavior has not been changed.

So how does your template structure look like?
Where is the variable not seen?

Which was your old Smarty version, which is the new one?
Back to top
View user's profile Send private message
cyber
Smarty Regular


Joined: 08 Jun 2006
Posts: 51

PostPosted: Wed Nov 25, 2015 5:32 am    Post subject: Reply with quote

I went from Smarty 2.6.22 to Smarty 3.1.27

For instance:

Code:
if(!$smarty->isCached('index.tpl'))
   {
   $content = $smarty->fetch('about.tpl');
   $smarty->assign('content',$content);
   }
$smarty->display('index.tpl');


{assign} is used in about.tpl to assign a title and description, which is used in index.tpl
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Wed Nov 25, 2015 6:14 am    Post subject: Reply with quote

I see.

In Smarty 2 an {assign} in a main template ('about.tpl') did also set that variable in the $smarty object. That is no longer true.
I see only the solution to use {assign ... scope=global}.
Sorry
Back to top
View user's profile Send private message
cyber
Smarty Regular


Joined: 08 Jun 2006
Posts: 51

PostPosted: Wed Nov 25, 2015 8:37 am    Post subject: Reply with quote

OK thanks !
I ended up doing the work already !
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Wed Nov 25, 2015 1:06 pm    Post subject: Reply with quote

Why don't you use CACHING_LIFETIME_SAVED and just {include} the relevant templates?
May be {include inline} even?
Back to top
View user's profile Send private message
lastguru
Smarty n00b


Joined: 14 Dec 2015
Posts: 3

PostPosted: Mon Dec 14, 2015 7:09 pm    Post subject: Reply with quote

I find that this behavior changed in 3.1.28 from quite a recent version (3.1.16): {assign} do not set variable in $smarty object. Indeed, setting scope in {assign} to "global" helps, however, {assign} is not the only way to assign a variable. For example, I use {capture}, and there is no scope there. Please make a possibility to set default scope to global.
Back to top
View user's profile Send private message
lastguru
Smarty n00b


Joined: 14 Dec 2015
Posts: 3

PostPosted: Mon Dec 14, 2015 8:22 pm    Post subject: Reply with quote

Done some more experiments...

This has worked before (3.1.16):
Code:
$output = $smarty->fetch('eval:' . $section_code);


Now (3.1.28 ) the same result can only be achieved like this:
Code:
$data = $smarty->createData($smarty);
$output = $smarty->fetch('eval:' . $section_code, $data);
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Tue Dec 15, 2015 1:12 am    Post subject: Reply with quote

I do not understand your last example. What results can be achieved by the data object?
Back to top
View user's profile Send private message
lastguru
Smarty n00b


Joined: 14 Dec 2015
Posts: 3

PostPosted: Tue Dec 15, 2015 1:32 am    Post subject: Reply with quote

1. Smarty engine evaluates a template code (lets call it CodeA).

2. There is a function call inside CodeA, that evaluates an additional template code (CodeB).

3. No Smarty variable scopes have been used in any of the code.

4. Before evaluating CodeA, there are some variable definitions ($smarty->assign() - without any data objects). These variables are freely available in both CodeA and CodeB.

5. If there are variables set in CodeA, they seem to be added into $smarty object, however they are not accessible inside CodeB unless assigned in CodeA as global ({assign scope="global"}). As there is no scope for {capture} results, there is no way to make CodeA {capture} results to be seen in CodeB just by modifying the templates. There was no such issue in 3.1.16 - all variables set in CodeA were usable in CodeB.

6. However, if in 3.1.28, in CodeB evaluation function (the one that is called from CodeA), I make a data object based on $smarty object, and use it to evaluate CodeB (as presented in the examples above), everything starts to work as before.
Back to top
View user's profile Send private message
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 -> General 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