![]() |
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> |
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 |
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.
|
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... |
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.
|
...
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?
|
Couldn't you just insert this before the template displays in products.php?
I think thats what tmx was getting at :wink: |
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? |
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. |
...
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...
|
You're right. I don't think I've adequately explained it. I have added a column to the xcart_products table for "Unit of Issue" (unit). I needed to be able to tell the customer at a glance whether the item was a set of 4, set of 12, a dozen in a bag, etc.
But the unit column itself only contains unit codes (ST4, ST12, BG, etc). The full descriptive unit names (ie, Set of 4) are stored in a separate table that I created. The ultimate goal is to add a line underneath the price of each product that says "Unit of issue: " and then insert the descriptive unit name, so that it reads something like "Unit of issue: Set of 4". So I created a php file that would take the productid (which products.tpl already is using), then query the xcart_products table to determine the unitcode, then query the unit table to determine the full unit name and display it as "Unit of issue: Set of 4". Seemed pretty easy to me. I figured I could just include that PHP file in the template right where I wanted it to appear, and that would be that. But since the PHP file can't use any Smarty variables, I'm stuck. Hope that clears things up a bit. I got confused just writing it :) |
...
Ok correct me if I'm wrong but could you add that into the array that products.php already creates, that is if the array doesn't already have it, and then in the tpl file use it by something like { $products[product].unit } ?
|
Re: ...
Quote:
I guess what this may come down to is just finding a different way of storing the unit information in the db, but I'm once again having to do things differently just to cater to Smarty. Ugh. |
...
Ok so couldn't you set that other table up into an array in either another php file with just an include or just in products.php and use what $products[product].unit comes up to get the full unit name using the same basic method?
|
variable passing
-tmx- described the exact process that I did. I had the same scenario where I added a new "leadtime" field to the products table. All that went into that field was a CODE that was translated from a new table ("leadtimes"). In the product.php code, I pulled the contents of the leadtimes table into an array. In the product.tpl, I then just referenced the matching entry in the array to the code in the product. That way I could change the "leadtimes" table at any time to change custom leadtimes based upon product characteristics without having to do a mass change in to all entries in the products table.
Let me know if you want an example of either the php or tpl files. |
Re: variable passing
Quote:
|
passing variables/alternative approach
AJ,
I just pulled my code (it has been a little while). It looks like I did the "correlation" in the product.php program, and left the cross-referenced narrative (from the leadtimes table) in a single element array (could have just assigned to a variable) that is referenced in the product.tpl. Here is a snippet from the product.php (much hacked 3.1.1/3.1.2 version): Code:
# Here is how I referenced it in product.tpl (once again from hacked 3.1.1 tpl file). Just a simple expression of the array. This was one of the first changes I made when I first started working with X-Cart, so I could have cleaned this up with just a single variable instead of an array, so you see the array index fixed to [0] :-) Code:
# test is only here to see if I need to display any leadtime message at all Hope this helps. Let me know if I can provide any more details. |
No dice. I tried something very similar to your code:
Code:
$product_info = func_select_product($productid, $user_account['membership']); ...and when I go to any product page, I get the "Access denied ! You are not allowed to access that resource!" message. I also thought about just trying to build my own query, based on the productid, but I can't even figure out how to get that variable. $products['productid'] doesn't work. I'm sure I'm nearing ten hours on this and I'm about to bug out. Once again, my client is going to have to suffer because Smarty is a pain in the ass. Thanks a bunch for the help so far, and if you've got any other ideas, I'm dying to hear them :x |
Can you send the tpl where you are accessing the variables?
Either in the forum our out, I can look at it later today. Can you also send the version you are working with? |
more Smarty fun
AJ,
I did look through your posting again (actually read it closely this time) and think I see what the problem is. In my example, I was using the product (not products) table and therefore could do a simple lookup in the php code and pass the variable onto the tpl. In your case, you have an entire array of products. In your sample code (from the Forum), you are trying to do a select to populate the $unit_text array that is trying to match "code" to an entire array, not a single element. That is what is causing the access error. Just load the entire "units" table into the "$unit_text" array in the products.php program. Code:
$unit_text = func_query("SELECT unit, code FROM units"); Code:
{section name=unittext loop=$unit_text} Code:
{$unit_text['$products[product].unit'].code} Give it a try! If it still doesn't work, send me an email and I should be able to get my test site back on 3.2.2 tomorrow. Hope this helps. |
Thanks, man. With the holiday and all, I may not get to try it again until next week, but I'm anxious to find out. But regardless of whether it works for me, thanks for spending the extra time helping me out.
Will let you know how it goes. |
All times are GMT -8. The time now is 10:32 PM. |
Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.