This question already has answers here:
How to call native es6 template string replacement from tag function?
(4 answers)
Closed 5 years ago.
With ES6 we can create "tag functions" which operate on a template string.
Is it possible to call the "default" tag function from within another one (that is, the one that is used when there is no prefix before the backtick
e.g.
function html(template, ...substitutions)
{
substitutions= substitutions.map(s=>SafelyEscapeStringForHtml(s));
return DefaultTagFunction(template, ...substitutions);
}
the upshot of which is that my tag function doesn't need to deal with the concatenation and ensuring the parameters are inserted into the correct locations in the template. (Sure this isn't hard, but it's ugly and I don't want to see it in every tag function!)
Your own answer came close - you can abuse String.raw to give you what you want (with interpreted escape sequences):
function html(template, ...substitutions)
{
substitutions= substitutions.map(s=>SafelyEscapeStringForHtml(s));
return String.raw({ raw: template }, ...substitutions);
}
This is essentially just tricking it into thinking that the escape-interpreted string is the raw version.
String.raw() is the default tag function
So the above code can be written as
function html(template, ...substitutions)
{
substitutions= substitutions.map(s=>SafelyEscapeStringForHtml(s));
return String.raw(template, ...substitutions);
}
Related
This question already has answers here:
React Native property values in quotes vs braces
(1 answer)
What do curly braces mean in JSX (React)?
(4 answers)
Closed 8 months ago.
<button className='btn' onClick={() => setValue(value - 1)}>
<form className='form' onSubmit={handleSubmit}>
I hope I don't get people riled up asking this. I tried to google and cannot find the answer. Why for some attributes in HTML uses 'xxx', eg classname='btn' and why some attributes uses {}, eg onSubmit={xxx}.
In the above case, the handleSubmit is a externally define function. Does {} imply a variable, that's all ? But I also saw some codes similar to this onSubmit={() => xxxx} which is an inline function and not an external variable.
Currently, I just memorise it as it come. Thanks very much !
I've tried to google my question but it makes me even more confused. My question is:
Here's the jQuery code:
$(document).ready(function() {
$(window).resize(function() {
if ($(this).width() < 200) {
$("p").css("color", "red");
} else {
$("p").css("color", "green");
}
});
}
Why do we write (this) and not ("this") ?
How do I know if (document) and (window) should be written with " " - and why's that?
Maybe you could link me somewhere that explains my issue. My code apparently works either way, I'm just curious about the why.
In JavaScript namespace, this is reserved [source].
The JavaScript object literal this refers to the inherited object from the present state in the current execution.
Another example of this we can see is when we are looping through an array and the object this would symbolize the current array object. You may, for example, see this.title, or this.description if we were iterating through a database array of blog posts.
this in jQuery refers to the inherited object. When we add the quotation marks, and it becomes a string, such as "this". This makes jQuery parse it as a DOM selector.
Then we are now looking for the HTML DOM selector <this>, which to my knowledge, does not actually exist in the accepted HTML syntax standards.
As otherwise stated, the concept of this will become tricky when you are working in other JavaScript environments, such as React or Angular. Within the context of a functional component, this becomes the state, such as handling user sessions.
This question already has answers here:
C++ global initialization order ignores dependencies?
(7 answers)
static initialization order fiasco
(4 answers)
Closed 3 years ago.
I'm developing a webserver using C++ and I want a string array to contain the HTML code of my HTML elements.
In the definition of that array, I want to supplement hard-coded characters with strings that are defined in other arrays.
For example, my webpages[] array would contain the HTML for each webpage and be defined as:
string webpages[NUM_WEBPAGES] =
{
...
"<p>Please login here</p>",
...
};
However, the HTML that gets sent to the client is:
<p>Please login here</p>
As you can see, the url from urls[PAGE_LOGIN] was not populated into the HTML.
I have confirmed via code inspection and gdb that urls[PAGE_LOGIN] has the value /login.
(1) Why is this not working?
(2) Is this possible?
(3) If no, any recommendations?
Thank you!
EDIT:
Minimal reproducible code example... because I don't know how to stackoverflow.
Also my C++ is bad.
webpages.h:
enum webpage
{
PAGE_LOGIN,
NUM_WEBPAGES
};
extern string webpages[NUM_WEBPAGES];
webpages.cpp:
string webpages =
{
"<p>Please login here</p>",
};
urls.h:
extern string urls[NUM_WEBPAGES];
urls.cpp:
string urls[NUM_WEBPAGES] =
{
"/login"
};
I have a generic template in play 2.6, that I want to pass in a variable amount of HtmlContents. I've defined the template like this (including the implicit parameter I have in case that changes anything):
#(foo: String)(content: Html*)(implicit bar: Bar)
On the template side, this works fine-- I can dissect content with for and render it as I want. However, I haven't been able to figure out a clean way to invoke the variable arguments from the underlying template.
e.g, I have a view named "Baz":
#(something: String)(implicit bar: Bar)
In it, I try to invoke the template with multiple Html arguments. I've tried the following:
#template("fooString"){{123},{abc}}
and
#template("fooString")({123}, {abc})
and
#template("fooString"){{123}, {abc}})
And various other permutations, but inside of an enclosing bracket it seems to interpret everything literally as a single parameter in the HtmlContent vararg.
However, this ended up working as I intended, passing in multiple HtmlContents:
#template("fooString")(Html("123"), Html("abc"))
So that works, and I can use a triple-quoted interpolated string for a large Html block-- but it seems like there should be a cleaner way to do this, and the string interpolation is dangerous as it doesn't do html escaping.
Is there a way to do this using the { enclosed syntax? I'd like to understand more what is actually happening on an underlying level, and how play parses and generates HtmlContent in brackets.
So consider you have below template
// main.scala.html
#(title: String)(contents: Html*)
There are different ways you can call this template
Option #1
This is what you already posted in the question
#main("This is a title")(Html("abc"), Html("123"))
Options #2
#html1 = {
Hello
}
#html2 = {
<div>Tarun</div>
}
#main("This is a title")(html1, html2)
Option #3
#main("This is a title")(Html(<div>Tarun
</div>.toString), Html(<div>
Lalwani
</div>.toString))
Options #4
This is not exactly same option, but needs change in Template signature itself
#(title: String)(contents: List[String])
And then calling it like below
#main("This is a title")(List(
"""<div>
Tarun
</div>
""", """Hello"""))
Option #5
This requires code files and was already answered on another SO thread
Paul Draper's answer on Why doesn't this pass multiple HTML parameters to template
Only in Mozilla Dev. Network is a function declaration explained with the following pseudoCode:
function name([param,[, param,[..., param]]]) {
[statements]
}
Is there any special significance or reason why the parameter list is represented as a nested list instead of just listing out the parameters as can be seen in any other function declaration on the Web?
Why not just show the declaration simply like:
function name(param1, param2, paramN...,) {
[statements]
}
Am I looking into this too much? Or is it just the Mozilla way of explaining the declaration?
The syntax shows when a parameter is optional