Variables containing "." character in Handlebars? - handlebars.js

I am using Handlebars templates. If I want to insert a variable in an hbs file, I add it as {{var_name}}.
The issue is, I have a large object with its keys taking formats similar to:
{
"stats.name":"John",
"stats.performance.day":123,
"stats.performance.month":4567,
"company":"My LLC"
}
If I try to add these to the Handlebars file as {{company}}, {{stats.name}}, {{stats.performance.day}}, {{stats.performance.month}} then only the {{company}} will be displayed. All other values come out blank, I assume because "." is a special character in Handlebars.
My question is, is there a way to override this, or do I actually need to iterate through this object and change every "." to a "_" or something before passing it to Handlebars to make it work?

I would recommend renaming your properties. Dot Notation is one of the two ways to access object properties in JavaScript. Typical code would look like: obj.prop. If your object's keys are not valid identifiers, then you must use Bracket Notation: obj['my-prop']. Since a dot is a property accessor, it is very unusual to see one in a property name. If I were to come across code written as obj['stats.performance.date'], I would assume it was a mistake and that obj.stats.performance.date was intended.
With that said, Handlebars does support referencing properties that are not valid identifiers. This is called "segment-literal-notation", and it is as simple as wrapping square brackets around your identifier:
{{[stats.performance.day]}}
The documentation also states that:
JavaScript-style strings, " and ', may also be used vs. [ pairs.
So the following alternatives are valid:
{{"stats.performance.day"}}
{{'stats.performance.day'}}

Related

How to remove "/"/"" from data stored in firebase with App Inventor

When adding data with the block call.StorageValue, the string is saved in firebase with "/" before and after the string,
There does not seem to be any block to remove it, How can I do it?
It's a normal firebase function that allows to separate the values and read them as such.
Example :
on Firebase, "\"English-EN\"" is a single value sent from the app as English-EN
and "[\"863674037411046\",\"863674037411046\",\"863674037411046\",\"863674037411046\"]" is a list of numbers sent as 863674037411046.
Try to retrieve the value with a button and to a simple label and you should see that it's displayed without the extra characters.
Source:check my app "harpokrates". I've made it as a firebase DB management demo and it uses nothing else. All values are stored as you describe and are retrieved just fine, without extra symbols or any need to trim the text.
ps:However if you do have extra symbols at some point, check your use of lists and lists of lists that might generate excessive "\" if you made a mistake somewhere. You can also use the "trim" or "split text" blocks but that would be bad practice. Finding the code error is best.
This is likely an escape character that escapes the special character " (quotation marks). This is common practice to use \ as an escape character to indicate that the next character has special meaning, in this case it is not the start or end of a string but actually part of it.
As such you can't actually remove it (just the escape character) and should consider how you got a quotation mark in the string to begin with.
You should however be able to remove the entire quotation mark \"

In gatling, how do I validate the value of a string extracted via the css check?

I'm writing a Gatling simulation, and I want to verify both that a certain element exists, and that the content of one of its attributes starts with a certain substring. E.g.:
val scn: ScenarioBuilder = scenario("BasicSimulation")
.exec(http("request_1")
.get("/path/to/resource")
.check(
status.is(200),
css("form#name", "action").ofType[String].startsWith(BASE_URL).saveAs("next_url")))
Now, when I add the startsWith above, the compiler reports an error that says startsWith is not a member of io.gatling.http.check.body.HttpBodyCssCheckBuilder[String]. If I leave the startsWith out, then everything works just fine. I know that the expected form element is there, but I cant confirm that its #action attribute starts with the correct base.
How can I confirm that the attribute start with a certain substring?
Refer this https://gatling.io/docs/2.3/general/scenario/
I have copied the below from there but it is a session function and will work like below :-
doIf(session => session("myKey").as[String].startsWith("admin")) { // executed if the session value stored in "myKey" starts with "admin" exec(http("if true").get("..."))}
I just had the same problem. I guess one option is to use a validator, but I'm not sure how if you can declare one on the fly to validate against your BASE_URL (the documentation doesn't really give any examples). You can use transform and is.
Could look like this:
css("form#name", "action").transform(_.startsWith(BASE_URL)).is(true)
If you also want to include the saveAs call in one go you could probably also do something like this:
css("form#name", "action").transform(_.substring(0, BASE_URL.length)).is(BASE_URL).saveAs
But that's harder to read. Also I'm not sure what happens when substring throws an exception (like IndexOutOfBounds).

Meteor: How to use object names including dashes in Spacebars

Didn't find the answer anywhere, but maybe one of you knows it.
I'm getting back data from http.call('GET'), I can use the data correctly in
Spacebars like
{{anydata.specificdata}}
but have no chance to use data w/ object names containing dashes like
{{anydata.specific-data}}
I tried
{{anydata.'specific-data'}}
, but this does not work either.
As I'm retrieving a lot of different data I would like to avoid to create helpers for every field containing dashes.
Does anyone know how I could handle something like
{{anydata.specific-data}?
Thanks for any answer that helps.
Have Fun!
I found the answer on a meteor form: https://forums.meteor.com/t/dash-character-in-spacebars/2885
I ran into a similar problem. I had a json object with a dash in a the variable name. In order to call an object with a dash, the following is done:
{{some.json.object.[with-a-dash]}}
Note the . before the open bracket and no quotes (single or double) around the dash named item.
on the HTTP success callback map the data object properties from dash to camelcase, then use your new object.
Some days offline behind...
I did not find a nice solution (due to the unexpectable combination of arrays and objects in the htpp result) but a working one:
I use EJSON.stringify() to stringify the http-result and replace the dashes in the object keys using a regular expression, then use EJSON.parse() to make it an object again. Done.
Not very elegant but working fine and fast.
Have Fun!

Encoding wildcarding, stemming, etc in simple search

We have a simple search interface which calls the search:search($query-text) function. Is there a syntax to include control for wildcarding, stemming and case sensitivity within the single text string that the function accepts? I haven't been able to find anything in the MarkLogic docs.
See the $options parameter and the <term> and <term-option> constraint at https://docs.marklogic.com/search:search . There is a guide at http://developer.marklogic.com/learn/2009-07-search-api-walkthrough
and some details http://developer.marklogic.com/learn/2009-07-search-api-walkthrough#ndbba3437f320a4a4
I don't know of any existing syntax for those options, aside from the built-in behavior of turning on wildcards when a term contains '*' or '?' and turning on case-sensitivity when the term contains capital letters.
You could develop a syntax. Implementing it might involve a custom parser along the lines of https://github.com/mblakele/xqysp then feeding the resulting cts:query into search:resolve.
Piggybacking on Eric Bloch's answer... you can always dynamically construct your node based on input in the user interface.
For example, I often do this in order to separate the facet selection portion of the query from the text search portion and put the facet selection query in the additional-query element in the options node.

Is # inside razor syntax called operator?

I just bought a book on ASP.NET MVC With Razor View Engine. There is a subsection called Usage of # Operator and this subsection title makes me ... well, uncomfortable.
Is # inside the razor view engine called operator?
UPDATE
I guess my question is not so clear. I want to know if # is an operator inside the razor view engine. For example, < > = != >= => these are called as operator inside C# language. Is it same for # inside Razor view engine?
I think the reason for you discomfort is that the # token (when talking about it from a parsing perspective, though the word character should also do) is overloaded to indicate a number of different situations. Let's examine what those are:
Write a value to the output:
#this.Value
Indicate a code block transition:
#{
var foo = 1;
foo += 1;
}
Indicate a code statement transition:
<div>
#if(foo) {
// code
}
</div>
Indicate an escape to markup till the end of the line
#if(foo) {
foo = false;
#: value printed to output
}
Indicate directive statements:
#inherits MyCustomBaseType
Indicate special code blocks:
#section Foo {
<div />
}
#helper Bar(int param) {
param += 1;
}
Delimit comment blocks:
#*
This is a comment
*#
Escape the # character:
Email me at myemail##example.com
In my opinion only the first usage can be considered as an operator. The operand (i.e. everything else that follows the # up to but excluding the first markup-significant whitespace character) is passed as the parameter to the Write() method. All other usages don't have any clearly identifiable operands or require additional tokens (the * in the comment block, etc) to be indentified.
According to the official pages on Razor, you can find one example here, it does not seem that this is called an operator.
From the linked page:
You add code to a page using the # character
(my emphasis)
I also found numerous other pages on that same site, all referring it to just the "# character", so in that sense it isn't considered an operator.
<opinion>
However, if you read on Wikipedia on the topic of operators, then:
Syntactically operators usually contrast to functions. In most languages, functions may be seen as a special form of prefix operator with fixed precedence level and associativity, often with compulsory parentheses e.g. Func(a) (or (Func a) in LISP). Most languages support programmer-defined functions, but cannot really claim to support programmer-defined operators, unless they have more than prefix notation and more than a single precedence level. Semantically operators can be seen as special form of function with different calling notation and a limited number of parameters (usually 1 or 2).
(again, my emphasis)
Then I would argue that # is in fact an operator. It is a symbol with a specific meaning, and you could argue that you're "escaping out of the surrounding context to do something else", sort of like a function call.
In other words, while the word operator does not appear in the website articles I've seen so far, I would consider it to be an operator nonetheless.
</opinion>
Hmm, are you referring to the Fonts and Colors section of the Tools > Options dialog in Visual Studio? If so, no it isn't an Operator, it's "HTML Server-Side Script". The "functions", "section" and other razor-specific keywords are also this color.
Otherwise, yes it is an operator from a technical description. I'm not entirely sure what other distinction you are looking for.
No, the # character is not used as an operator. It's only the choise of that author to call it so.
For example, in the blog post introducing Razor it's not described as an operator anywhere.
There doesn't seem to be an official or de-facto term for it yet. Personally I would call it a tag, as it's used in the markup code along with other tags, and it's used as a replacement for the <% %> script tag.
I guess that it's not really incorrect to call it an operator, but it's not commonly used. As the razor tag contains C# or VB code which also can contain operators, it can get confusing.
The # character starts inline expressions, single statement blocks, and multi-statement blocks.
On Scott Gu's blog, he doesn't refer to it as an operator but there are lots of other developers who do. Personally, I don't see this as an operator but more of an identifier.
As far as I know are symbols like >, != and && called equality, relational or conditional operators. The # symbol does not seem to fit in any of those groups so I must say, no, it cannot be called an operator. On the other hand however, it does indicate that 'some operation' is performed which makes it rather likely to be called an operator.
The # symbol is not an operator, it is an indicator which indicates that a razor statement begins.
It is comparable with <?php ?> in php. This indicates to the compiler/interpreter where your code is
e.g.
#Html.ActionLink(...)
a #(...) gives you the oportunitie to do some coding int there
e.g
#(
int a = 0;
int b = 4;
int c = a + b;
)
I hope this helps you on the way

Resources