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 Previous  1, 2
 
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
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Wed Jul 08, 2009 2:41 am    Post subject: Reply with quote

lorderunion wrote:
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.


It was a very early feature of the engine, long before design principles were completely materialized, and before a lot of the other better options were created. Never use it.
Back to top
View user's profile Send private message Visit poster's website
bimal
Smarty Elite


Joined: 19 Apr 2007
Posts: 423

PostPosted: Wed Jul 08, 2009 8:53 am    Post subject: Reply with quote

You can shorten the code with:
Code:
<select name="pie_type">
{html_options options=$types selected=$smarty.post.pie_type}
</select>


Please be sure to check the manual for confirmation
Back to top
View user's profile Send private message Visit poster's website
edank
Smarty n00b


Joined: 07 Jul 2009
Posts: 2

PostPosted: Wed Jul 08, 2009 10:22 am    Post subject: Reply with quote

first of all thanks for the quick response, though i still have a problem.

the select is in a section
Code:

{section loop=$cart_content  name=i}
     <SELECT name="shipment_{$smarty.section.i.index}">
..
..


from your examples, i tried accessing {$smarty.post.shipment_?}

the '?' is where im stuck. im trying to get the index number, but whatever i tried just got me a blank screen
Back to top
View user's profile Send private message
jezjones
Smarty n00b


Joined: 14 Jul 2009
Posts: 1

PostPosted: Tue Jul 14, 2009 4:38 pm    Post subject: Reply with quote

edank : here is a sample of how i have used indexes in sections

Code:

<{section name=mainData loop=$results}>
      <{if $results[$smarty.section.mainData.index].MediaID != $results[$smarty.section.mainData.index_prev].MediaID}>


... other code like <{/if}><{/section}>



the MediaID bit is the name of my variable in an associative array.

Also can people stop saying such bad things against {php} because without it smarty will become much more than a template language.

e.g. there is no way to mimic the following php function that controls display logic : in_array. When you are looping through an array (displaying stuff) with section, to check the current value is present in another array (perhaps showing different content depending on auth level)


HTH,

Jez
Back to top
View user's profile Send private message
dmm2020
Smarty Rookie


Joined: 29 Nov 2009
Posts: 27

PostPosted: Sun Nov 29, 2009 1:01 am    Post subject: Reply with quote

Celeb wrote:
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."


How do you do this when the smarty variable is an array? For example, I need to convert gameListArray.gameid to $gameid. All attempts I have tried failed.
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Sun Nov 29, 2009 8:41 pm    Post subject: Reply with quote

$gameListArray = $this->get_template_vars('gameListArray');
$gameid = $gameListArray['gameid'];
Back to top
View user's profile Send private message
tommix
Smarty n00b


Joined: 11 Dec 2009
Posts: 1

PostPosted: Fri Dec 11, 2009 12:38 am    Post subject: Reply with quote

So can ANYBODY tell how to get smarty loop variable INSIDE the {php} tag???

I CANNOT assign var in php, i must somehow get smarty section loop variable inside the {php} tag. is there any way?
Back to top
View user's profile Send private message
sharik
Smarty n00b


Joined: 11 Mar 2010
Posts: 1

PostPosted: Fri Mar 12, 2010 9:20 am    Post subject: more problem to access smarty variable in php?? Reply with quote

i know it is a older one thread... but may be newer one is in problem...
so i leave it for those........ i used:

Code:
{php}
$v1 = $this->get_template_vars('value1');
$v2 = $this->get_template_vars('value2');
$sum=$v1+$v2;
{/php}

_________________
Web Programming and SEO Tips
Back to top
View user's profile Send private message
demodav
Smarty n00b


Joined: 20 May 2010
Posts: 2

PostPosted: Thu May 20, 2010 1:39 pm    Post subject: This is better for display all. Reply with quote

{php}
$myVars = $this->get_template_vars();
echo "<pre>";
var_dump($myVars);
echo "</pre>";
{/php}
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Thu May 20, 2010 1:45 pm    Post subject: Reply with quote

{debug}
Back to top
View user's profile Send private message Visit poster's website
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 Previous  1, 2
Page 2 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