template-literals

ES6 template literals based on template variable [duplicate]

会有一股神秘感。 提交于 2020-01-13 10:03:45
问题 This question already has answers here : Convert a string to a template string (18 answers) Closed 3 years ago . I try to render a ES6 template literal variable : function render(template, data){ ... } const template = 'resources/${id}/'; console.log(render(template, {id: 1})); // -> resources/1/ Does exist a way to transform a string template with context into a formated string with ES6 template literals feature ? 回答1: You can not do this with simple template literals. However, you can

ES6 template literals based on template variable [duplicate]

一个人想着一个人 提交于 2020-01-13 10:02:24
问题 This question already has answers here : Convert a string to a template string (18 answers) Closed 3 years ago . I try to render a ES6 template literal variable : function render(template, data){ ... } const template = 'resources/${id}/'; console.log(render(template, {id: 1})); // -> resources/1/ Does exist a way to transform a string template with context into a formated string with ES6 template literals feature ? 回答1: You can not do this with simple template literals. However, you can

Can ES6 template literals be substituted at runtime (or reused)?

独自空忆成欢 提交于 2019-12-27 10:20:34
问题 tl;dr: Is it possible to make a reusable template literal? I've been trying to use template literals but I guess I just don't get it and now I'm getting frustrated. I mean, I think I get it, but "it" shouldn't be how it works, or how it should get. It should get differently. All the examples I see (even tagged templates) require that the "substitutions" be done at declaration time and not run time, which seems utterly useless to me for a template. Maybe I'm crazy, but a "template" to me is a

Split single line string into multiline string in JavaScript/p5.js

早过忘川 提交于 2019-12-25 07:25:45
问题 I have a .csv file that I'm calling in JavaScript through a p5.js sketch. One of the fields contains sentences that range from 103 char to 328 char. My script calls the data and displays in randomly on the canvas. Because some of the sentences are very long, they aren't fitting on the canvas properly, so I'd like to split them into 2- or 3-line strings. I've read up on Template Literals and RegExp in the JavaScript documentation, but all of the examples use string variables written out as a

Defining a function inside a template literal

蓝咒 提交于 2019-12-23 18:28:38
问题 I'm using styled-components as a solution to styling for React. They've got a nice approach that uses template literals to interpolate CSS. The template literals are passed the component's props so that you can do things like: const PasswordsMatchMessage = styled.div` background: ${props => props.isMatching ? 'green' : 'red'}; ` The result is a component that renders a div tag with either a green or red background depending on the value of isMatching . The above would be used via JSX like

How to call calling a function inside a Template literal

末鹿安然 提交于 2019-12-23 10:25:28
问题 How can I go about calling a function inside a Template literal. The function syntax in the attempt below shows in the HTML: function readURL(input) { if (input.files && input.files[0]) { var reader = new FileReader(); var html = ` <div class="row"> ${reader.onload = function (e) { $('#image_upload_preview').attr('src', e.target.result); }} <img id="image_upload_preview" src="http://placehold.it/100x100" alt="your image" /> </div> `; $("#test").append(html); reader.readAsDataURL(input.files[0

Template Literals with a ForEach in it

风流意气都作罢 提交于 2019-12-22 03:24:08
问题 Is it possible to return a string value in ForEach in a Template literal so it will be added in that place? Because if I log it it returns undefined . Or is that like I typed not possible at all? return `<div> <form id='changeExchangeForViewing'> <label for='choiceExchangeForLoading'>Change the exchange</label> <div class='form-inline'> <select id='choiceExchangeForLoading' name='choiceExchangeForLoading' class='form-control'> ${Object.keys(obj).forEach(function (key) { return "<option value=

How to nest template strings in ES6?

六月ゝ 毕业季﹏ 提交于 2019-12-19 05:15:07
问题 I got an prefer-template error from eslint. For the workaround, I changed my code to use a template string inside the require function which is nested inside the url function as following: { background: `url(${require(`../../assets/${edge.node.name.toLowerCase()}.png` center no-repeat`)}) } However, that gave an error, obviously. Here is the code I was using before, a plus sign concatenating inside the require function instead of the template string. { background: `url(${require('../../assets

How to use es6 template literal as Angular Component Input

落花浮王杯 提交于 2019-12-18 18:54:49
问题 In my Angular 4 application, I have a component which takes a string input: <app-my-component [myInput]="'some string value'"></app-my-component> In some cases I need to pass a variable inside the string, for example: <app-my-component [myInput]="'My name is ' + name + '!'"></app-my-component> it would be nice if I could use es6 template literals (aka template strings or back-tick strings ): <app-my-component [myInput]="`My name is ${name}!`"></app-my-component> but it doesn't work: Uncaught

ES6: Bad character escape sequence creating ASCII string

亡梦爱人 提交于 2019-12-17 17:08:08
问题 Here's my code: let padded = "03"; ascii = `\u00${padded}`; However, I receive Bad character escape sequence from Babel. I'm trying to end up with: \u0003 in the ascii variable. What am I doing wrong? EDIT: Ended up with ascii = (eval('"\\u00' + padded + '"')); 回答1: What am I doing wrong? A unicode escape sequence is basically atomic. You cannot really build one dynamically. Template literals basically perform string concatenation, so your code is equivalent to '\00' + padded It should be