Override Variable in Array - Smarty

廉价感情. 提交于 2021-01-28 05:43:33

问题


A slightly strange one, but I'm using an ecommerce platform called CS-Cart which uses smarty for it's templating language. It has a smarty debug console which allows me to see which variables the current page is using.

For example, here is an array that is being used (I have shortened this for the example):

 Appearance => Array (33)
  products_per_page => "10"
  admin_products_per_page => "10"
  admin_elements_per_page => "10"
  columns_in_products_list => "3"
  default_products_sorting => "price"

I would like to be able to assign a different value to default_products_sorting => "price" I plan to do this in the header of my document as it will be included in an IF statement whereby the variable is only changed if certain conditions are true.

Is this even possible in smarty? (without the use of PHP) and if so any ideas on how this could be achieved? I've scoured Google but so far turned up a blank.

Any help is much appreciated.


回答1:


You can change the variable like this

{$Appearance.default_products_sorting = "NEW_VALUE"}

And after this line, if you use {$Appearance.default_products_sorting} you should get the new value.

I just checked and it worked.

You should change it at the top of your smarty template, so that any code that is using this variable get the updated value.

Looks like the above method doesn't work for arrays. You can consider using the custom function from this link http://www.smarty.net/forums/viewtopic.php?t=7201&highlight=create+array




回答2:


{php}$this->_tpl_vars['Appearance']['default_products_sorting'] = "value";{/php}

I haven't tested this outside WHMCS but from a quick search it looks like it would still apply.

Found here: http://forum.whmcs.com/showthread.php?28490-Modify-single-item-of-smarty-array



来源:https://stackoverflow.com/questions/10848360/override-variable-in-array-smarty

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!