Smarty target last item - smarty

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?

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

Related

Display product available sizes on product miniature Prestashop 1.7

I would like to know how to show available product sizes on product miniature in Prestashop 1.7.
Variable $product.size gives an array so I tried somehow to use
<ul>
{foreach from=$product key=?? item=??}
<li>{$product.size}</li>
{/foreach}
</ul>
but doesn't work. Exactly it returns maybe 20 empty
file is located in your_theme/templates/catalog/_partials/miniatures/product.tpl
Could somebody help me?
Thanks is advance
Unfortunately in 1.7 you can't just display all attributes related to product, without additional modifications. By default, you have only access to "default" attribute.
There are number of additional modules to do that, you can search them by looking for "PrestaShop Attributes on product list" on both official and third party marketplaces.
If you want to consider doing modification by your own, I suggest to look at the Product::getProductsProperties method where you have a code which is used to get all informations about the product displayed on the list.
Display defaut attributes on product miniature Prestashop 1.7
file located : your_theme/templates/catalog/_partials/miniatures/product.tpl
{*------Display default attributes in product list-----*}
{if isset($product.attributes) && !empty($product.attributes)}
<span class="default-attributes">
{foreach from=$product.attributes item=attribute}
{$attribute.group} : {$attribute.name}
{/if}
{/foreach}
</span>
{/if}
For me works the solution by Idriss el basrii thanks to him
but you just delete the {/if} closure tag inside foreach :
{*------Display default attributes in product list-----*}
{if isset($product.attributes) && !empty($product.attributes)}
<span class="default-attributes">
{foreach from=$product.attributes item=attribute}
{$attribute.group} : {$attribute.name}
{/foreach}
</span>

smarty cs-cart assign a link to a variable

I am learning smarty. I want to assign a link to a variable:
{assign var="book_link" value="`$smarty.capture.$name` nofilter"}
I want to use truncate to produce that book_link, so i use
<div class="title-book">{$book_link|truncate:50}</div>
But it did not work.
just change the double quotes from value and try this
{assign var="book_link" value=`$smarty.capture.$name` nofilter}

Implementing the 360 magic spin in smarty

I can't integrate the 360 magic spin in smarty templates. While adding the following code
<a class="Magic360" href="assets/spin-images/Bar-360-01.jpg" data-magic360-options="filename: Bar-360-{col}.jpg;">
While adding the attribute of data-magic360-options with filename, the screen will goes blank.
I amusing smarty 2.x.
Smarty (PHP framework) counts all strings {..} like its own directives.
To avoid that, you should use the following code:
{literal}<a class="Magic360" href="assets/spin-images/Bar-360-01.jpg" data-magic360-options="filename: Bar-360-{col}.jpg;">{/literal}
or
<a class="Magic360" href="assets/spin-images/Bar-360-01.jpg" data-magic360-options="filename: Bar-360-{ldelim}col{rdelim}.jpg;">
Did you forget $ sign for col variable?
data-magic360-options="filename: Bar-360-{col}.jpg;"
Maybe you should use {$col}:
<a class="Magic360" href="assets/spin-images/Bar-360-01.jpg" data-magic360-options="filename: Bar-360-{$col}.jpg;">

Smarty - Display a Div Based on the Presence or Absence of a {CAPTURE} variable

I have defined a {CAPTURE} variable in Smarty using:
{capture name='websitediv'}
//code to generate some output to be captured.
{/capture}
and assigned the output to a template variable
{capture name='websitediv' assign='ws'}
I have condition set in my code whereby depending whether the above captured variable has a value or not, the contents of will be shown or hidden:
<div {if !isset($ws)} style="display:none" {/if}>
//else do something
</div>
Unfortunately, the last code does not work. No matter is the captured variable is available or not, the div remains displayed.
Just like a PHP variable, there is a difference between a Smarty variable being "unset", and it simply having a value which looks empty to a human.
In this case, your {capture} block is always processed, and always assigned to the variable, so the variable will always exist, and have some string content in it.
What you need to test is not its existence, but its content - is it an empty string, or, more likely, a string containing only the whitespace that sits between your Smarty tags.
Like in PHP, a completely empty string evaluates to false in a Smarty {if} statement, so you can say {if !$ws}...{/if} to detect that. But you want to ignore the whitespace, so what you probably want is {if !trim($ws)}...{/if}
<div style="{if !isset($ws)}display:none{/if}">
//else do something
</div>
With Smarty you've probably got bootstrap too, try this.
<div class="{if !isset($ws)}d-none{/if}">
//else do something
</div>

How can I detect homepage.tpl in WHMCS in Smarty?

I currently edit one template of WHMCS, it's written by Smarty.
Can I use something simple like
{if $is_homepage}
show something
{/if}
Which homepage is loaded from homepage.tpl, mean index page. Because I want to load some code, just in homepage only.
Thanks.
quite hacky but you might try something like this:
{if ($smarty.server.SCRIPT_NAME eq "/index.php")}
{/if}
Use this instead if you desire optimized code.
{if $filename eq "index"}
your code here
{/if}
you don't have to include the filename extension like .tpl (just in case you were wondering)

Resources