【问题标题】:if else condition in smarty .tpl for nth-child list/div如果在 smarty .tpl 中为 nth-child list/div 设置其他条件
【发布时间】:2012-07-14 17:04:20
【问题描述】:

我正在与 Smarty 合作。我想在我的.tpl 文件中为 nth-child li/div 类添加一个if else 条件。

我有两种图像尺寸。我想在第一个li中使用600px的图像,在第二和第三个中使用400px的图像,在第四个li中再次使用600px的图像等等。

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

我猜它非常复杂。是吗?

这是我的代码:

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

它存在于循环{section name=i loop=$posts} {include file="posts.tpl"} {/section}posts.tpl

【问题讨论】:

    标签: php css smarty


    【解决方案1】:

    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}
    

    【讨论】:

    • 请检查上面的代码。谢谢你的帮助。我研究过循环,这似乎是正确的方法。
    • 我的图片标签是{$purl}/thumbs/s-{$r[i].pic} for 400px 和{$purl}/thumbs/{$posts[i].pic} for 600px 希望它能帮助你理解
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-20
    • 1970-01-01
    • 1970-01-01
    • 2016-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多