Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

Retrospective variable setting / parsing

 
Post new topic   Reply to topic    Smarty Forum Index -> Smarty 3
View previous topic :: View next topic  
Author Message
phobia
Smarty n00b


Joined: 26 Apr 2010
Posts: 3

PostPosted: Mon Apr 26, 2010 6:05 pm    Post subject: Retrospective variable setting / parsing Reply with quote

Hey smartys! Smile

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
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Mon Apr 26, 2010 6:52 pm    Post subject: Reply with quote

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
View user's profile Send private message
phobia
Smarty n00b


Joined: 26 Apr 2010
Posts: 3

PostPosted: Mon Apr 26, 2010 6:59 pm    Post subject: Reply with quote

Thanks U.Tews, that's a good suggestion Smile

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
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Mon Apr 26, 2010 7:33 pm    Post subject: Reply with quote

I see no other possibility.
Back to top
View user's profile Send private message
phobia
Smarty n00b


Joined: 26 Apr 2010
Posts: 3

PostPosted: Tue Apr 27, 2010 11:46 am    Post subject: Reply with quote

Based on your suggestion and some preFiltering, I've gotten it the way I want it. Thanks for your help Smile
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    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