Search code examples
smartycs-cart

Smarty target last item


So after using cscart for a while now and learning everyday I have encountered something which I find rather annoying.

So whenever you add multiple values to a feature it will show them like f.e. "OrangeGreen" whilst I want it to view it like "Orange, Green". Well seems easy, just change the product_features.tpl

{elseif in_array($feature.feature_type, ["ProductFeatures::TEXT_SELECTBOX"|enum, "ProductFeatures::EXTENDED"|enum, "ProductFeatures::NUMBER_SELECTBOX"|enum])}
                {foreach from=$feature.variants item="var"}
                    {if $var.selected}{$var.variant}, {/if}
                {/foreach}

But now it won't be "Orange, Green". Now it is "Orange, Green,"

So could you guys help me and figure out how I can target the last item in this piece of code?


Solution

  • You can use foreach loop's properties like index, first and last to access particular elements in this loop.

    In Smarty V2 your foreach loop needs a name attribute for accessing it's properties:

    {foreach from=$feature.variants item="var" name="features"}
      {if $var.selected}{$var.variant}, {/if}
      {if $smarty.foreach.features.last} this is the last element in this loop{/if}
    {/foreach}
    

    (docs: http://www.smarty.net/docsv2/en/language.function.foreach.tpl#foreach.property.last)

    Smarty V3 is even simplier:

    {if $var@last} this is the last element in this loop{/if}
    

    (docs: http://www.smarty.net/docs/en/language.function.foreach.tpl#foreach.property.last )

    sorry, i'm not sure if cs-cart works with Smarty V2 or V3