What mean #:# in Razor syntax? - razor

Maybe stupid question, but i didn't find what it mean.
code example:
#if (isSomething ) {
#:#Scripts.Render("~/scripts/some-scripts")
}

That seems redundant. The #: specifies literal content, but then # takes you back into the server code context. It should work the same as:
#if (isSomething ) {
Scripts.Render("~/scripts/some-scripts")
}

Related

Replace Quotation in List of Lists R

I am trying to get a JSON response from an API:
test <- GET(url, add_headers(`api_key` = key))
content(test, 'parsed')
When I run content(test, 'parsed'), I get the following error:
# Error: lexical error: invalid string in json text. .Note: Final passage of the "fiscal cliff bill" on January 1
I think this is because of the double quotations. How can I either replace the double quotes or if this is not the problem, how can I fix this issue?
Thanks!
So I had run into a similar problem before, and I had intended to write a quite function to use Jeroen's fix to try to repair the JSON. Since I intended to do it anyway, here's a quick hack attempt.
NB: repairing a structured format like this is speculative at best and most certainly prone to errors. The good news is that I tried to keep this specific enough so that it will not produce false results: it'll either fix what it knows it can, or fail. The "unit-testing" really needs to check other corner-cases. If you find something that this does not fix (and should) or that this breaks (gasp!), please comment!
fix_json_quotes <- function(s) {
if (length(s) != 1) {
warning("the argument has length > 1 and only the first element will be used")
s <- s[[1]]
}
stopifnot(is.character(s))
val <- jsonlite::validate(s)
while (! val) {
ind <- attr(val, "offset") - 1
snew <- gsub("(.*)(['\"])([[:space:],]*)$", "\\1\\\\\\2\\3", substr(s, 1, ind))
if (snew != substr(s, 1, ind)) {
s <- paste0(snew, substr(s, ind + 1, nchar(s)))
} else {
break
}
val <- jsonlite::validate(s)
}
if (! val) {
# still not validating
stop("unable to fix quotes")
}
return(s)
}
Some sample data, unit-testing if you will (testthat is not required for use of the function):
library(testthat)
lst <- list(a="final \"cliff bill\" on")
json <- as.character(toJSON(lst))
json
# [1] "{\"a\":[\"final \\\"cliff bill\\\" on\"]}"
Okay, there should be no change:
expect_equal(json, fix_json_quotes(json))
Some bad data:
# un-escape the double quotes
badlst <- "{\"a\":[\"final \"cliff bill\" on\"]}"
expect_error(jsonlite::fromJSON(badlst))
expect_equal(json, fix_json_quotes(badlst))
PS: this looks specifically for double-quotes, nothing more. However, I believe that there are related errors that this might also be able to fix. I "left room" for this, in the second group within the regex (([\"])); for example, if single-quotes could also cause a problem, then the group could be changed to be ([\"']). I don't know if it's useful or even necessary.

angular 2+ component with attribute name and no parameters

I want to allow a user to provide a list of one-word attributes without parameter values. For example,
<container row crosscenter wrap spacearound ...>
which results in something like this in container.html
<div [ngClass]="{ 'flexDisplay': true, 'directionRow': isRow, 'directionCol': isCol, 'contentSpaceAround': isSpaceAround}" ...>
What I'm missing is how to set
#Input('row') isRow = false;
to true if 'row' was present in the container line.
Any ideas?
Thanks in advance.
Yogi
This can be handled in ngOnChanges. The value can be assigned either back to input property or to some object that will be passed to ngClass
ngOnChanges(changes: SimpleChanges) {
if ('foo' in changes) {
this.options.foo = true;
}
}
Since there's no way how inputs can become unassigned, there's no reason to provide bindings for them. #Attribute can be used instead:
constructor(#Attribute('foo') public foo: boolean|null) {
this.foo = (foo != null);
}
Using attributes for regular options isn't a good decision, design-wise. This prevents from setting them dynamically. Instead, it is always preferable to accept options input. If all options are supposed to be flags, it can be a string input that will be split and processed in ngOnChanges, like:
<container options="row crosscenter wrap spacearound">
or
<container [options]="'row crosscenter wrap spacearound'">
I think the answer to my question is to create directives for each of the "one-word" tags (attributes) I want to use.
:-)

Check for "Not in" a list condition Angular 2 template

I have a template as below:
<span *ngIf="item.status !=='E1' && item.status !=='B' && item.status !=='R'">{{item.status_desc}}</span>
As above, i have a ngIf condition there, making no sense but somehow it working. What am trying to do there is check "status in [E1, B, R]" something like that. How can i do that in the html without going to the ts file. Any idea guys?
In your HTML, you could use includes(), which returns true if the element is found:
<span *ngIf="!['E1', 'B', 'R'].includes(item.status)">{{item.status_desc}}</span>
as JB Nizet suggested.
Or you could use a function, like this:
statusList = [E1, B, R];
checkStatus(item)
{
return (statusList.indexOf(item) != -1);
}
where your HTML now should llol like this:
<span *ngif="checkStatus(item.status)">{{item.status_desc}}</span>
If you really don't want to go to your TypeScript source, you can do something like this for increased readability.
<span *ngIf="!['E1', 'B', 'R'].includes(item.status)">{{item.status_desc}}</span>
But perhaps it's wiser to make a variable on your class with the 'undesired' statuses like:
public ignoreStatus: string[] = ['E1', 'B', 'R'];
and then
<span *ngIf="!ignoreStatus.includes(item.status)">{{item.status_desc}}</span>
but then it would be even better to make a reusable method out of this in your class:
public isIgnoreStatus(item: any): boolean {
return this.ignoreStatus.includes(item.status);
}
with
<span *ngIf="!isIgnoreStatus(item.status)">{{item.status_desc}}</span>
No you cannot do that with template, what you can do is create a function that does the job for you
statuses = [E1, B, R];
checkValid(item){
return (statuses.indexOf(item) != -1);
}
then in HTML
<span *ngIf="checkValid(item.status)">{{item.status_desc}}</span>
You can create a pipe to do all this filters.
In that pipe you can write any logic to remove all unwanted elements.
It will make your code look better and understandable.

Regex javascript exclude one character

I need to find this string:
{ "any string but :",
for example:
{ id,
{ this.content,
{ var = {123},
I'm searching it on Notepad++ with:
{ (.*?),
But I don't know how to exclude the : with
^\:\
Any help? thanks.
Undesire results:
{ id:id,
I'm searching for a javascript syntax error. By mistake, someone has defined JSONS as:
{ id, data, etc, etc,...}
When they have to be (this is the correct way):
{ id: id, data: something, etc:etc, etc:something,...}
So I want to find where that string doesn't have the : (which is correct)
You could use something like this:
{[^:]*?,
Working demo
Update: I noticed you updated your question. So, for your edited question you can use:
{[^:]*?,|(?<=, )\w+,
Working demo 2

How to put block comments in Tcl?

I have code here
proc checkPrime {no} {
set i 1
set count 0
while {$i < $no} {
if {{$no%$i} eq 0} {
incr count
}
if {$count eq 2} {
puts "the number is prime number"
return
}
incr i
}
}
I want to put the whole procedure into a single comment, I don't want to have to put # before each line.
Is there any possibility to comment multiple lines in Tcl, as there is in Java using /* .. */?
I also want some of the text will be put into a single comment
Apart from the if {0} .. which is idiomatic (and one that most tcl programmers recognize) you can also use any other construct and stuff the things you want commented out in brace quotes. The real mechanism preventing execution here is that things inside brace quotes don't get substituted.
Here are some of my favourites. I like them because they are self-documenting:
set COMMENTED_OUT {
commented out stuff
}
and
proc COMMENTED_OUT {} {
commented out stuff...
}
I tend to prefer the proc because the block of commented out text is really a block of code.
Note that tcl does not compile proc bodies until first execution so commenting out using a proc is as cheap as set and if {0} ...
Use something along these lines:
if { 0 } {
a section
of
"commented" code
}