Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meaning of Dollar Sign & Curly Braces Containing Javascript Block Outside of HTML Script Tag

I'm currently reading 'Javascript Web Applications' (O'Reilly, Alex MacCaw) and very early on there's a code snippet which might appear to execute a JS function, within an HTML document, yet it is not enclosed by <script> tags:

// template.html
<div>
  <script>
    function doSomething(aParam) {
      /* do stuff ... */
    };
  </script>
  ${ doSomething(this.someX) }
</div>

Please could someone kindly explain the dollar-sign-curly-brace notation? I'm currently learning JS and I haven't seen this before, so I'm presuming it's either JS shorthand for code execution (if so, why no terminating semi-colon?), or perhaps some proprietary templating mark-up (Ruby? PHP?), or something else entirely?

Many thanks.

UPDATE

It transpires that later on in Chapter 5 (of the aforementioned book) we are then introduced to Javascript templating. It appears to be an assumption by the author that the reader has already encountered this templating technique before reading the book. As noted by Stackoverflow member Esailija, this book is not a beginners' guide to Javascript; I should add that I'm reading this book in parallel with 'Javascript: The Good Parts' (O'Reilly, Douglas Crockford) amongst others.

I had half-suspected some kind of templating, but I hadn't considered pure JS templating. I have used PHP and RoR frameworks in the past which also used similar templating concepts for injecting model data into views.

One final point: my reason(s) for reading 'Javascript Web Applications' is that it discusses the Model-View-Controller (MVC) pattern within the JS sphere. While it advocates the use of the jQuery library (among others) to both enhance and accelerate development, it is not a jQuery API book or yet-another-new-javascript-wrapper; rather, the book makes use of such libraries (where appropriate) to deal with cross-browser JS inconsistencies, while driving home actual JS best practices and patterns in creating 'web applications'.

like image 567
41st Avatar asked Nov 04 '22 08:11

41st


1 Answers

It's not supposed to be javascript but a template file. Template file contains static html and very dumb presentation logic, usually in a pseudolanguage as it is in this case. It should not contain any real javascript - as it is said in the book that it's something you shouldn't do.

The book then goes into a refactored version that only has static html and the pseudolanguage statement. See Javascript templating.

The book seems to be aimed at those who are already proficient at javascript and are looking into structuring their js better.

like image 136
Esailija Avatar answered Nov 09 '22 14:11

Esailija