+1 vote
by (130 points)
Found an question in the old forum named "How do you pick random images from a folder!", but i can not for my life concatenate a $variable and .jpg to get it work.

I am trying to do exactly what the old question ask, but in sugarcube.

Hope someone can help.

1 Answer

0 votes
by (8.6k points)

Show your code. Otherwise, this should work just fine:

<<set $variable = "http://www.publicdomainpictures.net/pictures/160000/velka/wooden-weave-">>
[img[$variable + random(1, 5) + ".jpg"]]

 

by (130 points)
Yeah, i am still very confused...

 

This is what i got: (as you can see i am using local folders to store the images)

<<set $random to random(1,5)>> //setting the random variable

random is:$random //Printing random var

[img[images/$random + ".png"]]  //Suggested solution, here i get nothing, not even a broken link

<img src="images/$random + .png"> //Testing since i am using this method to display images. Displays broken link

<img src="images/1.png"> //Static display of image to verify, works fine.
by (8.6k points)

Your "broken" variants should be written like this:

[img["images/" + $random + ".png"]] 
<<= "<img src='images/" + $random + ".png'>">>

The first syntax is simpler, but doesn't offer you quite as much flexibility in terms of <img> attributes. If all you need is the "src" one though, I'd suggest you use it.

by (130 points)
Perfect!

Thanks a lot!
...