【问题标题】:Get img src with Smarty template使用 Smarty 模板获取 img src
【发布时间】:2017-02-20 08:29:58
【问题描述】:

我有一个 Smarty 模板,目前正在插入产品图片。我想做的是修改该代码,以便它只给我图像的 src。

该模板包含文件“product_icon.tpl”,据我所知,这是该文件中的相关代码:

{capture name="main_icon"}
    <a href="{"$product_detail_view_url"|fn_url}">
        {include file="common/image.tpl" obj_id=$obj_id_prefix images=$product.main_pair image_width=$settings.Thumbnails.product_lists_thumbnail_width image_height=$settings.Thumbnails.product_lists_thumbnail_height}
    </a>
{/capture}

然后其中包含的“image.tpl”文件包含以下内容(为简洁起见,已修剪到相关部分):

{capture name="product_image_object"}
{** Sets image displayed in product list **}
{hook name="products:product_image_object"}
    <img class="ty-pict {$valign} {$class} {if $lazy_load}lazyOwl{/if} {if $generate_image}ty-spinner{/if} cm-image" {if $obj_id && !$no_ids}id="det_img_{$obj_id}"{/if} {if $generate_image}data-ca-image-path="{$image_data.image_path}"{/if} {if $lazy_load}data-{/if}src="{if $generate_image}{$images_dir}/icons/spacer.gif{else}{$image_data.image_path}{/if}" alt="{$image_data.alt}" title="{$image_data.alt}" {if $image_onclick}onclick="{$image_onclick}"{/if}  {if $image_width || $image_height} style="min-width: {$image_data.width}px; min-height: {$image_data.height}px; "{/if}/>
    {if $image_data.alt}<span class="stonestreets-caption">{$image_data.alt}</span>{/if}
{/hook}
{/capture}

这些sn-ps的代码能不能合并简化成只输出调整后图片的src?

【问题讨论】:

    标签: php smarty cs-cart


    【解决方案1】:

    您总是可以在 tpl 中使用 prin_r 来检查变量

    {$product.main_pair|print_r}
    

    会以不同的路径显示类似的东西

     Array
    (
        [pair_id] => 1072
        [image_id] => 0
        [detailed_id] => 1285
        [position] => 0
        [detailed] => Array
            (
                [object_id] => 247
                [object_type] => product
                [image_path] => http://localhost/cs-cart-git-dev/images/detailed/1/nokia_n1_perspectives_-_app.jpg
                [alt] => 
                [image_x] => 5000
                [image_y] => 3096
                [http_image_path] => http://localhost/cs-cart-git-dev/images/detailed/1/nokia_n1_perspectives_-_app.jpg
                [https_image_path] => https://localhost/cs-cart-git-dev/images/detailed/1/nokia_n1_perspectives_-_app.jpg
                [absolute_path] => /Applications/MAMP/htdocs/cs-cart-git-dev/images/detailed/1/nokia_n1_perspectives_-_app.jpg
                [relative_path] => detailed/1/nokia_n1_perspectives_-_app.jpg
            )
    )
    

    现在更容易发现你需要的东西

    <img src="{$product.main_pair.detailed.image_path}" width="{$product.main_pair.detailed.image_x}" height="{$product.main_pair.detailed.image_y}" alt="{$product.main_pair.detailed.alt}" />
    

    要即时调整图像大小,请使用

    {$image_data=$product.main_pair|fn_image_to_display:$image_width:$image_height}
    <img src="{$image_data.detailed.image_path}" width="{$image_data.detailed.image_x}" height="{$image_data.detailed.image_y}" alt="{$image_data.detailed.alt}" />
    

    【讨论】:

    • 这真的很方便谢谢。但这给了我原来的大小。如何找出调整后图像的路径?
    • 对不起,这没有给我任何东西?
    • 您是否将 $image_width:$image_height 更改为您自己的值?
    • 是的,通过设置变量,例如。 {$image_width=500} 并直接替换为数字。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-09
    • 2012-06-19
    • 2012-09-10
    • 1970-01-01
    • 2013-04-17
    相关资源
    最近更新 更多