<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>

CC BY-SA 3.0
2015-02-17T00:44:57.063Z
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
CC BY-SA 3.0
2015-02-17T01:14:28.253Z
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
CC BY-SA 3.0
2015-02-17T01:17:24.277Z
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.
Lindsey D
CC BY-SA 3.0
2015-02-17T01:22:11.437Z
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
CC BY-SA 3.0
2015-02-17T01:59:48.777Z
Check out the [the dropdown fields docs...](http://buildwithcraft.com/docs/dropdown-fields) That may help you find a more elegant solution.
Lindsey D
CC BY-SA 4.0
2018-11-15T20:49:29.827Z
@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) }}">`
Adrian
CC BY-SA 3.0
2015-02-17T00:34:54.560Z
2015-02-17T00:34:54.560Z
Lindsey D
<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>

<templating><variable>
How do I reference a variable/expression inside an expression in twig templating?
# 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


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


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.

> **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**: @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) }}">`  

---

