if else在smarty .tpl中为nth-child list / div条件

时间:2012-07-15 18:03:55

标签: php css3 smarty

我正在和Smarty一起工作。我想在我的if else文件中为n-child li / div类添加.tpl条件。

我有两种图像尺寸。我想在第一个li中使用600px图像,在第二个和第三个中使用400px图像,在第四个li中再使用600px图像,依此类推。

目前我正在使用CSS3 nth-child选择器并使用缩放的400px图像。但是加载时间很糟糕。我想在需要时使用较小的图像。

我猜它非常复杂。是吗?

这是我的代码:

<img src="{$purl}/thumbs/{$posts[i].pic}" alt="{$posts[i].story|stripslashes}" />

并且它存在于{section name=i loop=$posts} {include file="posts.tpl"} {/section}内的循环posts.tpl中。

1 个答案:

答案 0 :(得分:2)

cycle会有所帮助。

假设您有一个数组,其中包含存储在两个字段中的图片路径:image-600image-400

{foreach from=$images item=image}
    {capture assign=currentKey}image-{cycle values='600,400,400'}{/capture}
    <li><img src="{$image[$currentKey]}" alt="{$image.title}" /></li>
{/foreach}

在你的情况下,它可能看起来像这样:

{section name=i loop=$posts}
   {capture assign=thumbSize}{cycle values='big,small,small'}{/capture}
   <img src="{$purl}/thumbs/{if $thumbSize eq 'big'}{$posts[i].pic}{else}s-{$r[i].pic}{/if}" alt="{$posts[i].story|stripslashes}" />
{/section}