smarty name of variable dynamic - templates

I'm studying smarty for about two months and I find myself be in front of a problem.
I am creating a dynamic menu that reads the configuration from a file called submenu.conf in this file are:
tot_sub_menu_2 = "2"
text_sub_menu_2_1 = "Home"
text_sub_menu_2_2 = "about"
tpl file in my application I wish he would create the menu dynamically in this way:
{for $foo=1 to #tot_sub_menu_2#}
<li>{#text_sub_menu_2_.{$foo}}</li>
{/for}
I would like to take a dynamic parameter text_sub_menu_2_1 the second loop becomes text_sub_menu_2_2 etc.
someone knows how to help me?

I have not tested but you can try using {counter}
{for $foo=1 to #tot_sub_menu_2#}
{* initialize the count *}
{counter start=0 skip=1}
<li>{#text_sub_menu_2_.{counter}}</li>
Otherwise you can make use of the #iteration property. See http://www.smarty.net/docs/en/language.function.foreach.tpl#foreach.property.iteration
iteration contains the current loop iteration and always starts at
one, unlike index. It is incremented by one on each iteration.
{foreach from=$new_products item='product' name='newProducts'}
// Do somthing
//Here i want to increment {$count} if it's lower than 3 or set to 1 if higher than 3
{if $product#iteration <= 3}{$count = $product#iteration}{else}{$count = 1}{/if}
{/foreach}

Related

Dividing lists in Svelte

I had a recent urge to play with reading and formatting a GEDCOM (Genealogy) file and I'm using Svelte to accomplish this. But I'm stuck on a problem I'm not quite sure the best way to do it. Extracted from the GEDCOM file is a collection of names sorted by surnames. Let's say it's this:
Adams, John
Anderson, Neo
Cash, Johnny
Clapton, Eric
Cross, Christopher
Denver, John
The real list is quite a bit bigger and needs indicators for the letters of the alphabet. Essentially, I want to have the following as output:
Names starting with A:
Adams, John; Anderson, Neo;
Names starting with C:
Cash, Johnny; Clapton, Eric; Cross, Christopher;
Names starting with D:
Denver, John;
I have no problem spewing out the list. Svelte makes that easy enough. What I haven't figured out yet is how to interject the breaks. Each thing I've tried or thought about seems to be flawed. As far as I can tell, there's no good way to simply alter a variable as a loop progresses and there doesn't seem to be a way to get the previous item in the loop. Any suggestions for an approach?
I currently have this, but where to begin injecting the header?
{#each data.sort(compare) as n }
{#if n.ReverseName }
{n.ReverseName}
{/if}
{/each}
First of all, you want to ask yourself, "what is the format of the data output I want to obtain?". Considering your question and your objective to be able to loop through the output, you'd probably want the following structure:
output = [
[array of names starting with A, if any],
[array of names starting with B, if any],
etc.
]
I believe a relatively straightforward way to achieve this is with a reducer:
// start with sorting the input array so names are
// already properly sorted before being input to our reducer
const output = input.sort().reduce((acc, current) => {
// if the accumulator is an empty array,
// return the current value in a new sub array
// (the very first name pushed into the very first sub array)
if (acc.length === 0) {
return [ [ current ] ];
}
// otherwise, if the initial of the first name in
// the current (i.e. last created) sub array matches
// the initial of the current value, add that current
// value to the current sub array
const [ currentSub ] = acc.slice(-1);
if (currentSub[0].charAt(0) === current.charAt(0)) {
return [ ...acc.slice(0, -1), [ ...currentSub, current ] ];
}
// else add a new sub array and initialise it with the current value
return [ ...acc, [ current ] ];
}, []); // initialise with an empty array
The output array will have the desired shape [ [names starting with A, if any], [names starting with B, if any], ... ] and all that's left is for you to iterate through that output array:
{#each output as letterArray}
<p>Names starting with <strong>{letterArray[0].charAt(0)}</strong>:</p>
<p>{letterArray.join('; ')}</p>
{/each}
Or if you want to iterate over individual names, add an inner each block that iterates over letterArray. Many possibilities exist once your data is shaped the way you want it to be.
Demo REPL
Edit:
If input is dynamic (i.e. it is updated through the life of the component, either via a fetch or because it is received as a prop that might get updated), you can keep output automatically updated by turning its declaration into reactive code:
$: output = input.sort().reduce(...same code as above...)

How to extract a column based on it's content in PowerBI

I have a column in my table which looks like below.
ResourceIdentifier
------------------
arn:aws:ec2:us-east-1:7XXXXXX1:instance/i-09TYTYTY79716
arn:aws:glue:us-east-1:5XXXXXX85:devEndpoint/etl-endpoint
i-075656565f7fea3
i-02c3434343f22
qa-271111145-us-east-1-raw
prod-95756565631-us-east-1-raw
prod-957454551631-us-east-1-isin-repository
i-02XXXXXXf0
I want a new column called 'Trimmed Resource Identifier' which looks at ResourceIdentifier and if the value starts with "arn", then returns value after last "/", else returns the whole string.
For eg.
arn:aws:ec2:us-east-1:7XXXXXX1:instance/i-09TYTYTY79716  ---> i-09TYTYTY797168
i-02XXXXXXf0 --> i-02XXXXXXf0
How do I do this ? I tried creating a new column called "first 3 letters" by extracting first 3 letters of the ResourceIdentifier column but I am getting stuck at the step of adding conditional column. Please see the image below.
Is there a way I can do all of this in one step using DAX instead of creating a new intermediate column ?
Many Thanks
The GUI is too simple to do exactly what you want but go ahead and use it to create the next step, which we can then modify to work properly.
Filling out the GUI like this
will produce a line of code that looks like this (turn on the Formula Bar under the View tab in the query editor if you don't see this formula).
= Table.AddColumn(#"Name of Previous Step Here", "Custom",
each if Text.StartsWith([ResourceIdentifier], "arn") then "output" else [ResourceIdentifier])
The first three letters bit is already handled with the operator I chose, so all that remains is to change the "output" placeholder to what we actually want. There's a handy Text.AfterDelimiter function we can use for this.
Text.AfterDelimiter([ResourceIdentifier], "/", {0, RelativePosition.FromEnd})
This tells it to take the text after the first / (starting from the end). Replace "output" with this expression and you should be good to go.

counting up in list.generate, short flutter question

i recently started learning flutter and dart, so my apologies if this is a really simple problem, i am trying to generate a list with values from another list, as follows:
List transactions = List.generate(15, (index)=>{
“name”: names[1],
“dp”: “assets/cm1.jpeg”,
});
but i want the numbers 1 to increase by 1 with every new entry. so the list should be outputting the names in my names list 1 by 1 and cm1.jpeg, cm2.jpeg etc. similar to the i+1 code.
Thanks in advance!
You can use the index variable to get the index of the current element you are generating. So first element will run the generate method with an index value of 0, the second will have the value 1 and so on.
With this knowledge you can write something like:
List transactions = List.generate(15, (index)=>{
“name”: names[1 + index],
“dp”: “assets/cm${1 + index}.jpeg”,
});

How to manage looping on this list on Applscript?

The list is in the form of:-
0:
https://url
1:
https://url
..... And so on.
How could I loop on this list. So I could fetch the number first without ":" and type it somewhere then fetch the url that comes after that number and type it elsewhere. Then end repeat if the list is over.
Or should I use records instead?
I am still a beginner using AppleScript. I tried many commands I mixed up but the computer keeps running the script nonestop and the activity monitor shows the applescript using 100% of the processor and huge amount of ram.
Appreciate any help.
Thank you
You didn't define what your list really looks like very well so I made an assumption on my answer below. If I was wrong, hopefully my answer will at least point you in the right direction. (or if I've gotten it wrong, but you can choose to reformat it to the way I suggested, that could still help)
on run
set theList to {"0:http://apple.com", "1:http://google.com"} -- my guess at what your list looks like.
repeat with anItem in theList
set anItem to anItem as string
set itemParts to myParseItem(anItem)
set tID to the_integer of itemParts as integer
set tURL to the_url of itemParts as string
end repeat
end run
on myParseItem(theItem)
set AppleScript's text item delimiters to ":"
set delimitedList to every text item of theItem
set newString to (items 2 thru -1 of delimitedList as string)
set AppleScript's text item delimiters to {""}
set theInt to item 1 of delimitedList
set theURL to newString as string
return {the_integer:theInt, the_url:theURL}
end myParseItem

giving a string variable values conditional on another variable

I am using Stata 14. I have US states and corresponding regions as integer.
I want create a string variable that represents the region for each observation.
Currently my code is
gen div_name = "A"
replace div_name = "New England" if div_no == 1
replace div_name = "Middle Atlantic" if div_no == 2
.
.
replace div_name = "Pacific" if div_no == 9
..so it is a really long code.
I was wondering if there is a shorter way to do this where I can automate assigning values rather than manually hard coding them.
You can define value labels in one line with label define and then use decode to create the string variable. See the help for those commands.
If the correspondence was defined in a separate dataset you could use merge. See e.g. this FAQ
There can't be a short-cut here other than typing all the names at some point or exploiting the fact that someone else typed them earlier into a file.
With nine or so labels, typing them yourself is quickest.
Note that you type one statement more than you need, even doing it the long way, as you could start
gen div_name = "New England" if div_no == 1