Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.
Dedicated server web hosting provided by Guru-host.eu.
assign variable globally in smarty action

 
Post new topic   Reply to topic    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
jebediah
Smarty Rookie


Joined: 28 Jan 2012
Posts: 16

PostPosted: Sun Aug 05, 2012 10:18 am    Post subject: assign variable globally in smarty action Reply with quote

Hello everyone,

I've got the following 'setup':

index.tpl
Code:

{include file='header.tpl'}
  {$user.username}
{include file='footer.tpl'}


header.tpl
Code:

{get_current_user}<html>
  <head>
    <!-- all head definitions here -->
  </head>
  <body>
    <header>
      Welcome back {$user.username}!
    </header>
    <article>



footer.tpl
Code:

    </article>
  </body>
</html>


With a custom smarty function get_current_user:
Code:

  function get_current_user($params, &$smarty){
    $smarty->assign('user', array('username' => 'Jeb'));
  }


The 'problem' is that the user variable is only visible within the header.tpl and not in the index.tpl. I know that what I'm asking for seems like a bad approach and is misusing smarty's template hierarchy, BUT please bare with me Smile (we're using a template driven rapid prototyping approach, i.e. the template controls it's own data, while the application only controls the logic. This way the template can specify where to get the data from).

One way around that would be 'mimicking' the {assign} behaviour with the scope='root' attribute set, like so:


Code:

  function get_current_user($params, &$smarty){
    while($smarty!=null)
      $smarty->assign('user', array('username' => 'Jeb'));
      $smarty = $smarty->parent;
    }
  }


(please ignore the stupidity of this example and the re-initialisation of the array in every iteration, etc.).

This works as intended and the $user variable is visible in the index.tpl.

Here come the questions Smile:

a.) Is there a BETTER way to do this
b.) Is this approach going to be supported in future versions of smarty (i.e. will smarty continue to support the {assign scope='root'} tag/attribute)

Thanks and bests,
Jeb
Back to top
View user's profile Send private message
jebediah
Smarty Rookie


Joined: 28 Jan 2012
Posts: 16

PostPosted: Sun Aug 05, 2012 10:20 am    Post subject: Re: assign variable globally in smarty action Reply with quote

P.P.S: does anyone know the reason why the following does not work:

Code:

  function get_current_user($params, &$smarty){
    while($smarty->parent!=null)
      $smarty = $smarty->parent;
    }
    $smarty->assign('user', array('username' => 'Jeb'));
  }


i.e. ONLY assign the variable to the 'highest up the hierarchy' smarty template in order to avoid assignment in each step of the hieararchy. When doing this, the variable is neither visible to header.tpl (from where the function is called) nor to index.tpl. I thought that variables are 'inherited downwards'?

Thanks and bests,
Jeb
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Tue Aug 14, 2012 8:32 am    Post subject: Reply with quote

Smarty does create a local scope of variables by copying them from the parent when the template is called. So an assign within a template is normally not seen by it's calling template and an assign to an outerlevel scope is not inherited downwards in existing scopes.

Currently you need to assign in each level of hierachy.

But this problem is already addressed in upcomming major release 3.2.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    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