|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
phobia Smarty n00b
Joined: 26 Apr 2010 Posts: 3
|
Posted: Mon Apr 26, 2010 6:05 pm Post subject: Retrospective variable setting / parsing |
|
|
Hey smartys!
I'm wondering if Smarty 3 offers any way of setting variable contents in retrospective.
An example will illustrate better:
Code: |
<head>
<title>{$page.title}</title>
</head>
<body>
<!-- the following block will set the $page variable -->
{some_custom_block}
<h1>{$page.title}</h1>
<p>{$page.abstract}</p>
{/some_custom_block}
</body>
|
As you see the $page variable is first set in the block, but I need it's value up in the meta title as well.
Does smarty 3 offer any solution for this? and if not does any of you have any ideas on how to go about making it so?
Thanks in advance! |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Mon Apr 26, 2010 6:52 pm Post subject: |
|
|
Add an assign attribute to your block function which assigns the result to a Smarty variable instead of returning it like
Code: |
if (empty($params['assign'])){
return $result;
} else {
$smarty->assign($params['assign'],$result);
}
|
Then in the template
Code: |
{some_custom_block assign=foo}
<h1>{$page.title}</h1>
<p>{$page.abstract}</p>
{/some_custom_block}
<head>
<title>{$page.title}</title>
</head>
<body>
{$foo}
</body>
|
|
|
Back to top |
|
phobia Smarty n00b
Joined: 26 Apr 2010 Posts: 3
|
Posted: Mon Apr 26, 2010 6:59 pm Post subject: |
|
|
Thanks U.Tews, that's a good suggestion
I'm trying to avoid any extra markup and to moving the markup out of output context, though.
If keeping the markup like in my first post, is there any way of solving it so that the block mid-template can set the value for the variable in the beginning?
I've been looking into preFilters and that I might use them to rewrite the template to be like your suggestion. |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Mon Apr 26, 2010 7:33 pm Post subject: |
|
|
I see no other possibility. |
|
Back to top |
|
phobia Smarty n00b
Joined: 26 Apr 2010 Posts: 3
|
Posted: Tue Apr 27, 2010 11:46 am Post subject: |
|
|
Based on your suggestion and some preFiltering, I've gotten it the way I want it. Thanks for your help |
|
Back to top |
|
|
|
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
|