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.
Declaring a variable late in the template

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


Joined: 25 May 2006
Posts: 36

PostPosted: Sun Feb 19, 2012 5:00 pm    Post subject: Declaring a variable late in the template Reply with quote

I have a system in which I have a main template, that template is the core of the page. Later in that same template I include another template.

Kind of like this...
<html>
<head>
<title>Site Title</title>
</head>
<body>
Some Content Goes Here
{include file=$page}
</body>
</html>

The $page variable is populated outside of Smarty.

What I want to do is this...

MAIN TEMPLATE FILE
<html>
<head>
<title>Site Title -- {$pageTitle}</title>
</head>
<body>
Some Content Goes Here
{include file=$page}
</body>
</html>

SUB-TEMPLATE FILE
{assign var=pageTitle value="Page #2 Title" scope="global"}
Some More Content

Is it even possible to declare a variable in a sub-template to be used in the main template before the sub-template is included? Or am I going to be forced to register an output filter?
_________________
Tom Parkison
Back to top
View user's profile Send private message Visit poster's website
U.Tews
Administrator


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

PostPosted: Sun Feb 19, 2012 6:39 pm    Post subject: Reply with quote

No it's not possible to use a variable before it's declared.

Perhaps you should have a closer look into template inheritance

http://www.smarty.net/inheritance
Back to top
View user's profile Send private message
trparky
Smarty Regular


Joined: 25 May 2006
Posts: 36

PostPosted: Sun Feb 19, 2012 7:25 pm    Post subject: Reply with quote

Thanks! I did just that and it works wonderfully. I didn't even know that this was possible.
_________________
Tom Parkison
Back to top
View user's profile Send private message Visit poster's website
Dice
Smarty Rookie


Joined: 11 May 2010
Posts: 14

PostPosted: Tue Apr 24, 2012 8:48 am    Post subject: Reply with quote

U.Tews wrote:
No it's not possible to use a variable before it's declared.

Perhaps you should have a closer look into template inheritance

http://www.smarty.net/inheritance
Is there anyway to do this while keeping the same structure. I've been taking a look into the variable scopes and how the variables are mapped to individual templates. Will it be possible for me to say lets say:

I am currently in right.tpl let me add variable into header.tpl by tapping into the scope of header.tpl?
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Wed Apr 25, 2012 9:06 am    Post subject: Reply with quote

Template code and its subtemplates are rendered in the order they appear.

If you assign a varibale with root scope in a subtemplate it can be seen by all subtemplate rendered afterwards but ofcause not by templates rendered before.
Back to top
View user's profile Send private message
Dice
Smarty Rookie


Joined: 11 May 2010
Posts: 14

PostPosted: Wed Apr 25, 2012 4:05 pm    Post subject: Reply with quote

U.Tews wrote:
Template code and its subtemplates are rendered in the order they appear.

If you assign a varibale with root scope in a subtemplate it can be seen by all subtemplate rendered afterwards but ofcause not by templates rendered before.
Im aware of that, but im trying to change that second rule. How would i go about achieving this? How much of a rewrite is required? Or is there some magic method i can use that will help me out. I remember someone mentioned template_object, but I'm not sure how that works?
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Wed Apr 25, 2012 4:24 pm    Post subject: Reply with quote

You can't assign variables from a subtemplate to a subtemplate which is already rendered. That's impossible.
Back to top
View user's profile Send private message
Dice
Smarty Rookie


Joined: 11 May 2010
Posts: 14

PostPosted: Thu Apr 26, 2012 11:08 am    Post subject: Reply with quote

U.Tews wrote:
You can't assign variables from a subtemplate to a subtemplate which is already rendered. That's impossible.
Ok no problemo ill try it with a postfilter Smile
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