Why is my nested IF not working in Smarty when using first and last properties? - smarty

I'm trying to create a combination of names with a first and a last property. This works, but when i nest it into another if it goes broken.
Regular code:
{{foreach from=$xxx.aaaa item='aaaa_item' name=members}}
{{if $smarty.foreach.members.last}}
& {{$aaaa.bbbb}}
{{elseif $smarty.foreach.members.first}}
{{$aaaa.bbbb}}
{{else}}
, {{$aaaa.bbbb}}
{{/if}}
{{/foreach}}
Results
name, name, name & name
Nested code:
{{foreach from=$xxx.aaaa item='aaaa_item' name=members}}
{{if $aaaa_item.cccc == "no"}}
{{if $smarty.foreach.members.last}}
& {{$aaaa.bbbb}}
{{elseif $smarty.foreach.members.first}}
{{$aaaa.bbbb}}
{{else}}
, {{$aaaa.bbbb}}
{{/if}}
{{/if}}
{{/foreach}}
Results
,name, name, name
So it only give the result of the final 'else'
Could anyone please help me?

Just found the solution actually:
{{foreach from=$xxx.aaaa item=aaaa' name=members}}
{{if $aaaa_item.cccc == "no"}}
{{if $smarty.foreach.members.first}}{{$aaaa.bbbb}}
{{elseif $smarty.foreach.members.last}} & {{$aaaa.bbbb}}
{{elseif $smarty.foreach.members}}, {{$aaaa.bbbb}}
{{/if}}
{{/if}}
{{/foreach}}

Related

go template alternating design between rows

I have a simple template fetching a list of blog posts:
<div class="row">
<div class="cell">
{{range .Pages}}
<p class="dialogue dialogue--wide">
{{.Title}}
</p>
{{end}}
<br />
<br />
<br />
</div>
</div>
I would like to alternate the design between posts e.g. the first will use a certain class like
and the second record (or even and odd records if you prefer) another p e.g.
thank you!
I suggest solving this with CSS to save yourself some trouble. You can use odd or even in nth-child:
.dalogue:nth-child(odd) {
// CSS Property
}
Alternatively, you can use the index of the items. Below, I am using the modulo operator to determine if it's odd or even.
{{ range $index, $page := .Pages }}
{{ if eq (mod $index 2) 0 }} odd {{ else }} even {{ end }}
{{end}}
Note, I am switching odd and even around. Because the index starts at 0 and not at 1. But when looking at a table, you start counting at 1 and this is also how CSS would behave.
I have implemented the mod function myself using template.Funcs.
t.Funcs(map[string]any{"mod": func(a, b int) int { return a % b }})
https://go.dev/play/p/aAupH-4CugV

XPATH - retrieve class title and the one above

<div class="option-facility">
<span class="icon icon-18 icon-ki-close ng-isolate-scope" tooltip="" title="" data-original-title="Private bath"></span>
</div>
I want XPATH to return the original title "Private Bath" and also the word "close" from above OR to be more simple, I could specify the word "Private bath" then get the status of it: "icon-ki-close" would be fine. Trying variations of this with no luck:
//title[text()='data-original-title']/following-sibling::span/#icon-ki-close
You can use the following solution to solve your problem:
substring-before(substring-after(//span[#data-original-title-'Private bath']/#class, 'icon-ki-'), ' ')

Using XPath to get complete text of paragraph with span inside

<ul>
<li class="xyz">
<div class="divClass">
<span class="ContentItem---status---dL0iS">
<span>Success</span>
</span>
<p class="ContentItem---title---37IqA">
<span>Test Check</span>
: Please display the text
</p>
</div>
</li>
<li class="xyz">
<div class="divClass">
<span class="ContentItem---status---dL0iS">
<span>Not COMPLETED</span>
</span>
<p class="ContentItem---title---37IqA">
<span>Knowledge</span> A Team
</p>
</div>
</li>
.... and so on
</ul>
This is my html structure.I have this text Test Check inside a Span and : Please display the text inside a Paragraph tag.
What i need is ,i need to identify, whether my structure contains this complete text or not Test Check: Please display the text.
I have tried multiple ways and couldn't identify the complete path.Please find the way which i have tried
//span[text()='Test Check']/p[text()=': Please display the text']
Can you please provide me the xpath for this?
I think there is one possible solution to identify within the given html text and retrieve. I hope this solves your problem.
def get_tag_if_present(html_text):
soup_obj = BeautifulSoup(html_text,"html.parser")
test_check = soup_obj.find_all(text = re.compile(r"Test Check"))
result_val = "NOT FOUND"
if test_check:
for each_value in test_check:
parent_tag_span = each_value.parent
if parent_tag_span.name == "span":
parent_p_tag = parent_tag_span.parent
if parent_p_tag.name == "p" and "Please display the text" in parent_p_tag.get_text():
result_val = parent_p_tag
break
return result_val
The returned result_val will have the tag corresponding to the p tag element with the parameter. It would return NOT FOUND, if no such element exists.
I've taken this with the assumption that the corresponding data entries would exist in a "p" tag and "span" tag respectively , feel free to remove the said conditions for all identifications of the text in the given html text.

How to know if a range is empty?

How can I conditionally display something else if my range is empty?
{{range .Users}}
...
{{end}}
If the range is empty, I want to display a different block of HTML.
Use {{range pipeline}} T1 {{else}} T0 {{end}}:
{{range pipeline}} T1 {{else}} T0 {{end}}
The value of the pipeline must be an array, slice, map, or channel.
If the value of the pipeline has length zero, dot is unaffected and
T0 is executed; otherwise, dot is set to the successive elements
of the array, slice, or map and T1 is executed.
Example:
{{range .Users}}
...
{{else}}
<p>No users</p>
{{end}}

Smarty Conditionals strings

I need to create a single string in SMARTY from two as below
{$value.b64id} = ?type=1&id=aWQ9
this is what I have got so far, cant seem to get anywhere. I think the quote marks are messing me up!!
{assign var='controls' value='<a style="color: red;" href="http://example.com'|cat:{$value.b64id}|cat:'">click Me</a>'}
so what I want from the end of it
{$controls} = <a style="color: red;" href="http://example.com?type=1&id=aWQ9">click Me</a>
Many Thanks
First of all, you only need to use delimiters for variables if you want to display them in the template. When you're using variables inside a smarty function that's not necessary, so
|cat:{$value.b64id}
should be
|cat:$value.b64id
However, if you need to compose a string to reuse it several times, it's probably better to use {capture}
{capture "controls"}
<a style="color: red;" href="http://example.com{$value.b64id}">click Me</a>
{/capture}
and then just use {$controls}

Resources