Answers.Body: <p>With Twig, you'll never need to nest tags within other tags. In fact, it's impossible to do.</p>
  
  <p>You can simply pass your variable into <code>getUrl</code> <strong>instead</strong> of using a string.</p>
  
  <pre><code>&lt;img src="{{ asset.getUrl(entry.newsImageSize) }}"&gt;
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-17T00:44:57.063Z
Answers.Text: 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.
Answers.UserDisplayName: Jack McKenzie
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-17T01:14:28.253Z
Answers.Text: 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.
Answers.UserDisplayName: Jack McKenzie
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-17T01:17:24.277Z
Answers.Text: If you've got [`devMode`](http://buildwithcraft.com/docs/config-settings#devMode) enabled, try using [`{{ dump() }}`](http://twig.sensiolabs.org/doc/functions/dump.html) 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.
Answers.UserDisplayName: Lindsey D
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-17T01:22:11.437Z
Answers.Text: 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.
Answers.UserDisplayName: Jack McKenzie
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-17T01:59:48.777Z
Answers.Text: Check out the [the dropdown fields docs...](http://buildwithcraft.com/docs/dropdown-fields) That may help you find a more elegant solution.
Answers.UserDisplayName: Lindsey D
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-11-15T20:49:29.827Z
Answers.Text: @LindseyD . is correct, you need to add ".value" to the end of your field reference. So: you need to do this first, to set a variable:
  `{% set transformType =  entry.newsImageSize.value %}` Then you can do this for the image: `<img src="{{ asset.getUrl(transformType) }}">`
Answers.UserDisplayName: Adrian
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-17T00:34:54.560Z
Answers.LastActivityDate: 2015-02-17T00:34:54.560Z
Answers.UserDisplayName: Lindsey D
Body: <p>This is the desired code</p>
  
  <pre><code>&lt;img src="{{ asset.getUrl('newsImageHalfWidth') }}"&gt;
  </code></pre>
  
  <p>Which works. However I want the transform name to be replaced by the contents of </p>
  
  <pre><code>entry.newsImageSize
  </code></pre>
  
  <p>So in a kind of pseudocode it would be</p>
  
  <pre><code>&lt;img src="{{ asset.getUrl('{{entry.newsImageSize}}') }}"&gt;
  </code></pre>
  
  <p>which of course does not work.</p>
  
Tags: <templating><variable>
Title: How do I reference a variable/expression inside an expression in twig templating?
ThreadText: # How do I reference a variable/expression inside an expression in twig templating?
  
  (Asked by: username_0 on 2015-02-17)
  
  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.
  
  **Tags:** templating, variable
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2015-02-17. Score: 2 | Accepted answer
  
  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) }}">
  
  ```
  
  > **Comments:**
  > **username_0**: 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.  
  > **username_0**: 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.  
  > **username_1**: If you've got [`devMode`](http://buildwithcraft.com/docs/config-settings#devMode) enabled, try using [`{{ dump() }}`](http://twig.sensiolabs.org/doc/functions/dump.html) 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.  
  > **username_0**: 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.  
  > **username_1**: Check out the [the dropdown fields docs...](http://buildwithcraft.com/docs/dropdown-fields) That may help you find a more elegant solution.  
  > **username_2**: @username_1 . is correct, you need to add ".value" to the end of your field reference. So: you need to do this first, to set a variable: `{% set transformType =  entry.newsImageSize.value %}` Then you can do this for the image: `<img src="{{ asset.getUrl(transformType) }}">`  
  
  ---
  
  