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"
};
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 !
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);
}
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
This question already has answers here:
Selecting attribute values with html Agility Pack
(7 answers)
Closed 8 years ago.
I want to get the href-link from this:
<a class="abc" href="/subsite/2014/05/19/site.html"> <p>test1</p><p>test2</p> </a>
I'm trying this:
var nodes = doc.DocumentNode.SelectNodes("//a[#class='abc']/#href");
...InnerHtml becomes <p>test1</p><p>test2</p>, not the link in the href...
As explained by HtmlAgilityPack creator, #SimonMourier, here, you can't use SelectNodes() directly to get attributes (as the method name and the return type implies, it meant to select nodes).
You need to do it with different approach. Try to select the node instead of the attribute, then you can use LINQ extension method to extract attribute of each selected nodes, for example :
var attrs = doc.DocumentNode
.SelectNodes("//a[#class='abc' and #href]")
.Select(o => o.Attributes["href"]);
This will work too, as described in the link from #Tomalak.
//Load navigator for current document
HtmlNodeNavigator navigator = (HtmlNodeNavigator)doc.CreateNavigator();
//Get value from given xpath
string xpath = "//a[#class='abc']/#href";
var val = navigator.Select(xpath);
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
<div id="example-value"> or <div id="example_value">?
This site and Twitter use the first style. Facebook and Vimeo - the second.
Which one do you use and why?
Use Hyphens to ensure isolation between your HTML and JavaScript.
Why? see below.
Hyphens are valid to use in CSS and HTML but not for JavaScript Objects.
A lot of browsers register HTML Ids as global objects on the window/document object, in big projects, this can become a real pain.
For this reason, I use names with Hyphens this way the HTML ids will never conflict with my JavaScript.
Consider the following:
message.js
message = function(containerObject){
this.htmlObject = containerObject;
};
message.prototype.write = function(text){
this.htmlObject.innerHTML+=text;
};
html
<body>
<span id='message'></span>
</body>
<script>
var objectContainer = {};
if(typeof message == 'undefined'){
var asyncScript = document.createElement('script');
asyncScript.onload = function(){
objectContainer.messageClass = new message(document.getElementById('message'));
objectContainer.messageClass.write('loaded');
}
asyncScript.src = 'message.js';
document.appendChild(asyncScript);
}else{
objectContainer.messageClass = new message(document.getElementById('message'));
objectContainer.messageClass.write('loaded');
}
</script>
If the browser registers HTML ids as global objects the above will fail because the message is not 'undefined' and it will try to create an instance of the HTML object. By making sure an HTML id has a hyphen in the name prevents conflicts like the one below:
message.js
message = function(containerObject){
this.htmlObject = containerObject;
};
message.prototype.write = function(text){
this.htmlObject.innerHTML+=text;
};
html
<body>
<span id='message-text'></span>
</body>
<script>
var objectContainer = {};
if(typeof message == 'undefined'){
var asyncScript = document.createElement('script');
asyncScript.onload = function(){
objectContainer.messageClass = new message(document.getElementById('message-text'));
objectContainer.messageClass.write('loaded');
}
asyncScript.src = 'message.js';
document.appendChild(asyncScript);
}else{
objectContainer.messageClass = new message(document.getElementById('message-text'));
objectContainer.messageClass.write('loaded');
}
</script>
Of course, you could use messageText or message_text but this doesn't solve the problem and you could run into the same issue later where you would accidentally access an HTML Object instead of a JavaScript one
One remark, you can still access the HTML objects through the (for example) window object by using window['message-text'];
I would recommend the Google HTML/CSS Style Guide
It specifically states:
Separate words in ID and class names by a hyphen. Do not concatenate words and abbreviations in selectors by any characters (including none at all) other than hyphens, in order to improve understanding and scannability.
/* Not recommended: does not separate the words “demo” and “image” */
.demoimage {}
/* Not recommended: uses underscore instead of hyphen */
.error_status {}
/* Recommended */
#video-id {}
.ads-sample {}
It really comes down to preference, but what will sway you in a particular direction might be the editor you code with. For instance, the auto-complete feature of TextMate stops at a hyphen, but sees words separated by an underscore as a single word. So class names and ids with the_post work better than the-post when using its auto-complete feature (Esc).
I believe this is entirely up to the programmer. You could use camelCase too if you wanted (but I think that would look awkward.)
I personally prefer the hyphen, because it is quicker to type on my keyboard. So I would say that you should go with what you are most comfortable with, since both your examples are widely used.
Either example is perfectly valid, you can even throw into the mix ":" or "." as separators according to the w3c spec. I personally use "_" if it is a two word name just because of its similarity to space.
I use the first one (one-two) because its more readable. For images though I prefer the underscore (btn_more.png). Camel Case (oneTwo) is another option.
Actually some external frameworks (javascript, php) have difficulties (bugs?) with using the hypen in id names. I use underscore (so does 960grid) and all works great.
I would suggest underscore mainly for the reason of a javascript side-effect I'm encountering.
If you were to type the code below into your location bar, you would get an error: 'example-value' is undefined. If the div were named with underscores, it would work.
javascript:alert(example-value.currentStyle.hasLayout);