Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

using a smarty variable inside a php tag
Goto page 1, 2  Next
 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Tips and Tricks
View previous topic :: View next topic  
Author Message
oceatoon
Smarty Rookie


Joined: 30 Dec 2005
Posts: 9
Location: France

PostPosted: Tue Jan 10, 2006 3:37 pm    Post subject: using a smarty variable inside a php tag Reply with quote

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
View user's profile Send private message Visit poster's website
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Tue Jan 10, 2006 4:20 pm    Post subject: Reply with quote

There is an api method for this:
http://smarty.php.net/manual/en/api.get.template.vars.php

Don't forget that within a {php} block, you access the current Smarty object instance using $this.
Back to top
View user's profile Send private message
oceatoon
Smarty Rookie


Joined: 30 Dec 2005
Posts: 9
Location: France

PostPosted: Tue Jan 10, 2006 5:45 pm    Post subject: Reply with quote

Great, works like a charm
Thanks
Back to top
View user's profile Send private message Visit poster's website
budzone
Smarty n00b


Joined: 21 Apr 2008
Posts: 2

PostPosted: Mon Apr 21, 2008 12:17 pm    Post subject: Literally hours on this. Reply with quote

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


Joined: 17 Apr 2007
Posts: 1025
Location: Vienna

PostPosted: Mon Apr 21, 2008 12:27 pm    Post subject: Reply with quote

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


Joined: 21 Apr 2008
Posts: 2

PostPosted: Mon Apr 21, 2008 4:59 pm    Post subject: Reply with quote

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


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

PostPosted: Mon Apr 21, 2008 9:20 pm    Post subject: Reply with quote

$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
View user's profile Send private message
shannera
Administrator


Joined: 13 Feb 2006
Posts: 802
Location: Edertal, Germany

PostPosted: Tue Apr 22, 2008 7:56 am    Post subject: Reply with quote

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


Joined: 13 Jun 2008
Posts: 8
Location: San Francisco, CA

PostPosted: Fri Jun 13, 2008 8:40 am    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail AIM Address
mattpro
Smarty Rookie


Joined: 10 May 2008
Posts: 8

PostPosted: Mon Jun 23, 2008 8:32 am    Post subject: Reply with quote

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


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

PostPosted: Mon Jun 23, 2008 3:22 pm    Post subject: Reply with quote

It is much simple to use the {debug} tag to get all SMARTY variables displayed.
Back to top
View user's profile Send private message
ahmeds_link
Smarty Rookie


Joined: 18 Aug 2008
Posts: 5

PostPosted: Sun Feb 22, 2009 5:58 pm    Post subject: Reply with quote

what about variables in {section} like {$result[data].name} how can we use it inside {php} {/php} Tag
Back to top
View user's profile Send private message
bimal
Smarty Elite


Joined: 19 Apr 2007
Posts: 423

PostPosted: Wed Mar 18, 2009 8:47 pm    Post subject: Don't think so Reply with quote

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
View user's profile Send private message Visit poster's website
edank
Smarty n00b


Joined: 07 Jul 2009
Posts: 2

PostPosted: Tue Jul 07, 2009 4:58 pm    Post subject: same problem Reply with quote

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


Joined: 25 Jun 2009
Posts: 14
Location: Chicago, IL

PostPosted: Tue Jul 07, 2009 9:02 pm    Post subject: Reply with quote

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
View user's profile Send private message AIM Address
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Tips and Tricks All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
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