| View previous topic :: View next topic |
| Author |
Message |
trparky Smarty Regular
Joined: 25 May 2006 Posts: 36
|
Posted: Sun Feb 19, 2012 5:00 pm Post subject: Declaring a variable late in the template |
|
|
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 |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 4176 Location: Hamburg / Germany
|
Posted: Sun Feb 19, 2012 6:39 pm Post subject: |
|
|
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 |
|
trparky Smarty Regular
Joined: 25 May 2006 Posts: 36
|
Posted: Sun Feb 19, 2012 7:25 pm Post subject: |
|
|
Thanks! I did just that and it works wonderfully. I didn't even know that this was possible. _________________ Tom Parkison |
|
| Back to top |
|
Dice Smarty Rookie
Joined: 11 May 2010 Posts: 14
|
Posted: Tue Apr 24, 2012 8:48 am Post subject: |
|
|
| 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 |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 4176 Location: Hamburg / Germany
|
Posted: Wed Apr 25, 2012 9:06 am Post subject: |
|
|
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 |
|
Dice Smarty Rookie
Joined: 11 May 2010 Posts: 14
|
Posted: Wed Apr 25, 2012 4:05 pm Post subject: |
|
|
| 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 |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 4176 Location: Hamburg / Germany
|
Posted: Wed Apr 25, 2012 4:24 pm Post subject: |
|
|
| You can't assign variables from a subtemplate to a subtemplate which is already rendered. That's impossible. |
|
| Back to top |
|
Dice Smarty Rookie
Joined: 11 May 2010 Posts: 14
|
Posted: Thu Apr 26, 2012 11:08 am Post subject: |
|
|
| 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  |
|
| Back to top |
|
|