smarty section 学习

{section},{sectionelse}

Template sections are used for looping over arrays of data (just like {foreach}). All {section} tags must be paired with {/section} tags. Required parameters are name and loop. The name of the {section} can be anything you like, made up of letters, numbers and underscores. Sections can be nested, and the nested section names must be unique from each other. The loop variable (usually an array of values) determines the number of times the section will loop. When printing a variable within a section, the section name must be given next to variable name within brackets []. {sectionelse} is executed when there are no values in the loop variable.

 

Attribute NameTypeRequiredDefaultDescription
namestringYesn/aThe name of the section
loopmixedYesn/aValue to determine the number of loop iterations
startintegerNo0The index position that the section will begin looping. If the value is negative, the start position is calculated from the end of the array. For example, if there are seven values in the loop array and start is -2, the start index is 5. Invalid values (values outside of the length of the loop array) are automatically truncated to the closest valid value.
stepintegerNo1The step value that will be used to traverse the loop array. For example, step=2 will loop on index 0,2,4, etc. If step is negative, it will step through the array backwards.
maxintegerNon/aSets the maximum number of times the section will loop.
showbooleanNotruedetermines whether or not to show this section

 

Example 7-19. {section}

<?php

$data 
= array(1000,1001,1002);
$smarty->assign('custid',$data);

?>
{* this example will print out all the values of the $custid array *}
{section name=customer loop=$custid}
  id: {$custid[customer]}<br />
{/section}
<hr />
{*  print out all the values of the $custid array reversed *}
{section name=foo loop=$custid step=-1}
  {$custid[foo]}<br />
{/section}

The above example will output:

id: 1000<br />
id: 1001<br />
id: 1002<br />
<hr />
id: 1002<br />
id: 1001<br />
id: 1000<br />

Another couple of examples without an assigned array.

{section name=foo start=10 loop=20 step=2}
  {$smarty.section.foo.index}
{/section}
<hr />
{section name=bar loop=21 max=6 step=-2}
  {$smarty.section.bar.index}
{/section}

The above example will output:

10 12 14 16 18
<hr />
20 18 16 14 12 10

Example 7-20. {section} loop variable

<?php

$id 
= array(1001,1002,1003);
$smarty->assign('custid',$id);

$fullnames = array('John Smith','Jack Jones','Jane Munson');
$smarty->assign('name',$fullnames);

$addr = array('253 N 45th''417 Mulberry ln''5605 apple st');
$smarty->assign('address',$addr);

?>
{*
  the loop variable only determines the number of times to loop.
  you can access any variable from the template within the section.
  This example assumes that $custid, $name and $address are all
  arrays containing the same number of values
*}
{section name=customer loop=$custid}
<p>
  id: {$custid[customer]}<br />
  name: {$name[customer]}<br />
  address: {$address[customer]}
</p>
{/section}

The above example will output:

<p>
  id: 1000<br />
  name: John Smith<br />
  address: 253 N 45th
</p>
<p>
  id: 1001<br />
  name: Jack Jones<br />
  address: 417 Mulberry ln
</p>
<p>
  id: 1002<br />
  name: Jane Munson<br />
  address: 5605 apple st
</p>

Example 7-21. {section} naming

{*
  the name of the section can be anything you like,
  as it is used to reference the data within the section
*}
{section name=anything loop=$custid}
<p>
  id: {$custid[anything]}<br />
  name: {$name[anything]}<br />
  address: {$address[anything]}
</p>
{/section}

Example 7-22. nested sections

<?php

$id 
= array(1001,1002,1003);
$smarty->assign('custid',$id);

$fullnames = array('John Smith','Jack Jones','Jane Munson');
$smarty->assign('name',$fullnames);

$addr = array('253 N 45th''417 Mulberry ln''5605 apple st');
$smarty->assign('address',$addr);

$types = array(
           array( 
'home phone''cell phone''e-mail'),
           array( 
'home phone''web'),
           array( 
'cell phone')
         );
$smarty->assign('contact_type'$types);

$info = array(
           array(
'555-555-5555''666-555-5555''john@myexample.com'),
           array( 
'123-456-4''www.example.com'),
           array( 
'0457878')
        );
$smarty->assign('contact_info'$info);

?>
{*
  sections can be nested as deep as you like. With nested sections,
  you can access complex data structures, such as multi-dimensional
  arrays. In this example, $contact_type[customer] is an array of
  contact types for the current customer.
*}
{section name=customer loop=$custid}
<hr>
  id: {$custid[customer]}<br />
  name: {$name[customer]}<br />
  address: {$address[customer]}<br />
  {section name=contact loop=$contact_type[customer]}
    {$contact_type[customer][contact]}: {$contact_info[customer][contact]}<br />
  {/section}
{/section}

The above example will output:

<hr>
  id: 1000<br />
  name: John Smith<br />
  address: 253 N 45th<br />
    home phone: 555-555-5555<br />
    cell phone: 666-555-5555<br />
    e-mail: john@myexample.com<br />
<hr>
  id: 1001<br />
  name: Jack Jones<br />
  address: 417 Mulberry ln<br />
    home phone: 123-456-4<br />
    web: www.example.com<br />
<hr>
  id: 1002<br />
  name: Jane Munson<br />
  address: 5605 apple st<br />
    cell phone: 0457878<br />

Example 7-23. sections and associative arrays

<?php

$data 
= array(
          array(
'name' => 'John Smith''home' => '555-555-5555',
                
'cell' => '666-555-5555''email' => 'john@myexample.com'),
          array(
'name' => 'Jack Jones''home' => '777-555-5555',
                
'cell' => '888-555-5555''email' => 'jack@myexample.com'),
          array(
'name' => 'Jane Munson''home' => '000-555-5555',
                
'cell' => '123456''email' => 'jane@myexample.com')
        );
$smarty->assign('contacts',$data);

?>
{*
   This is an example of printing an associative array
   of data within a section
*}
{section name=customer loop=$contacts}
<p>
  name: {$contacts[customer].name}<br />
  home: {$contacts[customer].home}<br />
  cell: {$contacts[customer].cell}<br />
  e-mail: {$contacts[customer].email}
</p>
{/section}

The above example will output:

<p>
  name: John Smith<br />
  home: 555-555-5555<br />
  cell: 666-555-5555<br />
  e-mail: john@myexample.com
</p>
<p>
  name: Jack Jones<br />
  home phone: 777-555-5555<br />
  cell phone: 888-555-5555<br />
  e-mail: jack@myexample.com
</p>
<p>
  name: Jane Munson<br />
  home phone: 000-555-5555<br />
  cell phone: 123456<br />
  e-mail: jane@myexample.com
</p>

Database example (eg using Pear or Adodb)

<?php

$sql 
'select id, name, home, cell, email from contacts';
$smarty->assign('contacts',$db->getAll($sql) );

?>
{*
   output database result in a table
*}
<table>
<tr><th>&nbsp;</th><th>Name></th><th>Home</th><th>Cell</th><th>Email</th></tr>
{section name=co loop=$contacts}
  <tr>
    <td><a href="view.php?id={$contacts[co].id}">view<a></td>
    <td>{$contacts[co].name}</td>
    <td>{$contacts[co].home}</td>
    <td>{$contacts[co].cell}</td>
    <td>{$contacts[co].email}</td>
  <tr>
{/section}
</table>

Example 7-24. {sectionelse}

{* sectionelse will execute if there are no $custid values *}
{section name=customer loop=$custid}
  id: {$custid[customer]}<br />
{sectionelse}
  there are no values in $custid.
{/section}

Sections also have their own variables that handle section properties. These are indicated like so: {$smarty.section.sectionname.varname}

Note: As of Smarty 1.5.0, the syntax for section property variables has changed from {%sectionname.varname%} to {$smarty.section.sectionname.varname}. The old syntax is still supported, but you will only see examples of the new syntax.

index

index is used to display the current array index, starting with zero (or the start attribute if given), and incrementing by one (or by the step attribute if given.)

Technical Note: If the step and start section properties are not modified, then this works the same as the iteration section property, except it starts at 0 instead of 1.

Example 7-25. {section} property index

{* FYI, $custid[customer.index] and $custid[customer] are identical in meaning *}

{section name=customer loop=$custid}
  {$smarty.section.customer.index} id: {$custid[customer]}<br />
{/section}

The above example will output:

0 id: 1000<br />
1 id: 1001<br />
2 id: 1002<br />

index_prev

index_prev is used to display the previous loop index. on the first loop, this is set to -1.

index_next

index_next is used to display the next loop index. On the last loop, this is still one more than the current index (respecting the setting of the step attribute, if given.)

Example 7-26. {section} property index_next and index_prev

<?php

$data 
= array(1001,1002,1003,1004,1005);
$smarty->assign('custid',$data);

?>
{* FYI, $custid[cus.index] and $custid[cus] are identical in meaning *}

<table>
  <tr>
    <th>index</th><th>id</th>
    <th>index_prev</th><th>prev_id</th>
    <th>index_next</th><th>next_id</th>
  </tr>
{section name=cus loop=$custid}
  <tr>
    <td>{$smarty.section.cus.index}</td><td>{$custid[cus]}</td>
    <td>{$smarty.section.cus.index_prev}</td><td>{$custid[cus.index_prev]}</td>
    <td>{$smarty.section.cus.index_next}</td><td>{$custid[cus.index_next]}</td>
  </tr>
{/section}
</table>

The above example will output a table containing the following:

index  id    index_prev prev_id index_next next_id
0      1001  -1	                1          1002
1      1002  0          1001    2          1003
2      1003  1          1002    3          1004
3      1004  2          1003    4          1005
4      1005  3          1004    5

iteration

iteration is used to display the current loop iteration.

Note: This is not affected by the section properties start, step and max, unlike the index property. Iteration also starts with 1 instead of 0 like index. rownum is an alias to iteration, they work identical.

Example 7-27. {section} property iteration

<?php

// array of 3000 to 3015
$id range(3000,3015);
$smarty->assign('custid',$id);

?>
{section name=cu loop=$custid start=5 step=2}
  iteration={$smarty.section.cu.iteration}
  index={$smarty.section.cu.index}
  id={$custid[cu]}<br />
{/section}

The above example will output:

iteration=1 index=5 id=3005<br />
iteration=2 index=7 id=3007<br />
iteration=3 index=9 id=3009<br />
iteration=4 index=11 id=3011<br />
iteration=5 index=13 id=3013<br />
iteration=6 index=15 id=3015<br />

This example uses the iteration property to output a table header block every five rows (uses {if} with the mod operator).

<table>
{section name=co loop=$contacts}
  {if $smarty.section.co.iteration % 5 == 1}
    <tr><th>&nbsp;</th><th>Name></th><th>Home</th><th>Cell</th><th>Email</th></tr>
  {/if}
  <tr>
    <td><a href="view.php?id={$contacts[co].id}">view<a></td>
    <td>{$contacts[co].name}</td>
    <td>{$contacts[co].home}</td>
    <td>{$contacts[co].cell}</td>
    <td>{$contacts[co].email}</td>
  <tr>
{/section}
</table>

first

first is set to true if the current section iteration is the first one.

last

last is set to true if the current section iteration is the last one.

Example 7-28. {section} property first and last

This example loops the $customers array; outputs a header block on the first iteration and on the last outputs the footer block (uses section total property)

{section name=customer loop=$customers}
  {if $smarty.section.customer.first}
    <table>
    <tr><th>id</th><th>customer</th></tr>
  {/if}

  <tr>
    <td>{$customers[customer].id}}</td>
    <td>{$customers[customer].name}</td>
  </tr>

  {if $smarty.section.customer.last}
    <tr><td></td><td>{$smarty.section.customer.total} customers</td></tr>
    </table>
  {/if}
{/section}

rownum

rownum is used to display the current loop iteration, starting with one. It is an alias to iteration, they work identically.

loop

loop is used to display the last index number that this section looped. This can be used inside or after the section.

Example 7-29. {section} property index

{section name=customer loop=$custid}
  {$smarty.section.customer.index} id: {$custid[customer]}<br />
{/section}

There were {$smarty.section.customer.loop} customers shown above.

The above example will output:

0 id: 1000<br />
1 id: 1001<br />
2 id: 1002<br />

There were 3 customers shown above.

show

show is used as a parameter to section. show is a boolean value, true or false. If false, the section will not be displayed. If there is a {sectionelse} present, that will be alternately displayed.

Example 7-30. {section} attribute show

{*
  $show_customer_info (true/false) may have been passed from the PHP
  application, to regulate whether or not this section shows
*}
{section name=customer loop=$custid show=$show_customer_info}
  {$smarty.section.customer.rownum} id: {$custid[customer]}<br />
{/section}

{if $smarty.section.customer.show}
  the section was shown.
{else}
  the section was not shown.
{/if}

The above example will output:

1 id: 1000<br />
2 id: 1001<br />
3 id: 1002<br />

the section was shown.

total

total is used to display the number of iterations that this section will loop. This can be used inside or after the section.

Example 7-31. {section} property total

{section name=customer loop=$custid step=2}
  {$smarty.section.customer.index} id: {$custid[customer]}<br />
{/section}

    There were {$smarty.section.customer.total} customers shown above.

The above example will output:

0 id: 1000<br />
2 id: 1002<br />
4 id: 1004<br />

There were 3 customers shown above.

See also {foreach} and $smarty.section.  

深度学习是机器学习的一个子领域,它基于人工神经网络的研究,特别是利用多层次的神经网络来进行学习和模式识别。深度学习模型能够学习数据的高层次特征,这些特征对于图像和语音识别、自然语言处理、医学图像分析等应用至关重要。以下是深度学习的一些关键概念和组成部分: 1. **神经网络(Neural Networks)**:深度学习的基础是人工神经网络,它是由多个层组成的网络结构,包括输入层、隐藏层和输出层。每个层由多个神经元组成,神经元之间通过权重连接。 2. **前馈神经网络(Feedforward Neural Networks)**:这是最常见的神经网络类型,信息从输入层流向隐藏层,最终到达输出层。 3. **卷积神经网络(Convolutional Neural Networks, CNNs)**:这种网络特别适合处理具有网格结构的数据,如图像。它们使用卷积层来提取图像的特征。 4. **循环神经网络(Recurrent Neural Networks, RNNs)**:这种网络能够处理序列数据,如时间序列或自然语言,因为它们具有记忆功能,能够捕捉数据中的时间依赖性。 5. **长短期记忆网络(Long Short-Term Memory, LSTM)**:LSTM 是一种特殊的 RNN,它能够学习长期依赖关系,非常适合复杂的序列预测任务。 6. **生成对抗网络(Generative Adversarial Networks, GANs)**:由两个网络组成,一个生成器和一个判别器,它们相互竞争,生成器生成数据,判别器评估数据的真实性。 7. **深度学习框架**:如 TensorFlow、Keras、PyTorch 等,这些框架提供了构建、训练和部署深度学习模型的工具和库。 8. **激活函数(Activation Functions)**:如 ReLU、Sigmoid、Tanh 等,它们在神经网络中用于添加非线性,使得网络能够学习复杂的函数。 9. **损失函数(Loss Functions)**:用于评估模型的预测与真实值之间的差异,常见的损失函数包括均方误差(MSE)、交叉熵(Cross-Entropy)等。 10. **优化算法(Optimization Algorithms)**:如梯度下降(Gradient Descent)、随机梯度下降(SGD)、Adam 等,用于更新网络权重,以最小化损失函数。 11. **正则化(Regularization)**:技术如 Dropout、L1/L2 正则化等,用于防止模型过拟合。 12. **迁移学习(Transfer Learning)**:利用在一个任务上训练好的模型来提高另一个相关任务的性能。 深度学习在许多领域都取得了显著的成就,但它也面临着一些挑战,如对大量数据的依赖、模型的解释性差、计算资源消耗大等。研究人员正在不断探索新的方法来解决这些问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值