Jump to content

use php variable in tpl file


Recommended Posts

Hi there,

I would like to know how i can use a variable ($manu_id) that i have created and defined in product.php in product.tpl.

I have tried creating PHP code in the TPL file as the JS method didn't work.

Is there something i am missing in order to bring across the variables in the "code behind file" to the TPL files in prestashop.

Any help would be appreciated.

Cheers
Rob

Link to comment
Share on other sites

I get that you need to declare the variable using the "$smarty->assign('variable_name','value');", but I'm confused about something.

If there's already a $smarty->assign(array... in place, can you use the $smarty->assign('variable_name','value'); or do you need to include it in the $smarty->assign(array... portion of the code?

Also, regarding the "'value'" in the statement above, is that referring to the an assigned value or can you establish a variable that pulls from the database?

I'm trying to add a variable to the my-account.php page. In the my-account.tpl file, I've included an if statement: {if $customer.careerstatus == 1} {else} {/if}. Unfortunately, I can't get the variable "careerstatus" to pull from the database.

Any help you can give me would be greatly appreciated. Thanks.

Link to comment
Share on other sites


If there's already a $smarty->assign(array... in place, can you use the $smarty->assign('variable_name','value'); or do you need to include it in the $smarty->assign(array... portion of the code?

You can mix those together without a problem.


Also, regarding the "'value'" in the statement above, is that referring to the an assigned value or can you establish a variable that pulls from the database?

I'm trying to add a variable to the my-account.php page. In the my-account.tpl file, I've included an if statement: {if $customer.careerstatus == 1} {else} {/if}. Unfortunately, I can't get the variable "careerstatus" to pull from the database.


I'm not sure what you mean by "Pull from the database".

When you use $smarty->assign, the value of the variable gets assign there, it will not change if later in your code you assign a different value to it.

You can pass smarty a function, which will be executed when you call it from smarty, that function can go to the database to retrieve a value.

You can see an example for that in /init.php line #91 (the first variable is the name of the function to be used in smarty, the second is an array, first element is the class name where the function is located, the second one it the name of the fucntion)
Link to comment
Share on other sites

I've set up a new variable in the MySQL database which determines the type of customer that they are when they fill out the registration form.

I use this variable to determine what content to send when they log into their account by using the statement: {if $customer.careerstatus|intval == 1 }{else}{/if}.

This variable is set in the my-account.tpl file which doesn't seem to be able to pull the variable value for a test account that in which I've set the value == 1. Instead, it's unable to pull the data from the MySQL database and assumes that the value is NULL and goes to the {else} option instead.

Here's what I have in the my-account.php file:

<?php

/* SSL Management */
$useSSL = true;

include(dirname(__FILE__).'/config/config.inc.php');
include(dirname(__FILE__).'/init.php');

if (!$cookie->isLogged())
Tools::redirect('authentication.php?back=my-account.php');
else
{
$customer = new Customer(intval($cookie->id_customer));
$firsttime = intval($customer->first_time);
if ($firsttime == 0)
Tools::redirect('first-time.php?back=my-account.php');
elseif ($firsttime == 1)


include(dirname(__FILE__).'/header.php');

$smarty->assign(array(
'careerstatus' => $careerstatus,
'voucherAllowed' => intval(Configuration::get('PS_VOUCHERS')),
'returnAllowed' => intval(Configuration::get('PS_ORDER_RETURN')),
'HOOK_CUSTOMER_ACCOUNT' => Module::hookExec('customerAccount')
));
$smarty->display(_PS_THEME_DIR_.'my-account.tpl');

include(dirname(__FILE__).'/footer.php');
}
?>

I just can't get the variable $careerstatus to pull from the database. I've declared it in the Customer.php file in the Class folder and I don't know what I'm missing. Any thoughts.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...