Smarty comes with several custom functions that you can use in the templates. Some of the Custom Functions are given below.
assign
assign is used for assigning template variables during the execution of the template. It has two attributes i.e. var & value.
Example:
1.{assign var="country" value="USA"}
{$country} is a powerful country.
2.{assign "country" "USA"}
{$country} is a powerful country.
Demo:
cycle
cycle is used to cycle though a set of values. This makes it easy to alternate between two or more colors in a table. This feature is mainly used by the designers.
html_checkboxes is a custom function that creates an html checkbox group with provided data. Required attributes are name, values, output, selected unless you use options instead. All output is XHTML compatible.
Example:
In php side:
$smarty->assign('cust_ids', array(1000,1001,1002,1003));
$smarty->assign('cust_names', array(
'Amar',
'Subharam',
'Nibedita',
'Manjari')
);
$smarty->assign('customer_id', 1001);
In html side:
{html_checkboxes name='id' values=$cust_ids output=$cust_names
selected=$customer_id separator=' '}
Demo:
html_image:
html_image is a custom function that generates an HTML tag for an image. The height and width are automatically calculated from the image file if none are supplied.
The optional attributes: height,width,href.
Example:
{html_image file="hands.png"}
Demo:
html_options:
html_options is a custom function that creates html option group with provided data.Required attributes are values and output, unless you use options instead.
Example:
In php side:
$states_name = array("Odisha","Andhra Pradesh","Delhi","Punjab");
$state_index=array(0,1,2,3);
In html side:
Demo:
html_radios:
html_radios is a custom function that creates html radio button group with provided data. Required attributes are values and output, unless you use options instead.
Example:
In php side:
$smarty->assign('cust_ids', array(1000,1001,1002,1003));
$smarty->assign('cust_names', array(
'Amar',
'Subharam',
'Nibedita',
'Manjari')
);
$smarty->assign('customer_id', 1001);
In html side:
{html_radios name='id' values=$cust_ids output=$cust_names
selected=$customer_id separator=' '}
Demo:
html_select_date:
html_select_date is a custom function that creates date dropdowns for you. It can display any or all of year, month, and day.
html_table is a custom function.The cols attribute determines how many columns will be in the table. The table_attr, tr_attr and td_attr values determine the attributes given to the table, tr and td tags.
{eval} is used to evaluate a variable as a template. This can be used for things like embedding template tags/variables into variables or tags/variables into config file variables.
If you supply the assign attribute, the output of the {eval} function will be assigned to this template variable instead of being output to the template.
Example:
{$food='food is goog for health'}
{$errorstate="Please give the state name"}
{eval var=$food}<br/>
{eval var=$errorstate assign='state_error'}
{$state_error}
Demo:
Question for Smarty
Design a form having the above form fields at least 2 from each in smarty.
Suppose I have an array as array(a1=>array(a11,a12,a13),b1=>array(b11,b12,b13),c1=>array(c11,c12,c13)).
Write down the code in smarty so that the output will be as follows:
a11
a12
a13
b11
b12
b13
c11
c12
c13
Write the code in smarty so that the in a table the alternative colum have different colors and also it should continue in alternative row.
Write the code in smarty so that I have four check boxes and all the check boxes are checked and have the value which is different from the output value;
Write the code in smarty so that I have an image having height of 40px ahd width of 40px .
Write code so that I have three drop down fields one for country, one for state and one for district
when i select the country then the corresponding state should display and when i select the state
then the corresponding district should display.
Suppose I have two associative arrays like
$smarty->assign('a', array(1=>array('subha','amar'),4=>array('nibedita','harapriya')));
$smarty->assign('b', array(1=>array(8,9),4=>array(11,12)));
The output should be as follows:
subha amar
nibedita harapriya
Show the drop down for date in which the starting year will be 2011 and ending year will be 2018 the year should be in ascending order.
Show the drop down for hour,minute and second and should show in 12hour format and show AM and PM.
Suppose I have an array $smarty->assign('arr',array('a'=>array('b','c','d'),'e'=>array('f','g','h'),'f'=>array('f','g','h')));
Write the code so that the output will be like this: