Follow us on Twitter X-Cart on Facebook Wiki
Shopping cart software Solutions for online shops and malls
 

Passing Smarty Variables to PHP?

 
Reply
   X-Cart forums > X-Cart 4 > Dev Questions > Changing design
 
Thread Tools Search this Thread
  #1  
Old 11-22-2002, 07:44 AM
 
AJ AJ is offline
 

Member
  
Join Date: Oct 2002
Location: Marietta, Ohio
Posts: 25
 

Default Passing Smarty Variables to PHP?

Okay, I've been using the following method to include my own PHP files in Smarty .tpl template files:

{include_php file="/pathtofile/file.php"}

Now I have another problem. How in the WORLD do you pass a variable from the .tpl file to the included PHP file????

In my /skin1/main/customer/main/products.tpl file, I want to include a php file that contains some custom mysql queries. All I need to do, is pass the productid variable to this included file. I cannot do it to save my life. After hours of experimenting and research in newsgroups, etc, it is becoming painfully apparent that while PHP can send variables TO Smarty, it cannot receive variables FROM Smarty.

<rant>
This is another prime example why I think the "customizability" of X-Cart is really misrepresented. If Smarty and PHP can't talk to each other, what is the point? If X-Cart were 100% PHP, I could have accomplished this customization in twenty minutes. Instead, I've spent over five hours on it with no resolution, including the time spent posting this message.
</rant>
__________________
Andrew
Webdev/Designer/Filmmaker
http://www.jstudios.com
Reply With Quote
  #2  
Old 11-22-2002, 10:28 PM
  B00MER's Avatar 
B00MER B00MER is offline
 

X-Guru
  
Join Date: Sep 2002
Location: Keller, TX (Cart-Lab.com)
Posts: 3,165
 

Default

You wouldn't pass the variable from the .tpl it gets passed to the .php file that references the template.

http://smarty.php.net
__________________
Cart-Lab - 100+ Social Bookmarks for X-Cart.
Reply With Quote
  #3  
Old 11-23-2002, 05:31 AM
 
AJ AJ is offline
 

Member
  
Join Date: Oct 2002
Location: Marietta, Ohio
Posts: 25
 

Default

I'm sorry, but that doesn't make any sense to me. If the variable is passed to the .tpl file, then why can't I use the variable in a PHP file I include in the .tpl file? If it's all PHP-based, as smarty claims, then this should be no problem. If the variables are just unusable by PHP once it hits the .tpl file, that's ridiculous.
Reply With Quote
  #4  
Old 11-23-2002, 09:13 AM
 
tmx tmx is offline
 

Member
  
Join Date: Nov 2002
Location: Arizona (USA)
Posts: 28
 

Default How to..

You need to assign the variable over to smarty, here is how you do it...

Code:
$smarty->assign("variable_for_smarty",$variable_in_php_your_transfering);

variable_for_smarty = What you want to name the variable for use in your *.tpl file, there is no $ in that line but to access it in the php it will be $variable_for_smarty

$variable_in_php_your_transfering = The variable in that php file that you want to use..

Hopefully that explains it and i have it correctly..

Here's an example as well...

Code:
$smarty->assign("memlevel",$user_account[membership]);

That will allow you to get the currect user that is logged in membership level, you would just need to specify { $memlevel } in your *.tpl...
__________________
-tmx-
Reply With Quote
  #5  
Old 11-23-2002, 10:26 AM
 
AJ AJ is offline
 

Member
  
Join Date: Oct 2002
Location: Marietta, Ohio
Posts: 25
 

Default

Thanks for the response, but that's not quite what I'm looking for. I already have the variable in the .tpl file. It's the productid variable in /skin1/main/customer/main/products.tpl. I want to include a php file in that .tpl template that uses the productid variable that is already set and being used by Smarty. This has got to be possible somehow.
Reply With Quote
  #6  
Old 11-23-2002, 10:55 AM
 
tmx tmx is offline
 

Member
  
Join Date: Nov 2002
Location: Arizona (USA)
Posts: 28
 

Default ...

You do realize that variable is COMING from a php file correct? The php file that variable is coming from is in /xcart dir/customer/products.php. Maybe that will help?
__________________
-tmx-
Reply With Quote
  #7  
Old 11-24-2002, 02:13 AM
  B00MER's Avatar 
B00MER B00MER is offline
 

X-Guru
  
Join Date: Sep 2002
Location: Keller, TX (Cart-Lab.com)
Posts: 3,165
 

Default

Couldn't you just insert this before the template displays in products.php?

I think thats what tmx was getting at
__________________
Cart-Lab - 100+ Social Bookmarks for X-Cart.
Reply With Quote
  #8  
Old 11-24-2002, 06:32 AM
 
AJ AJ is offline
 

Member
  
Join Date: Oct 2002
Location: Marietta, Ohio
Posts: 25
 

Default

I understand your suggestion, but how can I do that? Sure, I might be able to find a way to do the mySQL query somewhere else, but I need the result displayed in a specific location in the template.

Plus, the products.php file doesn't actually create the variables anyway. I think it's someplace in the func.php file. It just seems like a no-brainer. The variable is already being used in the tpl file, why can't I use it in an included PHP file, too?

Does anyone really think this is easier than an all-php system?
Reply With Quote
  #9  
Old 11-24-2002, 09:43 AM
 
derrick92130 derrick92130 is offline
 

Advanced Member
  
Join Date: Nov 2002
Location: San Diego, California USA
Posts: 68
 

Default variable use in PHP and Smarty templates

Can you tell us what the programming logic is that you are trying to do? We may be able to give you an alternative way to capture the information in the PHP program (usually through storage in an array that is passed the the TPL) for use later in the Smarty template.

I have used both 100% PHP applications and now Smarty templates through the use of X-Cart. I have found that once I got through the understanding of the strengths of both, I am able to implement changes and new customer requirements quite quickly.

I have passed variables from PHP to Smarty templates and then back to PHP, but not to PHP as an included file, only as a call from a form, etc. In that case you just pass it as an argument. Obviously, that won't work in the include.
__________________
-Derrick
FreeRangeMinds, LLC
Reply With Quote
  #10  
Old 11-24-2002, 09:44 AM
 
tmx tmx is offline
 

Member
  
Join Date: Nov 2002
Location: Arizona (USA)
Posts: 28
 

Default ...

What exactly are you trying to do? Or what variable are you trying to get to use, i'm sure you could get the variable in that php file and then just assign it to smarty so that you could use it. I guess i just don't really understand what your asking...
__________________
-tmx-
Reply With Quote
Reply
   X-Cart forums > X-Cart 4 > Dev Questions > Changing design



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -8. The time now is 06:13 PM.

   

 
X-Cart forums © 2001-2020