<p>The data in Craft is handled mostly by Models (and arrays of Models). Before pulling actual information, you define your criteria in an <a href="http://buildwithcraft.com/docs/templating/elementcriteriamodel">ElementCriteriaModel</a> using generic parameters and parameters specific to what you are trying to get (<a href="http://buildwithcraft.com/docs/templating/craft.entries">entries</a>, <a href="http://buildwithcraft.com/docs/templating/craft.assets">assets</a>, <a href="http://buildwithcraft.com/docs/templating/craft.categories">etc.</a>). You then use one of the ElementCriteriaModel's functions to pull the actual data. You will get either a model or an array of models to loop over. This works the same for most things in Craft because most things are Elements.</p>

<p>You can look under the Variables heading in the side menu of the <a href="http://buildwithcraft.com/docs/templating/">templating docs</a> to see the kinds of models you will get.</p>

<p>On your homepage, you start with an automatically created <code>entry</code> model (this is created on every page that Craft recognizes as an entry's page). Here's the basic code you need:</p>

<pre><code>{% for event in entry.featuredEvent %}
    {# Output some event code here #}
    {% set block = event.articleBody.type('image').first() %}
    {% for image in block.image.find() %}
        {# Output some image code here #}
        &lt;img src="{{ image.getUrl(image.size) }}" class="{{ image.alignment }}"&gt;
    {% endfor %}
{% endfor %}
</code></pre>

<p>So an explanation of the above. In Craft, related elements are not automatically pulled from the DB. Instead each field that points to a related element (entries, assets, categories, users, etc.) is an <code>ElementCriteriaModel</code> so that you add more criteria before pulling. So <code>entry.featuredEvent</code> is an <code>ElementCriteriaModel</code> that points to all of your featured event entries.</p>

<p>When working with <code>ElementCriteriaModel</code> you have a few options to pull the data and use it. In the above code, when you give a <code>for</code> loop <code>ElementCriteriaModel</code> it will automatically pull the data and give the loop entries. So we take the <code>entry.featuredEvent</code>, and loop over the entries that's given to us when we use it in the <code>for</code> loop.</p>

<p>Inside the for loop, we pull the first image block in <code>event.articleBody</code>. Because matrix blocks are elements, we get the same features. So we set the type, and pull the first one.</p>

<p>In the next <code>for</code> loop, we are grabbing all of our images in the image block. The tricky part here is that we can make the code a little more efficient by simply pulling all of the images and looping over them. If you have very different html you plan on using for one image and multiple images, you will need to <code>set</code> the images as a variable like we did the block. You can then check the number of images using the <code>length</code> filter, and either Craft's <a href="http://buildwithcraft.com/docs/templating/tags#switch"><code>switch</code></a> tag, or a couple <code>if</code> statements.</p>

<p>Personally, I recommend using the loop, and using CSS to make the style differences you need.</p>

<p>Finally, we output each image. Because you're using a transform on them, we can't use each assets <code>getImg</code> function. Instead we write the img tag manually and grab the transformed url using <code>getUrl</code>.</p>

<p>The next bit is making a couple assumptions. I'm assuming that <code>size</code> is a image transform handle. I'm also assuming that these fields are on the asset. If they're on the matrix block, replace <code>image.size</code> and <code>image.alignment</code> with <code>block.size</code> and <code>block.alignment</code>.</p>

CC BY-SA 3.0
2014-06-30T23:26:59.950Z
That means that the image field you mentioned isn't called image, or isn't an asset reference. It could also be a few other things, but I can't know without looking at it.
Bryan Redeagle
CC BY-SA 3.0
2014-06-30T22:17:15.693Z
2014-06-30T22:17:15.693Z
Bryan Redeagle
<p>I'm having a bit of a hard time wrapping my head around exactly how to work with both Relationships and Assets. Here is my current problem that I seem to be stumped on, i'm sure once I get this going I'll be good to go and fully understand how to work with both:</p>

<p>I need to determine if an entry is selected to be related to an entry single (the homepage) via the entry field 'featuredEvent', then
I need to determine if the 'image' block is being used in the matrix 'articleBody' in that related entry, then
I need to pull the first instance of the 'image' block (not necessarily the first block altogether), then
I need to determine if there is more than one 'asset/image' in the 'image' field in the 'image' block in the related entry, then
If there is only one image I need to output some code, and then output the image, as well as the other parameters 'size' &amp; 'alignment', which are both drop downs, 'size' selects the pre-determined 'imageTransform' where as 'alignment' just applies a class or
If there are multiple images i need to output some code, and then output the images, as well as the other parameters 'size' &amp; 'alignment'.</p>

<p>Could anybody write that code out for me and explain what is happening in each line to help me get a better grasp? Thank you so much in advance.</p>

CC BY-SA 3.0
2014-06-30T20:24:34.050Z
What you're asking for is very involved. Rather than have us simply do all the work for you, could you edit the question with what you have thus far? Even if it's not working, we can instead help you fix what's wrong and explain more along the way.
Bryan Redeagle
CC BY-SA 3.0
2014-06-30T20:25:40.853Z
I also recommend making your question more descriptive of what you're asking for in the details.
Bryan Redeagle
CC BY-SA 3.0
2014-06-30T20:44:33.783Z
I've done a lot of trial and error and nothing has stuck since i don't quite grasp the way the data works yet. Hence asking to 'explain what is happening in each line' so i can get a grasp and be able to figure out other similar issues on my own. Not really sure how to be more descriptive, I tried to break it down literally item by item of what needs to happen. I have a single (the homepage) that has an 'entry' field to relate an entry and show it on the homepage. I need to then look in that entry for a particular matrix block type, grab the first of that type, and then the assets deal.
Christopher Healey
CC BY-SA 3.0
2014-06-30T20:48:14.330Z
No worries. I'll explain as best I can.
Bryan Redeagle
<entries><assets><relations>
How do I pull related entries and assets?
# How do I pull related entries and assets?

(Asked by: username_0 on 2014-06-30)

I'm having a bit of a hard time wrapping my head around exactly how to work with both Relationships and Assets. Here is my current problem that I seem to be stumped on, i'm sure once I get this going I'll be good to go and fully understand how to work with both:

I need to determine if an entry is selected to be related to an entry single (the homepage) via the entry field 'featuredEvent', then
I need to determine if the 'image' block is being used in the matrix 'articleBody' in that related entry, then
I need to pull the first instance of the 'image' block (not necessarily the first block altogether), then
I need to determine if there is more than one 'asset/image' in the 'image' field in the 'image' block in the related entry, then
If there is only one image I need to output some code, and then output the image, as well as the other parameters 'size' & 'alignment', which are both drop downs, 'size' selects the pre-determined 'imageTransform' where as 'alignment' just applies a class or
If there are multiple images i need to output some code, and then output the images, as well as the other parameters 'size' & 'alignment'.

Could anybody write that code out for me and explain what is happening in each line to help me get a better grasp? Thank you so much in advance.

**Tags:** entries, assets, relations
> **Comments:**
> **username_1**: What you're asking for is very involved. Rather than have us simply do all the work for you, could you edit the question with what you have thus far? Even if it's not working, we can instead help you fix what's wrong and explain more along the way.  
> **username_1**: I also recommend making your question more descriptive of what you're asking for in the details.  
> **username_0**: I've done a lot of trial and error and nothing has stuck since i don't quite grasp the way the data works yet. Hence asking to 'explain what is happening in each line' so i can get a grasp and be able to figure out other similar issues on my own. Not really sure how to be more descriptive, I tried to break it down literally item by item of what needs to happen. I have a single (the homepage) that has an 'entry' field to relate an entry and show it on the homepage. I need to then look in that entry for a particular matrix block type, grab the first of that type, and then the assets deal.  
> **username_1**: No worries. I'll explain as best I can.  

---

## 1 answers

### Answer by username_1 on 2014-06-30. Score: 13 | Accepted answer

The data in Craft is handled mostly by Models (and arrays of Models). Before pulling actual information, you define your criteria in an ElementCriteriaModel using generic parameters and parameters specific to what you are trying to get (entries, assets, etc.). You then use one of the ElementCriteriaModel's functions to pull the actual data. You will get either a model or an array of models to loop over. This works the same for most things in Craft because most things are Elements.
You can look under the Variables heading in the side menu of the templating docs to see the kinds of models you will get.
On your homepage, you start with an automatically created entry model (this is created on every page that Craft recognizes as an entry's page). Here's the basic code you need:
{% for event in entry.featuredEvent %}
    {# Output some event code here #}
    {% set block = event.articleBody.type('image').first() %}
    {% for image in block.image.find() %}
        {# Output some image code here #}
        
    {% endfor %}
{% endfor %}

So an explanation of the above. In Craft, related elements are not automatically pulled from the DB. Instead each field that points to a related element (entries, assets, categories, users, etc.) is an ElementCriteriaModel so that you add more criteria before pulling. So entry.featuredEvent is an ElementCriteriaModel that points to all of your featured event entries.
When working with ElementCriteriaModel you have a few options to pull the data and use it. In the above code, when you give a for loop ElementCriteriaModel it will automatically pull the data and give the loop entries. So we take the entry.featuredEvent, and loop over the entries that's given to us when we use it in the for loop.
Inside the for loop, we pull the first image block in event.articleBody. Because matrix blocks are elements, we get the same features. So we set the type, and pull the first one.
In the next for loop, we are grabbing all of our images in the image block. The tricky part here is that we can make the code a little more efficient by simply pulling all of the images and looping over them. If you have very different html you plan on using for one image and multiple images, you will need to set the images as a variable like we did the block. You can then check the number of images using the length filter, and either Craft's switch tag, or a couple if statements.
Personally, I recommend using the loop, and using CSS to make the style differences you need.
Finally, we output each image. Because you're using a transform on them, we can't use each assets getImg function. Instead we write the img tag manually and grab the transformed url using getUrl.
The next bit is making a couple assumptions. I'm assuming that size is a image transform handle. I'm also assuming that these fields are on the asset. If they're on the matrix block, replace image.size and image.alignment with block.size and block.alignment.

> **Comments:**
> **username_1**: That means that the image field you mentioned isn't called image, or isn't an asset reference. It could also be a few other things, but I can't know without looking at it.  

---

