<p>No disrespect, but you may be misunderstanding the relationship between Twig and HTML.</p>

<p>The Twig code is completely oblivious to your HTML DOM structure. Its job is to parse any Twig tags into PHP (which are then ultimately rendered in HTML).</p>

<p>Based on your example, try it like this:</p>

<pre><code>{% set myDiv = "Some text" %}
&lt;div id="myDiv"&gt;{{ myDiv }}&lt;/div&gt;
</code></pre>

<p>To expand further on the relationship between Twig/PHP/HTML, think of it like this...</p>

<p>(1) Twig is parsed on the <strong>server</strong>. It generates a PHP file which represents your entire Twig file. That PHP file is stored until a change is made to your Twig template. Assuming nothing has changed, Twig will then call that PHP file directly (bypassing the original Twig file).</p>

<p>(2) PHP will then do everything it needs to do, again on the <strong>server</strong>. It will finish rendering whatever HTML it needs to, before sending back that fully-parsed HTML to the <strong>client</strong>.</p>

<p>(3) Once you get the the <strong>client</strong> side, it's all HTML (and CSS and JS). There is no more PHP or Twig.</p>

CC BY-SA 3.0
2014-06-13T21:21:10.483Z
Ok, understand. Anyway, it solves my problem! Thanks!
nicael
CC BY-SA 3.0
2014-06-13T21:24:46.700Z
@nicael You can also set that variable in a layout template like this {% if myDiv is defined %}{{ myDiv }}{% endif %} and then extend the layout and define that variable per entry i.e. {% set myDiv = entry.variable %}. Check out the layouts built into a default Craft install to see examples of this in action.
Alan Miller
CC BY-SA 3.0
2014-06-13T21:26:05.317Z
@nicael Glad that helped! I expanded my answer, in case anyone in the future needs further explanation. :)
Lindsey D
CC BY-SA 3.0
2014-06-13T22:03:19.560Z
@LindseyD When I grow up, I'd like to answer questions like you did here.
Matt Stein
CC BY-SA 3.0
2014-06-13T22:04:26.353Z
Lol, thanks @MattStein... This is where my OCD comes in handy. ;)
Lindsey D
CC BY-SA 3.0
2014-06-13T21:16:23.543Z
2014-06-13T22:02:01.123Z
2014-06-13T22:02:01.123Z
Lindsey D
<p>Suppose I have </p>

<pre><code>&lt;div id="myDiv"&gt;&lt;/div&gt;
</code></pre>

<p>I want to set its content to "Some text".</p>

<p>Is it possible?</p>

<p>Something like </p>

<pre><code>{% set id.myDiv = "Some text" %}
</code></pre>

<p><strong>Edit</strong></p>

<p>Well, despite the fact it is not, Lindsey D solved my problem in the BEST way.</p>

CC BY-SA 3.0
2014-06-13T21:03:24.163Z
Mind providing a little more context? This doesn't seem to be a Craft-specific question.
Lindsey D
CC BY-SA 3.0
2014-06-13T21:09:47.680Z
@LindseyD I edited. Actually I thought about placing it in some if.
nicael
CC BY-SA 3.0
2014-06-14T18:41:30.983Z
Despite the fact that the answer is "no, this isn't possible", this **is** in fact a legitimate question. Many people who begin working with Twig don't fully comprehend the relationship between Twig, PHP, and HTML. I've modified the title of this question to provide more clarity in what was being asked... Perhaps the downvoters would be so kind as to remove those down votes.
Lindsey D
CC BY-SA 3.0
2014-06-14T18:43:10.857Z
Many thanks, @LindseyD! Maybe there are some another reasons of downvotes, but it is live example of bad question ))
nicael
CC BY-SA 3.0
2014-06-16T15:34:03.557Z
Agreed @LindseyD. I didn't know *how to answer* the question well, but it's perfectly legitimate. It's not like nicael asked how to fix a wobbly bicycle seat...
Matt Stein
<templating>
Using Twig, is it possible to dynamically insert text into a div by referencing the div's ID?
# Using Twig, is it possible to dynamically insert text into a div by referencing the div's ID?

(Asked by: username_0 on 2014-06-13)

Suppose I have 


I want to set its content to "Some text".
Is it possible?
Something like 
{% set id.myDiv = "Some text" %}

Edit
Well, despite the fact it is not, Lindsey D solved my problem in the BEST way.

**Tags:** templating
> **Comments:**
> **username_1**: Mind providing a little more context? This doesn't seem to be a Craft-specific question.  
> **username_0**: @LindseyD I edited. Actually I thought about placing it in some if.  
> **username_1**: Despite the fact that the answer is "no, this isn't possible", this **is** in fact a legitimate question. Many people who begin working with Twig don't fully comprehend the relationship between Twig, PHP, and HTML. I've modified the title of this question to provide more clarity in what was being asked... Perhaps the downvoters would be so kind as to remove those down votes.  
> **username_0**: Many thanks, @LindseyD! Maybe there are some another reasons of downvotes, but it is live example of bad question ))  
> **username_2**: Agreed @LindseyD. I didn't know *how to answer* the question well, but it's perfectly legitimate. It's not like nicael asked how to fix a wobbly bicycle seat...  

---

## 1 answers

### Answer by username_1 on 2014-06-13. Score: 15 | Accepted answer

No disrespect, but you may be misunderstanding the relationship between Twig and HTML.
The Twig code is completely oblivious to your HTML DOM structure. Its job is to parse any Twig tags into PHP (which are then ultimately rendered in HTML).
Based on your example, try it like this:
{% set myDiv = "Some text" %}
{{ myDiv }}

To expand further on the relationship between Twig/PHP/HTML, think of it like this...
(1) Twig is parsed on the server. It generates a PHP file which represents your entire Twig file. That PHP file is stored until a change is made to your Twig template. Assuming nothing has changed, Twig will then call that PHP file directly (bypassing the original Twig file).
(2) PHP will then do everything it needs to do, again on the server. It will finish rendering whatever HTML it needs to, before sending back that fully-parsed HTML to the client.
(3) Once you get the the client side, it's all HTML (and CSS and JS). There is no more PHP or Twig.

> **Comments:**
> **username_0**: Ok, understand. Anyway, it solves my problem! Thanks!  
> **username_3**: @nicael You can also set that variable in a layout template like this {% if myDiv is defined %}{{ myDiv }}{% endif %} and then extend the layout and define that variable per entry i.e. {% set myDiv = entry.variable %}. Check out the layouts built into a default Craft install to see examples of this in action.  
> **username_1**: @nicael Glad that helped! I expanded my answer, in case anyone in the future needs further explanation. :)  
> **username_2**: @LindseyD When I grow up, I'd like to answer questions like you did here.  
> **username_1**: Lol, thanks @MattStein... This is where my OCD comes in handy. ;)  

---

