View previous topic :: View next topic |
Author |
Message |
oceatoon Smarty Rookie
Joined: 30 Dec 2005 Posts: 9 Location: France
|
Posted: Tue Jan 10, 2006 3:37 pm Post subject: using a smarty variable inside a php tag |
|
|
Hi
I need to call a smarty variable I declared in my main call page, inside a {php} block but this doesn't seem to be interpreted.
I tried putting it as a variable inside the block {php myvar="$myvar"}
but this doesn't work either.
Thanks for the tip
Regards
Tibor |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
|
Back to top |
|
oceatoon Smarty Rookie
Joined: 30 Dec 2005 Posts: 9 Location: France
|
Posted: Tue Jan 10, 2006 5:45 pm Post subject: |
|
|
Great, works like a charm
Thanks |
|
Back to top |
|
budzone Smarty n00b
Joined: 21 Apr 2008 Posts: 2
|
Posted: Mon Apr 21, 2008 12:17 pm Post subject: Literally hours on this. |
|
|
How can it be so difficult to call a simple variable from a smarty template.
I used
{php}
$band_id = "$this->get_template_vars('band_id')";
$source = "./flash/profile_banner/settings.xml";
$destination = "./members/$band_id/settings.xml";
copy($source, $destination);
{/php}
Doesn't work I tried everything. I just want to call 1 simple variable "band_id" so my script can work. I read all the forums and nothing is clear. Everyone says to assign a variable. Well they are already assigned I just need to call it.
PLEASE HELP I FEEL LIKE KICKING SMARTY'S ASS!!! |
|
Back to top |
|
Celeb Administrator
Joined: 17 Apr 2007 Posts: 1025 Location: Vienna
|
Posted: Mon Apr 21, 2008 12:27 pm Post subject: |
|
|
Firstly, lose the quotes:
Code: | $band_id = $this->get_template_vars('band_id'); |
Secondly, this doesn't really look like a Smarty job to me. What does copying files have to do with display logic? Move that task to your php files where they belong.
And last but not least: Don't use {php}{/php} blocks. There always is a better way to do the task at hand. To quote it once again: "If the answer is {php}, you're asking the wrong question." _________________ Darn computers always do what I tell them to instead of what I want them to do. |
|
Back to top |
|
budzone Smarty n00b
Joined: 21 Apr 2008 Posts: 2
|
Posted: Mon Apr 21, 2008 4:59 pm Post subject: |
|
|
thanks for your reply. but I purchased a script from a third party that provides no support unless I pay for it. so I need to figure it out on my own. the program I bought has a script that re-creates tpl files and cfg files into user accounts, this is fine and dandy but I'm adding flash to the user accounts and since the profiles are created on the fly I wanted to add a cp command so that the index.swf and the settings.xml copy over to the account. If I wrote the program I might have done it differently but this, I purchased. from hundreds of files I cannot figure it out for the life of me. So I need a simple fix. I understand the purpose of smarty and how it should be used but I'm not a professional PHP and Smarty programmer so this is the closest way I can think to fix the problem. I did try:
$band_id = $this->get_template_vars('band_id');
without the quotes and gives me a blank page as though an error has occured. when I add the quotes it comes back up. So there is absolutely no way for a smarty variable to be called from a {php} ??? I know alot of people are having the same problem and there is no clear answer. at least not a detailed step by step explanation, so it leaves people like me in the dark. I love Open Source and it's fans, I love what they do and have respect, but I guess since they work free there is usually very poor documentation, I assume they contribute code and they are not copywriters. Anyways, if there is a way I would like to know in details. If someone can help I would greatly appreciate it. Thank you. |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Mon Apr 21, 2008 9:20 pm Post subject: |
|
|
$band_id = $this->get_template_vars('band_id');
Will return the value of the SMARTY variable "band_id". You can use the {debug} tag to check if any value ws assigned to "band_id". |
|
Back to top |
|
shannera Administrator
Joined: 13 Feb 2006 Posts: 802 Location: Edertal, Germany
|
Posted: Tue Apr 22, 2008 7:56 am Post subject: |
|
|
Is there any reason you have to do the copy process in the smarty template with the evil {php} tags, and not the PHP script calling Smarty and this template? |
|
Back to top |
|
lorderunion Smarty Rookie
Joined: 13 Jun 2008 Posts: 8 Location: San Francisco, CA
|
Posted: Fri Jun 13, 2008 8:40 am Post subject: |
|
|
I've never really understood why the {php} tag even exists. The whole point of a templating engine is to keep your business logic and views separate. |
|
Back to top |
|
mattpro Smarty Rookie
Joined: 10 May 2008 Posts: 8
|
Posted: Mon Jun 23, 2008 8:32 am Post subject: |
|
|
This will list all the current template variables and there values:
Code: | {php}
$myVars = $this->get_template_vars();
$this->assign('myVars',$myVars);
{/php}
{foreach from=$myVars key=k item=i}
{$k}: {$i}<br />
{/foreach} |
|
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Mon Jun 23, 2008 3:22 pm Post subject: |
|
|
It is much simple to use the {debug} tag to get all SMARTY variables displayed. |
|
Back to top |
|
ahmeds_link Smarty Rookie
Joined: 18 Aug 2008 Posts: 5
|
Posted: Sun Feb 22, 2009 5:58 pm Post subject: |
|
|
what about variables in {section} like {$result[data].name} how can we use it inside {php} {/php} Tag |
|
Back to top |
|
bimal Smarty Elite
Joined: 19 Apr 2007 Posts: 423
|
Posted: Wed Mar 18, 2009 8:47 pm Post subject: Don't think so |
|
|
ahmeds_link wrote: | what about variables in {section} like {$result[data].name} how can we use it inside {php} {/php} Tag |
I must request anyone to disregard the presence of {php} and {/php} tags! Think about NOT writing these tags.
However, any code snippets you write within these files are called by some function/member of the Smarty object. Hence, if you write <global>, for examlple, global $result;, you can get access to the php's global variables.
Here, I assumed that, you had written:
$smarty->assign('result', $result); where, the names are identical. |
|
Back to top |
|
edank Smarty n00b
Joined: 07 Jul 2009 Posts: 2
|
Posted: Tue Jul 07, 2009 4:58 pm Post subject: same problem |
|
|
hi, in my tpl i have a SELECT built dynamically (inside a loop). after i submit the form its in, i return to the same page, but i want the option that was selected before to be selected now.
for that i tried putting {php} calling a php function that will echo "selected" if necessary, the problem is that i cant seem to pass the {section} variable..
any suggestions? or maybe a different way to do this?
thanks |
|
Back to top |
|
philoertel Smarty Rookie
Joined: 25 Jun 2009 Posts: 14 Location: Chicago, IL
|
Posted: Tue Jul 07, 2009 9:02 pm Post subject: |
|
|
there's a better way :)
Code: | <select name="pie_type">
{foreach from=$types item="type"}
<option value="{$type}"{if $type == $smarty.post.pie_type} selected="selected"{/if}>{$type}</option>
{/foreach}
</select> |
_________________ Phil Oertel
Application Developer
http://www.soliantconsulting.com |
|
Back to top |
|
|