This is a read-only archive of the Framer Community on Facebook.

What is Framer? Join the Community
Return to index
Jon Arnold
Posted Jan 16 - Read on Facebook

Is there a efficient way to use a loop to generate similar layers with slightly differences?

What I have:
labels = ["stockCountLabel", "sumPriceLabel"]

for i in labels
i = new Layer()
i.width = screenWidth
i.height = 80
i.x = 0

switch i
when "sumPriceLabel"
i.html = 200
i.y = 580
when "stockCountLabel"
i.html = 600
i.y = 422
else
i.html = "yolo"
i.y = 400

i.backgroundColor = "transparent"
i.style = {
"padding": "0px"
"font-size": "60px"
"line-height": "1.4em"
"text-align": "center"
"color": "#333"
}

I would like to access stockCountLabel and sumPriceLabel later in code and use them.
I know that I am overwriting "i" in my code…

Thank you for your help!

10 Comments

Koen Bok

I would either do it like this, or just set the distinct values after you set the defaults for all of them (outside of the loop).

Jon Arnold

thanks for the quick reply!but how can I write the variable in the loop so that i can call print sumPriceLabel outside of the loop?

Koen Bok
Jon Arnold

sorry, but I meant that I want to access the layers later on:
layerNames = ["layerA", "layerB"]
layers = {}

for layerName in layerNames
layers[layerName] = new Layer backgroundColor:"red"

layerA.x = 50

Jon Arnold

"can't find variable: layerA" :-(

Jordan Robert Dobson

Almost, find it like this.

layers["layerA"].x = 50

Or

layers.layerA.x = 50

Jon Arnold

But I would like to write layerA.x = 50 ;-)

Koen Bok

Well, then you could use the window object. But be careful not to overwrite something else like "console" or "Screen"

Jordan Robert Dobson

labels = ["stockCount", "sumPrice"]
label = {}

Then loop...

label.stockCount.x = 50

That should get you damn close without messing with the window object.

Mohamed Tedjani Meftah

This is pushing it too far, but you can extend the layer class and make each label a custom class.

Read the entire post on Facebook