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} in Smarty3 not working as expected

 
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 -> Smarty 3
View previous topic :: View next topic  
Author Message
alsal
Smarty n00b


Joined: 06 Jan 2007
Posts: 2

PostPosted: Sun May 30, 2010 9:18 pm    Post subject: {assign} in Smarty3 not working as expected Reply with quote

I've split up a page into several templates. One being the head.tpl for the <head> part of the html.

I'd like to assign meta tags dynamically based on the content file being included. I'm using rewrite rules to redirect everything to index.php then dynamically building the page based on the request.

so in head.tpl is the line:
<meta name="description" content="{$META_DESC}">

then in content.tpl I put:
{assign var=META_DESC value="some description">

the shell.tpl file would include these 2 templates in the right places.

The resulting text is:
<meta name="description" content="">

unless I place the {assign} in shell.tpl, then it works:
<meta name="description" content="some description">


Placing the {assign} in shell.tpl is not acceptable.

Can anyone offer some help?
Back to top
View user's profile Send private message
douglassdavis
Smarty Junkie


Joined: 21 Jan 2008
Posts: 541

PostPosted: Mon May 31, 2010 3:27 am    Post subject: Re: {assign} in Smarty3 not working as expected Reply with quote

vars are only available after they have been assigned.

since head.tpl displays first, the $META_DESC variable has not been initialized yet.

For a situation like this, the way I do it is... fetch your content.tpl file, which determines what it wants to add to the <head>, save that, pass it to your shell.tpl file.

I have a plug in function that is called like:
{html_head}
<meta name="description" content="some description">
{/html_head}

It collects the values passed to it in a global PHP var to be output later when the <head> is output.
Back to top
View user's profile Send private message
Lemon Juice
Smarty Pro


Joined: 24 May 2006
Posts: 109

PostPosted: Mon May 31, 2010 11:07 am    Post subject: Reply with quote

Or use template inheritance which is available in Smarty 3 - {extends} and {block}. This is exactly to solve the problem you are facing.
Back to top
View user's profile Send private message
trparky
Smarty Regular


Joined: 25 May 2006
Posts: 36

PostPosted: Thu Jun 10, 2010 1:46 am    Post subject: Reply with quote

Sounds like you are trying to put too much logic in the display layer. Try doing the assigning of data before you display the parent template so that you don't make assign calls in the template code.

So do this...

$smarty->assign("META_DESC", "some description");
$smarty->display("parent_template.tpl");
_________________
Tom Parkison
Back to top
View user's profile Send private message Visit poster's website
PabloOrlow
Smarty n00b


Joined: 25 Apr 2011
Posts: 1

PostPosted: Mon Apr 25, 2011 6:06 pm    Post subject: Reply with quote

Smarty 3 has a feature of template inheritance. This might help in solving the problem at hand. Do try it!

Do you have any opinion on the Wealthy Affiliate too?


Last edited by PabloOrlow on Sat Jan 23, 2016 7:03 pm; edited 1 time in total
Back to top
View user's profile Send private message
rodneyrehm
Administrator


Joined: 30 Mar 2007
Posts: 674
Location: Germany, border to Switzerland

PostPosted: Mon Apr 25, 2011 6:47 pm    Post subject: Reply with quote

Consider using template inheritance. Your templates will gain on readability and performance. Passing data to an extended template can be achieved quite easily: https://gist.github.com/832525
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 -> Smarty 3 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