{html_table}

{html_table}是一个 自定义函数,可使用数组形式的数据来创建一个HTML的<table>.

参数名称 类型 必选参数 默认值 说明
loop array Yes n/a 循环赋值的数组
cols mixed No 3 表格的列数,或者是逗号分隔的列头文字列表,或是列头文字的数组。 如果cols属性为空,但设置了rows,将以rows数量和显示元素的总数进行计算得出列数, 以便每列能显示全部的元素。 如果rows和cols都设置了,那么cols会忽略默认值3. 如果设置cols为一个列表或数组,那么列数将取决于列表或数组的元素个数。
rows integer No empty 表格的行数。如果设置为空,但设置了cols,那么将以cols数量和显示元素的总数进行计算得出行数, 以便每行能显示全部的元素。
inner string No cols 显示元素的循环方向。cols意味着元素将按“一列一列”地显示。 而rows意味着元素将“一行一行”地显示。
caption string No empty 表格中<caption>属性值
table_attr string No border="1" <table>标签的属性
th_attr string No empty <th>标签的属性 (循环)
tr_attr string No empty <tr>标签的属性 (循环)
td_attr string No empty <td>标签的属性 (循环)
trailpad string No &nbsp; 在最后行空单元格中填充的字符(如果有的话)
hdir string No right 每行显示的方向。可以设置: right (从左到右), 和 left (从右到左)
vdir string No down 每列显示的方向。可以设置: down (上到下), up (下到上)

Example 8.19. {html_table}


<?php
$smarty->assign( 'data', array(1,2,3,4,5,6,7,8,9) );
$smarty->assign( 'tr', array('bgcolor="#eeeeee"','bgcolor="#dddddd"') );
$smarty->display('index.tpl');
?>

  

例子演示如何从PHP赋值到模板并且显示表格。下面是各种输出:


{**** Example One ****}
{html_table loop=$data}

<table border="1">
<tbody>
<tr><td>1</td><td>2</td><td>3</td></tr>
<tr><td>4</td><td>5</td><td>6</td></tr>
<tr><td>7</td><td>8</td><td>9</td></tr>
</tbody>
</table>


{**** Example Two ****}
{html_table loop=$data cols=4 table_attr='border="0"'}

<table border="0">
<tbody>
<tr><td>1</td><td>2</td><td>3</td><td>4</td></tr>
<tr><td>5</td><td>6</td><td>7</td><td>8</td></tr>
<tr><td>9</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>
</tbody>
</table>


{**** Example Three ****}
{html_table loop=$data cols="first,second,third,fourth" tr_attr=$tr}

<table border="1">
<thead>
<tr>
<th>first</th><th>second</th><th>third</th><th>fourth</th>
</tr>
</thead>
<tbody>
<tr bgcolor="#eeeeee"><td>1</td><td>2</td><td>3</td><td>4</td></tr>
<tr bgcolor="#dddddd"><td>5</td><td>6</td><td>7</td><td>8</td></tr>
<tr bgcolor="#eeeeee"><td>9</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>
</tbody>
</table>