Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8157701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:22:26+00:00 2026-06-06T17:22:26+00:00

How to set up variables inside an underscore.js template for an app built with

  • 0

How to set up variables inside an underscore.js template for an app built with backbone.js? I just want to create reusable processed strings. Also, can underscore.js‘s built-in functions like _.escape be used to process those variables?

<script type="text/html" id="templateresults">

<p><%= encodeURIComponent(title) %></p> // this works

// try 1:
var encodedTitle = encodeURIComponent(title); // shows up as regular text
<p>'+encodedTitle+'</p> // this doesn't work and shows up as regular text

// try 2:
<% var encodedTitle = encodeURIComponent(title); %> // nothing shows up
<p><% '+encodedTitle+' %></p> // nothing shows up

</script>

title is a JSON item (text string).

Encoded output: This%20is%20a%20Sample%20Title
Regular output: This is a Sample Title

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T17:22:28+00:00Added an answer on June 6, 2026 at 5:22 pm

    Your try 2 is almost right but the tag where you output encodedTitle is missing the = at the start and doesn’t need the + in the string. Should be:

    <p><%= encodedTitle %></p>
    

    Alternatively you could also do:

    <p><% print(encodedTitle) %></p>
    

    In underscore templates, any javascript you want evaluated must be contained inside <% %>, hence why your second attempt just outputs the the javascript as a string. You correctly used the = in your sample at the top but omitted it in try 2.

    The = tells the templating engine to output the result of the enclosed javascript as a string. If you don’t use the =, the javascript is executed, but nothing is output. Underscore’s templates provide the print() function as an alternative to using the =, I dont know that one way is better than the other.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to be able to set variables in a template to string values.
I want to create variables inside a function from a dictionary. Let's say I
I just want to SELECT values into variables from inside a procedure. SELECT blah1,blah2
Can I set variables inside if-construct? In general, where is it allowed to set
When is the right time to use functions to set variables inside of my
I have a basic app set up using Backbone.js and Tornado Web. When I
I'm trying to store the names of some variables inside strings. For example: Dim
Is it possible to get/set instance variables of an object from inside a module
I have a batch file which initializes variables via SET inside a for loop,
I'm attempting to set up a script to concatenate some variables inside a string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.