1

Hopefully simple variable / concatenation question: What is the best way to retrieve the url of a specific asset the name of which should be concatenated from an asset field variable and a text string?

I need to end accessing the url of an asset like so – {{asset.portraitThumbSmall.url}}, but I am in a loop where “portrait” could also be “landscape” ...or “square”, and is derived from an asset field variable on an asset called “thumbnailCrop”. With me so far?

So something like...

    {{asset.thumbnailCrop ~ "ThumbSmall" ~ .url}}

…only, unlike this, it should actually work.

I did read this -> How can I incorporate a variable into a string? …but I’m trying to incorporate a string into a variable. Not sure it applies.

Someone in the Slack channel suggested...

    {% set thumbnail = attribute(asset, thumbnailCrop ~ "ThumbSmall") %}
    {{ thumbnail.url }}

But it doesn't work, and I was also wondering if it wasn't possible to make something more compact.

Any help would be appreciated!

3

Give this a shot...

{% set transform = (asset.thumbnailCrop.value ?: "portrait") ~ "ThumbSmall" %}
{{ asset.url(transform) }}

If asset.thumbnailCrop is a dropdown field (or similar), you'll need to specifically extract the value from it. If no value is found, it'll fall back to using the "portrait" string.

  • YOU DID IT :-) Thank you. Earlier suggestions were very similar, just missed ".value". Working great now. – kerns Dec 17 '15 at 13:41

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.