0

I am using underscore in my external templates(using require.js) and trying to access the data inside document.ready function. But I get the above mentioned error while executing the template.
Below is the code snippet

<script>
    $(document).ready(function(){
         <%_.each(people, function(person){ %>
             console.log(<%=person%>);
      <% }); %>
    });
</script>

Where am I going wrong? All help will be much appreciated. Thanks

5
  • I'm not sure what you're trying to accomplish with this code but you can't mix your template into plain JavaScript like that, your template should be in a separate text file or <script type="text/x-underscore"> element. Commented Apr 26, 2013 at 6:07
  • I am trying to extract individual person's age and use it to draw a chart. I believe using "_.each" inside ready function is giving me the problem. Please correct me if I am wrong. Commented Apr 26, 2013 at 8:53
  • But the <% ... %> stuff isn't JavaScript so you can't put them inside a $(document).ready() callback function. Commented Apr 26, 2013 at 20:36
  • You could put a plain _(people).each(function(p) { console.log(p) }) in the callback though. Commented Apr 26, 2013 at 21:04
  • Thanks a lot for the suggestion. I tried with this, _(<%= people %>.each(function(person) {...} . But this doesn't work.How do I get the value that is sent by the view to the template? Commented Apr 29, 2013 at 6:13

1 Answer 1

2

You seem to be a bit confused about how templates work. You need two separate things:

  1. The template. This often goes inside a <script type="text/x-underscore"> container. We use <script> with a non-HTML and non-JavaScript type to keep the browser from trying to interpret the template data in any way, we want the browser to leave the template alone so that we can have our way with it via _.template.
  2. The JavaScript that uses the template. This is where you call _.template to create a function representation of the template and then call that function to get your HTML.

So, if you wanted to produce a list of people, you'd have a template like this:

<script id="t" type="text/x-underscore">
    <% _(people).each(function(person) { %>
        <%= person %>
    <% }); %>
</script>

and then you'd have some JavaScript like this to use it:

<script type="text/javascript">
    $(document).ready(function() {
        var people = [ 'Tom', 'Dick', 'Harry' ];
        var tmpl   = _.template($('#t').html());
        $('body').append(tmpl({
            people: people
        }));
    });
</script>

Note that there is no <% ... %> stuff in the <script type="text/javascript">, that stuff goes in the template.

You can call whatever JavaScript you want inside the template so you could also do this:

<script id="t" type="text/x-underscore">
    <% _(people).each(function(person) { %>
        <% console.log(person) %>
    <% }); %>
</script>

Of course, if that's all you wanted to do then you wouldn't use a template at all, you'd just write some JavaScript:

<script type="text/javascript">
    $(document).ready(function() {
        var people = [ 'Tom', 'Dick', 'Harry' ];
        _(people).each(function(person) { // Or any of the other ways of looping over people...
            console.log(person);
        });
    });
</script>

Here's a quick demo of the above: http://jsfiddle.net/ambiguous/V7DXv/

Sign up to request clarification or add additional context in comments.

4 Comments

Hi Sir, Thanks a lot for your time. I completely understand how templates work. Probably the way I have put forth my question is confusing. Let me quickly tell you what I am intending to do.. I have person's age extracted and pushed into an array, which has to be sent to the chart library as an input. Find the code snippet here: link I want the array data to be accessed in ready function.
Why are you using a template at all? Why not simply build age in JavaScript as usual and hand that to the chart?
I'm using backbone & reqiure.js in my project. I'm making the call for data in my view just because it can be used by other templates too. That's the reason i'm not building "age" in JavaScript.
You can't get anything out of the template other than text. Unless you want the template to do something horrifically evil like assigning to a property in window. I guess I don't understand the question or what templates have to do with it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.