Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-13T21:21:10.483Z
Answers.Text: Ok, understand. Anyway, it solves my problem! Thanks!
Answers.UserDisplayName: nicael
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-13T21:24:46.700Z
Answers.Text: @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.
Answers.UserDisplayName: Alan Miller
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-13T21:26:05.317Z
Answers.Text: @nicael Glad that helped! I expanded my answer, in case anyone in the future needs further explanation. :)
Answers.UserDisplayName: Lindsey D
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-13T22:03:19.560Z
Answers.Text: @LindseyD When I grow up, I'd like to answer questions like you did here.
Answers.UserDisplayName: Matt Stein
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-13T22:04:26.353Z
Answers.Text: Lol, thanks @MattStein... This is where my OCD comes in handy. ;)
Answers.UserDisplayName: Lindsey D
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-13T21:16:23.543Z
Answers.LastActivityDate: 2014-06-13T22:02:01.123Z
Answers.LastEditDate: 2014-06-13T22:02:01.123Z
Answers.UserDisplayName: Lindsey D
Body: <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>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-06-13T21:03:24.163Z
Comments.Text: Mind providing a little more context? This doesn't seem to be a Craft-specific question.
Comments.UserDisplayName: Lindsey D
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-06-13T21:09:47.680Z
Comments.Text: @LindseyD I edited. Actually I thought about placing it in some if.
Comments.UserDisplayName: nicael
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-06-14T18:41:30.983Z
Comments.Text: 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.
Comments.UserDisplayName: Lindsey D
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-06-14T18:43:10.857Z
Comments.Text: Many thanks, @LindseyD! Maybe there are some another reasons of downvotes, but it is live example of bad question ))
Comments.UserDisplayName: nicael
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-06-16T15:34:03.557Z
Comments.Text: 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...
Comments.UserDisplayName: Matt Stein
Tags: <templating>
Title: Using Twig, is it possible to dynamically insert text into a div by referencing the div's ID?
ThreadText: # 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
  
  ```
  <div id="myDiv"></div>
  
  ```
  
  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, username_1 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**: @username_1 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, @username_1! Maybe there are some another reasons of downvotes, but it is live example of bad question ))  
  > **username_2**: Agreed @username_1. I didn't know *how to answer* the question well, but it's perfectly legitimate. It's not like username_0 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" %}
  <div id="myDiv">{{ myDiv }}</div>
  
  ```
  
  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**: @username_0 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**: @username_0 Glad that helped! I expanded my answer, in case anyone in the future needs further explanation. :)  
  > **username_2**: @username_1 When I grow up, I'd like to answer questions like you did here.  
  > **username_1**: Lol, thanks @username_2... This is where my OCD comes in handy. ;)  
  
  ---
  
  