<p>Not sure I totally get your problem, but you can use bracket style notation to use variables for field handles, i.e.</p>

<pre><code>{{ variable[someVariableHandle].topLeftStyles }}
</code></pre>

<p>or even something like</p>

<pre><code>{{ variable[someVariableHandle][someVariableProperty] }}
</code></pre>

<p>As for actually outputting the fields in a sane manner, you'll need to map the variable field handles to your entries in some way – one way to do it is to simply use a naming convention for the field handles where you use some unique property on the entries (e.g. the entry ID) as part of the handle:</p>

<pre><code>...
{% set fieldHandle = 'fieldname-'~entry.id' %}
{% if variable[fieldHandle] is defined %}
    {{ variable[fieldHandle].topLeftStyles }}
{% endif %}
...
</code></pre>

<p>Another way is to manually set up a map in your template – here's an example using entry IDs for keys, and the field handles for values:</p>

<pre><code>{% set fieldHandles = {
    35: 'someFieldHandle',
    464: 'anotherFieldHandle'
} %}
...
{% for variable in sitemap.relatedTo(category) %}
    {% set fieldHandle = fieldHandles[variable.id] ?? null %}
    {% if fieldHandle and variable[fieldHandle] is defined %}
        &lt;span class="placeinphoto__dot" style="{{ variable[fieldHandle].topLeftStyles }}"&gt;{{ variable.title }}&lt;/span&gt;
    {% endif %}
{% endfor %}
</code></pre>

CC BY-SA 3.0
2017-01-18T19:56:43.270Z
The manual way is working, but the list will get very long as each development could have 50+ houses, so is there a way to shorten this part of the code: `{% set fieldHandles = {
        452: 'knowleHillParkSiteplan',
      453: 'knowleHillParkSiteplan',
      454: 'knowleHillParkSiteplan',
      447: 'birchleySiteplan',
        448: 'birchleySiteplan',
      449: 'birchleySiteplan',
      450: 'birchleySiteplan'
    } %}`
mention79
CC BY-SA 3.0
2017-01-18T20:09:42.420Z
Well the common denominator for the field handles seems to be some sort of address – if you can pull the address part (e.g. `"knowleHillPark"`) from a different field on the entries, you could use that as the "identifier". Say you add a field called `shortAddress` or something, in which you input the address (e.g. `"Knowle Hill Park"`), then you could do something like this in the template: `{% set fieldHandle = variable.shortAddress|kebab ~ 'Siteplan' %}`. Then you can skip the "manual" mapping.
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2017-01-18T20:15:34.423Z
No offence, but I'm also thinking that if you need to add 50 fields to your field layout to get this working, there's got to be a better way. What if you created a Matrix field – either in a related single or a global set, perhaps – with a block type containing an Assets field and an Entries field, for relating the two together? Sure, you'll need to add the same Asset 50 times, but something along those lines would probably still be better than having those 50 fields clutter up the field layout for all your entries (sorry if I misinterpret your original issue, it's still not 100% clear to me).
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2017-01-18T20:22:36.017Z
The way the donkeytail plugin works is that you need to create a single field in which the image is set, and each individual dot is then added with a single entry per dot, and I've set this up in a matrix already to make it easy in the cms. So to help explain there could be up to 15 property developments. And so that would need 15 fields. And then each property development might have up to 50 houses each. But most are under 10. So I have to create a new entry per dot.
mention79
CC BY-SA 3.0
2017-01-18T20:27:25.423Z
The development names are already setup as a category field. Could I use that instead of a new field `shortAddress`?
mention79
CC BY-SA 3.0
2017-01-18T20:36:24.347Z
Of course, as long as the category titles and field handle addresses match. Something like `{% set fieldHandle = variable.categoryField.first().title|trim|kebab ~ 'Siteplan' %}` should do it; that'd turn a category titled `"Knowle Hill Park"` into a field handle `knowleHillParkSiteplan`.
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2017-01-18T20:45:29.277Z
It's not showing. Can you see anything wrong with this. `{% set fieldHandle = variable.developments.first().title|trim|kebab ~ 'Siteplan' %}
    {% set sitemap = craft.entries.section('sitePlan') %}
    {% for variable in sitemap.relatedTo(category) %}
        {% set fieldHandle = fieldHandles[variable.id] ?? null %}
        {% if fieldHandle and variable[fieldHandle] is defined %}
            <span class="placeinphoto__dot" style="{{ variable[fieldHandle].topLeftStyles }}">{{ variable.title }}</span>
        {% endif %}
    {% endfor %}` Only the first line is different
mention79
CC BY-SA 3.0
2017-01-18T20:50:58.687Z
Not sure, but you're looking for something like this:
`{% for variable in sitemap.relatedTo(category) %}
    {% set fieldHandle = variable.developments.first().title|trim|kebab ~ 'Siteplan' %}
    {% if variable[fieldHandle] is defined %}
        <span class="placeinphoto__dot" style="{{ variable[fieldHandle].topLeftStyles }}">{{ variable.title }}</span>
    {% endif %}
{% endfor %}`
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2017-01-18T21:00:50.330Z
It's working on one, but not the other yet, but think that's just to do with the field naming. But it is working at least, so thankyou for your help. I wouldn't have been able to do that on my own yet, as still have a lot to learn about twig and craft
mention79
CC BY-SA 3.0
2017-01-18T21:02:04.393Z
Yeah, if the field names and category titles don't match, you'll need to change one or the other so they do :) Happy you got it sorted, cheers.
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2017-01-18T19:34:38.550Z
2017-01-18T19:34:38.550Z
Mats Mikkel Rummelhoff
<p>I'm building a site for a property developer that has multiple housing developments. And on each development page there is a siteplan image that shows a CGI of the all the houses on the development.</p>

<p>I'm trying to implement the Donkeytail plugin <a href="https://straightupcraft.com/craft-plugins/donkeytail-lite" rel="nofollow noreferrer">https://straightupcraft.com/craft-plugins/donkeytail-lite</a> which lets you add pinpoints on images and have made some progress and have it working. However, every development page needs it's own siteplan, and so I have to create a field for each one.</p>

<p>So my question is how do I reference it in the template dynamically? As currently it is being called via <code>variable.fieldname.topLeftStyles</code></p>

<p>But this only references the exact fieldname of one siteplan. So I need a dynamic way so that it can be different on each development page. </p>

<pre><code>{% set sitemap = craft.entries.section('sitePlan') %}
{% for variable in sitemap.relatedTo(category) %}
    &lt;span class="placeinphoto__dot" style="{{ variable.fieldname.topLeftStyles }}"&gt;{{ variable.title }}&lt;/span&gt;
{% endfor %}
</code></pre>

<templating><field>
Reference a field name relatedTo(category)
# Reference a field name relatedTo(category)

(Asked by: username_0 on 2017-01-18)

I'm building a site for a property developer that has multiple housing developments. And on each development page there is a siteplan image that shows a CGI of the all the houses on the development.
I'm trying to implement the Donkeytail plugin https://straightupcraft.com/craft-plugins/donkeytail-lite which lets you add pinpoints on images and have made some progress and have it working. However, every development page needs it's own siteplan, and so I have to create a field for each one.
So my question is how do I reference it in the template dynamically? As currently it is being called via variable.fieldname.topLeftStyles
But this only references the exact fieldname of one siteplan. So I need a dynamic way so that it can be different on each development page. 
{% set sitemap = craft.entries.section('sitePlan') %}
{% for variable in sitemap.relatedTo(category) %}
    {{ variable.title }}
{% endfor %}

**Tags:** templating, field


---

## 1 answers

### Answer by username_1 on 2017-01-18. Score: 2 | Accepted answer

Not sure I totally get your problem, but you can use bracket style notation to use variables for field handles, i.e.
{{ variable[someVariableHandle].topLeftStyles }}

or even something like
{{ variable[someVariableHandle][someVariableProperty] }}

As for actually outputting the fields in a sane manner, you'll need to map the variable field handles to your entries in some way – one way to do it is to simply use a naming convention for the field handles where you use some unique property on the entries (e.g. the entry ID) as part of the handle:
...
{% set fieldHandle = 'fieldname-'~entry.id' %}
{% if variable[fieldHandle] is defined %}
    {{ variable[fieldHandle].topLeftStyles }}
{% endif %}
...

Another way is to manually set up a map in your template – here's an example using entry IDs for keys, and the field handles for values:
{% set fieldHandles = {
    35: 'someFieldHandle',
    464: 'anotherFieldHandle'
} %}
...
{% for variable in sitemap.relatedTo(category) %}
    {% set fieldHandle = fieldHandles[variable.id] ?? null %}
    {% if fieldHandle and variable[fieldHandle] is defined %}
        {{ variable.title }}
    {% endif %}
{% endfor %}

> **Comments:**
> **username_0**: The manual way is working, but the list will get very long as each development could have 50+ houses, so is there a way to shorten this part of the code: `{% set fieldHandles = {         452: 'knowleHillParkSiteplan',       453: 'knowleHillParkSiteplan',       454: 'knowleHillParkSiteplan',       447: 'birchleySiteplan',         448: 'birchleySiteplan',       449: 'birchleySiteplan',       450: 'birchleySiteplan'     } %}`  
> **username_1**: Well the common denominator for the field handles seems to be some sort of address – if you can pull the address part (e.g. `"knowleHillPark"`) from a different field on the entries, you could use that as the "identifier". Say you add a field called `shortAddress` or something, in which you input the address (e.g. `"Knowle Hill Park"`), then you could do something like this in the template: `{% set fieldHandle = variable.shortAddress|kebab ~ 'Siteplan' %}`. Then you can skip the "manual" mapping.  
> **username_1**: No offence, but I'm also thinking that if you need to add 50 fields to your field layout to get this working, there's got to be a better way. What if you created a Matrix field – either in a related single or a global set, perhaps – with a block type containing an Assets field and an Entries field, for relating the two together? Sure, you'll need to add the same Asset 50 times, but something along those lines would probably still be better than having those 50 fields clutter up the field layout for all your entries (sorry if I misinterpret your original issue, it's still not 100% clear to me).  
> **username_0**: The way the donkeytail plugin works is that you need to create a single field in which the image is set, and each individual dot is then added with a single entry per dot, and I've set this up in a matrix already to make it easy in the cms. So to help explain there could be up to 15 property developments. And so that would need 15 fields. And then each property development might have up to 50 houses each. But most are under 10. So I have to create a new entry per dot.  
> **username_0**: The development names are already setup as a category field. Could I use that instead of a new field `shortAddress`?  
> **username_1**: Of course, as long as the category titles and field handle addresses match. Something like `{% set fieldHandle = variable.categoryField.first().title|trim|kebab ~ 'Siteplan' %}` should do it; that'd turn a category titled `"Knowle Hill Park"` into a field handle `knowleHillParkSiteplan`.  
> **username_0**: It's not showing. Can you see anything wrong with this. `{% set fieldHandle = variable.developments.first().title|trim|kebab ~ 'Siteplan' %}     {% set sitemap = craft.entries.section('sitePlan') %}     {% for variable in sitemap.relatedTo(category) %}         {% set fieldHandle = fieldHandles[variable.id] ?? null %}         {% if fieldHandle and variable[fieldHandle] is defined %}             <span class="placeinphoto__dot" style="{{ variable[fieldHandle].topLeftStyles }}">{{ variable.title }}</span>         {% endif %}     {% endfor %}` Only the first line is different  
> **username_1**: Not sure, but you're looking for something like this: `{% for variable in sitemap.relatedTo(category) %}     {% set fieldHandle = variable.developments.first().title|trim|kebab ~ 'Siteplan' %}     {% if variable[fieldHandle] is defined %}         <span class="placeinphoto__dot" style="{{ variable[fieldHandle].topLeftStyles }}">{{ variable.title }}</span>     {% endif %} {% endfor %}`  
> **username_0**: It's working on one, but not the other yet, but think that's just to do with the field naming. But it is working at least, so thankyou for your help. I wouldn't have been able to do that on my own yet, as still have a lot to learn about twig and craft  
> **username_1**: Yeah, if the field names and category titles don't match, you'll need to change one or the other so they do :) Happy you got it sorted, cheers.  

---

