Passing Array to Snippet

I want to pass an array of sizes to a snippet. Here’s my call to the snippet:

<?php snippet("images/sizes", array("sizes" => array("100px", "200px", "300px", "400px", "500px"))) ?>

And here’s my snippet:

(min-width: 1280px) <?= $sizes[4] ?>,
(min-width: 1024px) <?= $sizes[3] ?>,
(min-width: 768px) <?= $sizes[2] ?>,
(min-width: 640px) <?= $sizes[1] ?>,
<?= $sizes[0] ?>

Is there a cleaner way to do this? It works, I’m just not loving the execution.

I just realized the second array was redundant, this is a little better:

<?php snippet("images/sizes", array("sizes" => ["100px", "200px", "300px", "400px", "500px"])) ?>

The only difference between the first and the second example is that in the second one, you are mixing long and short array syntax. That should be avoided.

Long array syntax:

$a = array();

Short array syntax:

$a = [];

In all of your code, you should always stick to one notation for consistency, not mix, so either

array("sizes" => array("100px", "200px", "300px", "400px", "500px"));

or

['sizes' => ["100px", "200px", "300px", "400px", "500px"]]

Instead of passing an array of sizes to a single key, I’d pass an associative array:

<?php snippet('images/sizes', ['size_100' => '100px', 'size_200' => '200px', 'size_300' => '300px', 'size_400' => '400px', 'size_500 => '500px']) ?>

Then in your snippet:

(min-width: 1280px) <?= $size_500 ?>,
(min-width: 1024px) <?= $size_400 ?>,
(min-width: 768px) <?= $size_300 ?>,
(min-width: 640px) <?= $size_200 ?>,
<?= $size_100 ?>