how i can write php code in skins file

how i can write php code in skins file ( .tpl )

You can use {php} {/php} tags:



[url]Search Results for "docsv2" | Smarty

I'm trying to upgrade my 2.0.15 store to 2.2.2 on my development server. The {php} smarty tags from my 2.0.15 template are not working properly in the 2.2.2 template. It looks like the {php} smarty tag was deprecated in smarty 3.0. Is that what CS-Cart is using now?



In 2.2.2, this code:

{php}
$day = date("z");
$month = date("n");
$season = 'bg_landing_01_home';

$northern = array(
'summer' => array(6, 7, 8),
'autumn' => array(9, 10, 11),
'winter' => array(12, 1, 2),
'spring' => array(3, 4, 5)
);

if(in_array($month, $northern['winter'])) { $season = 'bg_landing_winter_home'; }
if(in_array($month, $northern['spring'])) { $season = 'bg_landing_summer_home'; }
if(in_array($month, $northern['summer'])) { $season = 'bg_landing_summer_home'; }
if(in_array($month, $northern['autumn'])) { $season = 'bg_landing_autumn_home'; }
{/php}


Outputs this in plain text:

$day = date("z");
$month = date("n");
$season = 'bg_landing_01_home';

$northern = array(
'summer' => array(6, 7, 8),
'autumn' => array(9, 10, 11),
'winter' => array(12, 1, 2),
'spring' => array(3, 4, 5)
);

if(in_array($month, $northern['winter']))
if(in_array($month, $northern['spring']))
if(in_array($month, $northern['summer']))
if(in_array($month, $northern['autumn']))


The {php} tags are missing, and nothing inside them is being interpreted.

I can not remember off the top of my head, but look in the config.php or init.php files in your root directory for a flag to re-enable the smarty php function.



David

According to the Smarty documentation, the flag to enable it is:

$smarty->allow_php_tag=true

But, I don't see anything like this in the init.php or config.php files.

Look for



'allow_php_in_templates' => false, // Allow to use {php} tags in templates



in config.local.php



Set to true

Why put the php tags in your templates? Why not do the php code in a controller (such as addons/my_changes/controllers/customer/init.post.php) and then just set a smarty variable (like Registry::get(‘view’)->assign(‘season’, $season);) and then just use {$season} in your template.



Bad idea to put PHP in the template. It makes the template code recurse unnecessarily.

[quote name=‘tbirnseth’ timestamp=‘1317346109’ post=‘122632’]

Why put the php tags in your templates? Why not do the php code in a controller (such as addons/my_changes/controllers/customer/init.post.php) and then just set a smarty variable (like Registry::get(‘view’)->assign(‘season’, $season);) and then just use {$season} in your template.



Bad idea to put PHP in the template. It makes the template code recurse unnecessarily.

[/quote]



That. Is. Awesome.



It works perfect. I’ve never used the my_changes addon before. I appreciate you pointing me in the right direction.

Glad I could help. Much rather get someone headed in a right direction than encourage them on how to do it incorrectly. Makes it easier to help in the long term…

I had the following in {php} tags with 2.0.15. Any pointers on translating this to 2.2.3 and putting into the addons/my_changes/controllers/customer/init.post.php file


$cartSubtotal = $this->_tpl_vars['cartSubtotal'];
$freeThreshold = $this->_tpl_vars['freeThreshold'];
$freeThreshold = floatval($freeThreshold) + 0.01;
$freeLang = $this->_tpl_vars['freeLang'];
$shippingLang = $this->_tpl_vars['shippingLang'];
$addAmount = $freeThreshold - $cartSubtotal;
if ($cartSubtotal == null || $cartSubtotal == "null" || $cartSubtotal == 0) {
echo "

".$freeLang." ".$shippingLang." on orders of $".$freeThreshold." or more.

";
} elseif ($cartSubtotal <= $freeThreshold) {
echo "

Add $".number_format($addAmount, 2)." more for ".$freeLang." ".$shippingLang."!

";
} else {
echo "

Your order qualifies for ".$freeLang." ".$shippingLang."!

";
}

I certainly wouldn't put this in init.post.php… Maybe checkout.post.php…



It would be a “port” of your previous work. But all the data you're pulling from the template should be available via things like Registry::get('view')->get_var('variable_name');



Then you'd use a Registry::get('view')->assign('variable_name', 'value'); to make the variable available in your template.

The final code turned out like this:

$cartSubtotal = $_SESSION['cart']['display_subtotal'];
$freeThreshold = fn_get_lang_var('text_free_ship_threshold');
$freeThreshold = floatval($freeThreshold) + 0.01;
$freeLang = fn_get_lang_var('free');
$shippingLang = fn_get_lang_var('text_standard_shipping');
$addAmount = $freeThreshold - $cartSubtotal;
$rea_free_ship_notice = "";

if ($cartSubtotal == null || $cartSubtotal == "null" || $cartSubtotal == 0) {
$rea_free_ship_notice .= "

".$freeLang." ".$shippingLang." on orders of {:content:}quot;.$freeThreshold." or more.

";
} elseif ($cartSubtotal <= $freeThreshold) {
$rea_free_ship_notice .= "

Add {:content:}quot;.number_format($addAmount, 2)." more for ".$freeLang." ".$shippingLang."!

";
} else {
$rea_free_ship_notice .= "

Your order qualifies for ".$freeLang." ".$shippingLang."!

";
}

Registry::get('view')->assign('rea_free_ship_notice', $rea_free_ship_notice);




I had to use unescape in the template so my HTML would work correctly:

{$rea_free_ship_notice|unescape:"htmlall"}