2

This is the desired code

<img src="{{ asset.getUrl('newsImageHalfWidth') }}">

Which works. However I want the transform name to be replaced by the contents of

entry.newsImageSize

So in a kind of pseudocode it would be

<img src="{{ asset.getUrl('{{entry.newsImageSize}}') }}">

which of course does not work.

2

With Twig, you'll never need to nest tags within other tags. In fact, it's impossible to do.

You can simply pass your variable into getUrl instead of using a string.

<img src="{{ asset.getUrl(entry.newsImageSize) }}">
| improve this answer | |
  • Thanks. I've tried that and I get a 'Division by zero' error. But when I just use {{ entry.newsImageSize }} I get the transform name displaying on the page. So can't work out what I am doing wrong. – Jack McKenzie Feb 17 '15 at 0:44
  • I'm getting closer. I think the reason the code is not working for me is because entry.newsImageSize is from a dropdown field, and is therefore not a string. I'm now looking for a way to convert the variable to a string. – Jack McKenzie Feb 17 '15 at 1:14
  • If you've got devMode enabled, try using {{ dump() }} to figure out exactly what that value holds. You may also want to setup a conditional, to ensure that entry.newsImageSize is not empty before trying to use it. – Lindsey D Feb 17 '15 at 1:17
  • Thanks for your help Lindsay D. The variable was not empty, but the wrong type, so when I converted to a string using <img src="{{ asset.getUrl( "" ~ entry.newsImageSize) }}"> it all worked OK. There might be a more elegant way to do the conversion. – Jack McKenzie Feb 17 '15 at 1:22
  • Check out the the dropdown fields docs... That may help you find a more elegant solution. – Lindsey D Feb 17 '15 at 1:59

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.