Smarty - Difficult if else to make select options - foreach

I am having real trouble getting my head around this smarty else if.
For each
{foreach $values as $value}
{if $value = a}
do action
{elseif $value = b}
do action
{elseif $value = c}
do action
{/if}
{/foreach}
I only want to 'do action' once for the entire foreach. For any one iteration of the foreach a differnt condition(a b or c) might be = to $value
The real world example that I am working on is adding 'selected' to a drop down option.
<select>
{foreach $offices as $office}<!-- The if below is done so that the form is sticky, and when editing a post, it shows the post's current setting.-->
<option
{if $smarty.post.OfficeCode == $office.OfficeCode}
selected='selected'
{elseif $bulletin_array[0].OfficeCode == $office.OfficeCode}
selected='selected'
{elseif $office.OfficeCode == $UsersOffice}
selected='selected'
{/if}
title='{$office.Description}' value='{$office.OfficeCode}'>{$office.Title}
</option>
{/foreach}
</select>
the first if will be true if the form has been submited - to make the form sticky
the second part will be true the form is being pre-filled when editing the record - ie prefil the form with the current value
the third part will be true if the user is creating a new record, in which case set it to a default option that the user usually uses.
I need some way of only allowing selected to be added once.
I php I would have a variable outside the foreach which was set to true if selected was added. I would then check that variable before doing the if elseif

{if ($smarty.post.OfficeCode eq $office.OfficeCode)
or ($bulletin_array[0].OfficeCode eq $office.OfficeCode)
or ($office.OfficeCode eq $UsersOffice)}
selected='selected'
{/if}

Related

Is it possible to put multiple conditions in a th:classappend attribute

I've been racking my brains on this for a while and I'm pretty sure I'm getting the syntax wrong.
Basically I have a <div> and want it to have the class="hidden" unless two thymeleaf conditions are met.
I want the class 'hidden' unless there's an error present on the input.
This works on it's own:
th:classappend="${#fields.hasErrors('month') == false} ? 'hidden'
I want the class hidden unless the value of the input is not null.
This works on it's own:
th:classappend="${applicationData.month == null} ? 'hidden'"
Is there anyway to concat these two conditions into one th:classappend? I'm struggling with getting that working at the moment. Something like this doesn't seem to work for me
<div id="date-met" class="date-container" th:classappend="${#fields.hasErrors('month') == false} ? 'hidden' || ${applicationData.month == null} ? 'hidden'" >
<input id="month" name="month" type="text" th:value="${applicationData.month}">
</div>
Thanks in advance and apologies if it's a stupid question!
You can use:
( condition_one || condition_two ) ? 'hidden'
So in your case that would be:
th:classappend="${ ( #fields.hasErrors('month') == false || applicationData.month == null ) ? 'hidden' }">
The parentheses may not even be necessary - just added for clarity.

Accessing the name of a smarty variables

Hello i need some help with a basic question. This is my code:
{foreach $sArticle.attributes.core->toArray() as $attribute}
<tr class="product--properties-row">
<td class="product--properties-label is--bold">{$sArticle.attributes.name}{$sArticle.attributesName}</td>
<td class="product--properties-label is--bold">{$attribute}</td>
</tr>
{/foreach}
1.Question: How do I enter the attributes name? I mean the column name from the attribute in the database?
2.Question: I only want to loop with the foreach through columns with name="artikelattr_" any idea how this can be done?
Thanks
I adjusted my answer here: https://stackoverflow.com/a/71237355/7201069
How to get the name of the attribute.
To consider only certain attributes that starts with a specific string, just add an if in the loop:
{foreach $sArticle.attributes.core->toArray() as $attributeName => $attribute}
{if $attributeName|strpos:"artikelattr_" === 0}
{$attributeName|var_dump}
{$attribute|var_dump}
{/if}
{/foreach}
You can simply use nearly all PHP functions in Smarty.

Separate link_to with comma

I want to separate this block with commas :
- game_publication.groups.each_with_index do |group, index|
= link_to store_group_path(current_store, group) do
%span= #groups.find(group).name.to_s + (index > 0 ? ', ' : '')
But for the moment it returns something like
<label>Groups :</label>
<a href="/66-store/groups/4594?locale=en">
<span>party hard</span>
</a>
<a href="/66-store/groups/5063?locale=en">
<span>b0m,</span>
</a>
<a href="/66-store/groups/5066?locale=en">
<span>test,</span>
</a>
</label>
It doesn't seems a situation where I can use any rails helpers.
I would like something like group1, group2, group3.
<label>Groups :</label>
<a href="/66-store/groups/4594?locale=en">
<span>party hard,</span>
</a>
<a href="/66-store/groups/5063?locale=en">
<span>b0m,</span>
</a>
<a href="/66-store/groups/5066?locale=en">
<span>test</span>
</a>
</label>
First, are you sure that you pasted here the exact code which gave the posted result? In your code, you have
(index > 0 ? '' : ',')
which means: Do not add a comma UNLESS we are on the first element. The result you posted had the comma the otherway round: It has a comma everywhere, EXCEPT for the first element. With other words: The code you posted, can't produce the output you posted.
Now for your problem: You want to add a comma on every element, except the last. This means that you need to know the highest (last) index value:
last_index = game_publication.groups.size - 1
With this, you can write your expression as
(index == last_index ? '' : ',')

How can remove first letters from a name in smarty?

This code find my first letter and show it. But how can remove it?
Like: From EDCstreet to street.
{foreach from=$elements item=street}
{if $street|substr:0:1 eq 'm'}
{$street}
{/if}
{/foreach}
You can use:
{foreach from=$elements item=street}
{if $street|substr:0:1 eq 'm'}
{$street|substr:1}
{/if}
{/foreach}
to display text without this first letter

ASP .Net MVC, problem with checkboxes!

Basically I have a set of checkboxes that are dynamically created from view data like so:
<input type="checkbox" name="Calendars" value="<%= c.ID %>" /><%= c.Name %>
The value being the Calendar Id.
I can get what checkbox has been brought back using the FormsCollection its messy but it works!
(There also seems to be a bug with the checkbox helper that renders a hidden field next to the checkbox which means true is actually returned as "true,false"! I can work around this so its not an issue just thought Id mention it)
The problem comes when trying to hook the checkboxes up on an edit page!
I have a schedule class which can have multiple calendars and I want to show which calendars a schedule has by checking them on the edit!
My view is strongly typed but MVC magic can't map this!
Any ideas on whats the best way to do this??
I had tried passing the calendar ids in ViewData and do some inline code to check the appropriate checkbox but this is getting messy!
Thanks!!
UPDATE:
Done this
s.ID == c.ID).Select(s => s).Count() > 0) ? "checked=checked" : "" %>
You need to add "checked" tag manually to every check box:
<input type="checkbox" name="Calendars" value="<%= c.ID %>" checked="checked" /><%= c.Name %>
You dont need <input type="checkbox" - use Html.Checkbox(). It renders a hidden field next to the checkbox - but it is not a bug. From ASP.NET MVC source, InputExtensions.cs, line 201:
// Render an additional <input type="hidden".../> for checkboxes. This
// addresses scenarios where unchecked checkboxes are not sent in the request.
// Sending a hidden input makes it possible to know that the checkbox was present
// on the page when the request was submitted.
Use this:
<%= Html.CheckBox("Calendars", c.ID) %>

Resources