1

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) %}
    <span class="placeinphoto__dot" style="{{ variable.fieldname.topLeftStyles }}">{{ variable.title }}</span>
{% endfor %}
2

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 %}
        <span class="placeinphoto__dot" style="{{ variable[fieldHandle].topLeftStyles }}">{{ variable.title }}</span>
    {% endif %}
{% endfor %}
10
  • 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 Jan 18 '17 at 19:56
  • 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. – Mats Mikkel Rummelhoff Jan 18 '17 at 20:09
  • 1
    The development names are already setup as a category field. Could I use that instead of a new field shortAddress? – mention79 Jan 18 '17 at 20:27
  • 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. – Mats Mikkel Rummelhoff Jan 18 '17 at 20:36
  • 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 %} – Mats Mikkel Rummelhoff Jan 18 '17 at 20:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.