X-Cart: shopping cart software

X-Cart forums (https://forum.x-cart.com/index.php)
-   Changing design (https://forum.x-cart.com/forumdisplay.php?f=51)
-   -   Passing Smarty Variables to PHP? (https://forum.x-cart.com/showthread.php?t=738)

AJ 11-22-2002 07:44 AM

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>

B00MER 11-22-2002 10:28 PM

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

:arrow: http://smarty.php.net

AJ 11-23-2002 05:31 AM

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.

tmx 11-23-2002 09:13 AM

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...

AJ 11-23-2002 10:26 AM

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.

tmx 11-23-2002 10:55 AM

...
 
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?

B00MER 11-24-2002 02:13 AM

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

I think thats what tmx was getting at :wink:

AJ 11-24-2002 06:32 AM

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?

derrick92130 11-24-2002 09:43 AM

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.

tmx 11-24-2002 09:44 AM

...
 
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...


All times are GMT -8. The time now is 07:55 PM.

Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.