creating an array and append values to it in smarty template - append

I want to create an array in smarty and do an append functionality in it! Like if I declare a variable in smarty template like {assign var=sizearr value=''} and then i want to append values to this in a loop, and i can access values like {sizearr.0}, how can i do that?

Use append. I'm not sure if this is also available in Smarty 2
{append var='sizearr' value='' index=0}

In smarty3 yould also use a more php-like approach:
{$sizearr[] = 'your value'}
and either loop through the array like
{foreach $sizearr as $value}
{$value#key}: {$value}
{/foreach}
or just hit a specific index:
{$sizearr[2]}

You can use this too:
{$sizearr[] = "Size value"}
Here you can see the full doc (Section Appending an array)

You can simply use the smarty built-in function append :
Now lets take this example:
{assign var="ages" value=[] }
{for $i=1 to 3}
{append var="ages" value=$i }
{/for}
In the above example we didn't specify index parameter in the append function, so the value will be appended at the end of the ages array.
Hope this is helpful for everyone.

Related

Running a for loop in Powershell

I am new o scripting in powershell and am from a Python background. I want to know if I'm doing this right.
I created this array and want to extract each item one by one
$M365_E3_Grps = ("O365-CHN-DomainUser,O365-Vendor-Exchange-User")
ForEach ($Indiv_Grp in $M365_E3_Grps) {
ForEach ($Indiv_Grp in $M365_E3_Grps) {
`$ADGroup = $Indiv_Grp$ADGroup = $Indiv_Grp`
I want to know if we can extract vals with a for loop like this and assign it to a variable like this.
Construct of your array
Your array is not quite correct and will be populated as a string. To create a string array you will need to quote each item in comma separated list. The parentheses are also not required.
$M365_E3_Grps = "O365-CHN-DomainUser","O365-Vendor-Exchange-User"
Your foreach keyword syntax is however correct, even if the formatting in your question was slightly off.
foreach ($Indiv_Grp in $M365_E3_Grps) {
# Assigning $Indiv_Grp to $ADGroup here is kind of redundant since
# the value is already assinged to $Indiv_Grp
$Indiv_Grp
}

How to append a character while using the conditional statement in sightly?

I need to set the href value depending on the nullity of sling:vanityPath. The hit.properties.sling:vanityPath returns the string value for sling:vanityPath property. I need to append a / before the hit.properties.sling:vanityPath value. Is it possible to do that in the following code or do I have to test it twice, but that comes with code repetition?
<a href="${hit.properties.sling:vanityPath == null? hit.path : hit.properties.sling:vanityPath # extension='html'}"/>
Concatenation or binary operators are not supported in HTL. You can either use prependPath or double test for the slash.
I don't think you can do it in one line. But you can try defining a variable for the concatenated value instead of duplicating code for the a tag. Also, do you have to check for null specifically? can't you just reverse the conditions and check if the value exists?
<sly data-sly-test.concatenatedURL="${['/', hit.properties.sling:vanityPath] # join = ''}"/>
<a href="${hit.properties.sling:vanityPath ? concatenatedURL : hit.path # extension='html'}"/>
You can use # join to concat an array:
<a href="${hit.properties.sling:vanityPath ? ['/', hit.properties.sling:vanityPath] : hit.path # join ='', 'extension='html'}"/>

Create array from keys of an array of hashtables

I have an array where each element is a hashtable. Each hashtable has the same keys. Here it is:
#(
#{"MarketShortCode"="abc";"MarketName"="Market1" },
#{"MarketShortCode"="def";"MarketName"="Market2" },
#{"MarketShortCode"="ghi";"MarketName"="Market3" },
#{"MarketShortCode"="jkl";"MarketName"="Market4" }
)
I want a nice elegant way to extract an array containing just the value of the MarketShortCode key. So I want this:
#("abc","def","ghi","jkl")
This is the best I've come up with:
$arr = #()
$hash | %{$arr += $_.MarketShortCode}
$arr
But I don't like that cos its three lines of code. Feels like something I should be able to do in one line of code. Is there a way?
Just do this:
$hash | %{$_.MarketShortCode}
That is, return the value from the block instead of adding it to an array and then dereferencing the array.
If you're using PowerShell 3+, there's even shorter way:
$hash.MarketShortCode
PowerShell automatically applies dot . to each item in an array when it's used this way, but it wasn't supported until v3.

Need to copy only a portion of array string to another array

This is my source array:
my #raw_stack = (
'a1~a2~a3~a4~a5',
'b1~b2~b3~b4~b5',
'c1~c2~c3~c4~c5',
'd1~d2~d3~d4~d5',
'e1~e2~e3~e4~e5',
);
I want get the 3rd value in '~' pattern then place that
to another array.
The other array should now look like this:
my #other_stack = (
'a3',
'b3',
'c3',
'd3',
'e3',
);
I could go about looping through the stack array then split
and push to another array, but i'm looking for a lean way
to code this.
Any ideas?
my #other_stack = map {(split/~/)[2]} #raw_stack;
Use map to list transform instead of push. Use index/substr or unpack if the items are fixed-width. This generally is faster than split, which uses regex.

Changing each entry in a list returned from powershell

I have a list of paths (filtered by special criteria). I want to mutate each entry in this list, but can't find a way (I think it's immutable). What's the best way of going about this?
Thanks
I guess you have some collection, not only list (probably array).
PS> $myList = 'first','second','third'
You can mutate the collection by indexing or just by creating new array like this:
PS> $myList[1] = '2nd'
#or
PS> $myList | % { $_.Substring(0,2) }
fi
se
th