-3

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, Lindsey D solved my problem in the BEST way.

  • Mind providing a little more context? This doesn't seem to be a Craft-specific question. – Lindsey D Jun 13 '14 at 21:03
  • @LindseyD I edited. Actually I thought about placing it in some if. – nicael Jun 13 '14 at 21:09
  • 2
    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 Jun 14 '14 at 18:41
  • Many thanks, @LindseyD! Maybe there are some another reasons of downvotes, but it is live example of bad question )) – nicael Jun 14 '14 at 18:43
  • 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... – Matt Stein Jun 16 '14 at 15:34
15

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.

  • 1
    Ok, understand. Anyway, it solves my problem! Thanks! – nicael Jun 13 '14 at 21:21
  • @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 Jun 13 '14 at 21:24
  • @nicael Glad that helped! I expanded my answer, in case anyone in the future needs further explanation. :) – Lindsey D Jun 13 '14 at 21:26
  • 2
    @LindseyD When I grow up, I'd like to answer questions like you did here. – Matt Stein Jun 13 '14 at 22:03
  • Lol, thanks @MattStein... This is where my OCD comes in handy. ;) – Lindsey D Jun 13 '14 at 22:04

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.