Templating with Underscores: Revolutionizing token markers

When using out of the box underscore templating, the default markers for raw content are <%= %>, and for HTML escaped content are <%- %>.

However, it is possible to change these markers by adjusting the template settings, for example:

_.templateSettings.interpolate = /\{\{(.+?)\}\}/g;

The question arises, how does this configuration affect the distinction between raw and escaped content? It seems that by changing the markers, you might end up with only one type. Have I missed something in this process?

Answer №1

The documentation for Underscore.js states (with emphasis added):

If you're not a fan of ERB-style delimiters, you have the option to modify Underscore's template settings to use different symbols for code interpolation. Simply define an interpolate regex to identify expressions that should be interpolated literally, an escape regex for expressions that need to be inserted after HTML escaping, and an evaluate regex for expressions that should be evaluated without being inserted into the output.

By specifying the escape property in the _.templateSettings object:

_.templateSettings.escape = /\{\{-(.*?)\}\}/g
>>> compiled = _.template("Escaped: {{- value }}\nNot escaped: {{ value }}")
>>> compiled({value: 'Hello, <b>world!</b>'})
"Escaped: Hello, &lt;b&gt;world!&lt;&#x2F;b&gt;
Not escaped: Hello, <b>world!</b>"

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Instead of showing the data in the variable "ionic", there is a display of "[object object]"

Here is the code snippet I'm working with: this.facebook.login(['email', 'public_profile']).then((response: FacebookLoginResponse) => { this.facebook.api('me?fields=id,name,email,first_name,picture.width(720).height( ...

Tips for implementing xpath in module.exports with mocha javascript

Currently, I am utilizing mocha in conjunction with Node.js and have encountered a challenge. In my scenario, I need to use the XPath defined in one test file (verification.js) and apply it in another file (test.js). The issue arises from the fact that the ...

Using Angular's ngBlur directive on multiple input fields

My current task involves capturing and saving all content on a webpage after it has been edited. For example, when a user clicks into an input field, enters data, and then clicks elsewhere, I would like to trigger a function to collect all the model data a ...

Excessive CPU usage caused by a patch in jQuery dealing with regular expressions

Working on a project developed by an unknown individual has presented some challenges. Without any means of contact with this person, I noticed that the browser's CPU consumption spikes significantly upon loading the page. Upon further investigation, ...

Is there a way to have my code run a script only once right after a component has finished loading?

I am currently developing a web application using Vuejs and implementing the vue-router with single file components. One issue I am facing is that whenever a user navigates to a specific route, the created() function within the component gets triggered. T ...

The icon displays correctly in Firefox but is not visible in IE

link REL="SHORTCUT ICON" HREF="/images/greenTheme/favicon.ico" type="image/x-icon" While this code functions properly in Firefox, it appears to be causing issues in Internet Explorer. Can anyone provide guidance on how to resolve the compatibility issue w ...

Is it possible for a web server to transmit information without being prompted by the client?

I'm looking to create a web application for a tool that can run anywhere from a minute to a couple of hours. I want users to be able to initiate the tool's run directly from the webpage, and receive real-time status updates as the process progres ...

Issue encountered while retrieving information from XML file through AJAX

I am facing an issue while using AJAX to fetch data from an external XML file. The error I receive is "Invalid argument" and I am working with IE 8. Below is the code snippet: var xhr; xhr = new XMLHttpRequest(); xhr.open("GET","C:/Users/abc/Desktop/Pr ...

How to conditionally prevent event propagation to children in VueJs

This Vue component is called ColorButtonGroup, and it serves as a group of checkbox/toggle buttons. It has a maximum limit of 4 colors that can be selected. The component utilizes another component called ToggleButton, which is a simple toggle selection w ...

The CORS policy has blocked access to XMLHttpRequest at 'http://localhost:8080/' from the origin 'http://localhost:3000'

There seems to be an issue with the CORS policy blocking access to XMLHttpRequest when trying to send a post request from NuxtJS frontend to a Node server hosted on localhost:3000. Despite using CORS in the app, the error persists, preventing insertion of ...

The inputs for Node express middleware are unclear and lack definition

I am currently exploring Node.js as a potential replacement for my existing DOT NET API. I have created middleware to enforce basic non-role authorization in my application, but I am encountering compilation problems with the function inputs. Compilation ...

The sequencing of AJAX calls on the server side may not necessarily match the order in which they were initiated on the client side

I am working on an ASP.NET MVC application that includes a JavaScript function within a view. Here is the code snippet: function OnAmountChanged(s, fieldName, keyValue, url) { console.log("Get Number: " + s.GetNumber()); console.log(" "); ...

Is there a way for me to incorporate the input parameter value passed to a JavaScript function into the popup that is being opened by the function itself?

I am not very proficient in PHP and JavaScript, and I'm facing an issue while trying to pass a value from a main page to a popup page that is defined within this main page. To provide more context: In my account.php page, there is a link like this: ...

Parent Directory Injector: Prioritizing Injection of Parent Directories

Currently, I am utilizing 'grunt-injector' to inject a list of files using 'app/**/*.js'. However, I am facing dependency errors due to the alphabetical order in which the files are injected. To avoid these issues, I am looking for a so ...

Creating a Dynamic Dependent Dropdown with Jquery and Ajax in PHP

As a newbie in coding, I stumbled upon some valuable information to enhance my register form using Ajax. While the PHP files seem to be functioning correctly, I suspect that the JS file is not performing as expected. In the register form, you'll find ...

After refreshing the page, the data stored in the localStorage array gets replaced

After refreshing the webpage, my localStorage array values are being overwritten. During a session, I am able to update values on the front end, and the array in localStorage gets updated accordingly. However, if multiple inputs are changed during a sessio ...

How to eliminate a hyperlink from an HTML element with the help of JQuery

Recently, I was assigned to revamp a website for the company I work for. However, upon closer inspection, I realized that the website is quite messy and relies heavily on templates, resulting in certain elements being auto-generated as active links. The i ...

Is there a way to customize the color of the active navbar item?

My navigation bar consists of 3 buttons and I'm looking to customize the text color for the active page. Additionally, I want to set a default button which is the first one in the list. I attempted using StyledLink from another solution but it doesn& ...

Exploring the Benefits of Using Gatsby with Material-UI: The Importance of Creating a Page

Upon reviewing the gatsby demo showcased on the material-ui project github page, I found myself puzzled by a few lines of code. In the specific file getPageContext.js export default function getPageContext() { // Ensuring each server-side request has i ...

New update in Next.js version 13.4 brings a modification to routing system

I'm currently working with Next.js 13.4 and an app directory. I'm trying to implement a centrally located loader that will show whenever there is a route change anywhere within the app. Since routes/navigation don't have event listeners, I&a ...