简体   繁体   English

使用Smarty模板获取img src

[英]Get img src with Smarty template

I have a Smarty template that is currently inserting a product image. 我有一个Smarty模板,当前正在插入产品图片。 What I would like to do is modify that code so that it gives me the src of the image only. 我想做的就是修改该代码,以便仅提供图像的src。

The template includes the file 'product_icon.tpl' and from what I gather this is the relevant code from that file: 模板包括文件“ 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}

Then the 'image.tpl' file that was included there contains this (trimmed to relevant part for brevity): 然后,其中包含的“ 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}

Can these snippets of code be combined and simplified down to only output the src of the resized image? 可以将这些代码片段合并并简化为仅输出经过调整大小的图像的src吗?

You can always use prin_r in tpl to check the variables 您可以随时在tpl中使用prin_r来检查变量

{$product.main_pair|print_r}

will be displayed something similar with different paths 将会显示类似的路径

 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
        )
)

Now are more easier to spot what you need 现在更容易发现您的需求

<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}" />

To resize the image on the fly please use 要即时调整图像大小,请使用

{$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}" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM