# Using images inside structure field

**URL:** <https://forum.getkirby.com/t/using-images-inside-structure-field/2566>\
**Category:** Questions\
**Tags:** v2\
**Created:** [November 20, 2015, 11:59am UTC](https://forum.getkirby.com/t/using-images-inside-structure-field/2566 "2015-11-20T11:59:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![phrase](https://avatars.discourse-cdn.com/v4/letter/p/dfb087/32.png) [@phrase](https://forum.getkirby.com/u/phrase)\
**Post date:** [November 20, 2015, 11:59am UTC](https://forum.getkirby.com/t/using-images-inside-structure-field/2566/1 "2015-11-20T11:59:30Z")

</div>

Hi. For a project I’m working on I needed to add an image inside a structure field, that will then be available to the template. I used the following code to do this, but for some reason I think my solution is not very elegant and could be simpler. Any ideas?

Blueprint

```
publications:
    label: Publications
    type: structure
    fields:
        name:
            label: Name
            type: text
        info:
            label: Info
            type: text
        preview:
            label: Preview
            type: select
            options: images    
    width: 1/2 

```

Template

```
<?php $publications = yaml($page->publications()) ?>
<?php foreach($publications as $publication): ?>
    <div class="publication">
        <div class="name">
            <?php echo $publication['name'] ?><br />
        </div>
        <div class="info">
            <?php echo $publication['info'] ?>
        </div>
        <div class="preview">
            <?php $previewName = $publication['preview'] ?>
            <?php if ($previewName != ""): ?>
                <?php $previewUrl = $page->files()->find($previewName) ?>
                <?php echo $previewUrl->crop(400, 250) ?>
            <?php endif ?>
        </div>
    </div>
<?php endforeach ?>

```

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [November 20, 2015, 12:05pm UTC](https://forum.getkirby.com/t/using-images-inside-structure-field/2566/2 "2015-11-20T12:05:58Z")

</div>

Your code is perfectly alright. The only thing you could shorten is this line:

```php
<?php $previewUrl = $page->files()->find($previewName) ?>

```

to

```auto
<?php $previewUrl = $page->file($previewName) ?>

```

Other than that, you could use the `toStructure()` method instead of the `yaml()` method.

---

<div class="post-metadata">

**Author:** ![phrase](https://avatars.discourse-cdn.com/v4/letter/p/dfb087/32.png) [@phrase](https://forum.getkirby.com/u/phrase)\
**Post date:** [November 20, 2015, 1:07pm UTC](https://forum.getkirby.com/t/using-images-inside-structure-field/2566/3 "2015-11-20T13:07:38Z")

</div>

Ah, good to know! Still your recommendations help as I’m trying to keep code as clean as possible. Thanks.

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [November 20, 2015, 1:14pm UTC](https://forum.getkirby.com/t/using-images-inside-structure-field/2566/4 "2015-11-20T13:14:14Z")

</div>

Just on a side note, you can also use chaining syntax instead of the brackets (was introduced a while ago and makes the code more coherent:

```php
<?php $publications = $page->publications()->yaml() ?>

```

---

<div class="post-metadata">

**Author:** ![distantnative](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/distantnative/32/11319_2.png) [@distantnative](https://forum.getkirby.com/u/distantnative)\
**Post date:** [February 6, 2025, 5:04pm UTC](https://forum.getkirby.com/t/using-images-inside-structure-field/2566/5 "2025-02-06T17:04:11Z")

</div>

Archived on February 6, 2025, 5:04pm UTC


