I am reading a example from a book like this:
#switch ((int)ViewBag.ProductCount) {
case 0:
#: Out of Stock
break;
case 1:
<b>Low Stock (#ViewBag.ProductCount)</b>
break;
default:
#ViewBag.ProductCount
break;
}
so my question is how do we determine when and where do we need the "#" ? for example ViewBag.ProductCount does not have it but then in case 1: it does have it.
You need the # sign before the start of a code block:
#{
// code here
}
Or before a control of flow statement that appears within mHTML markup#
#if(this || that){
<h1>Hello World</h1>
}
else
{
<h1>Good bye</h1>
}
Or before a server-side statement, expression or value that needs to be rendered to the browser:
#Datetime.Now // will render the current time to the browser
#(2/2 == 1) // will render 'true' to the browser
You can read more at my site here: http://www.mikesdotnetting.com/Article/153/Inline-Razor-Syntax-Overview
Related
I would like to write a snippet in VS Code that writes a "switch" expression (in Javascript), but one where I can define the number of cases.
Currently there is a snippet that produces the outline of a switch expression with 1 case, and allows you to tab into the condition, case name, and the code contained within.
I want to be able to type "switch5" ("5" being any number) and a switch with 5 cases to be created, where I can tab through the relevant code within.
I know the snippets are written in a JSON file, can I include such conditional logic in this, or is it not possible?
Thanks!
The short answer is that you cannot do that kind of thing in a standard vscode snippet because it cannot dynamically evaluate any input outside of its designated variables with some limited workarounds like I'll mention next.
You might - I and others have written answers on SO about his - type your various case values first and then trigger a snippet tat would transform them into a switch statement. It is sort of doing it backwords but it might be possible.
There are extensions, however, that do allow you to evaluate javascript right in a snippet or setting and output the result. macro-commander is one such extension. I'll show another simpler extension doing what you want: HyperSnips.
In your javascript.hsnips:
snippet `switch(\d)` "add number of cases to a switch statement" A
``
let numCases = Number(m[1]) // 'm' is an array of regex capture groups
let caseString = ''
if (numCases) { // if not 'switch0'
let tabStopNum = 1
caseString = `switch (\${${tabStopNum++}:key}) {\n`
for (let index = 0; index < m[1]; index++) {
caseString += `\tcase \${${tabStopNum++}:value}:\n\t\t\$${tabStopNum++}\n`
caseString += '\t\tbreak;\n\n'
}
caseString += '\tdefault:\n'
caseString += '\t\tbreak;\n}\n'
}
rv = `${caseString}` // return value
``
endsnippet
The trickiest part was getting the unknown number of tabstops to work correctly. This is how I did it:
\${${tabStopNum++}:key}
which will resolve to ${n:defaultValue} where n gets incremented every time a tabstop is inserted. And :defaultValue is an optional default value to that tabstop. If you don't need a defaultValue just use \$${tabStopNum++} there.
See https://stackoverflow.com/a/62562886/836330 for more info on how to set up HyperSnips.
I'm generating an HTML report using protractor-html-screenshot-reporter.
I get false/true under the Passed column but I want Passed/Failed instead.
Expected - Failed(in red) or Passed(in green)
Actual - False(in red) or True(in green)
Code Snippet -
function defaultMetaDataBuilder(spec, descriptions, results, capabilities) {
var metaData = {
description: descriptions.join(' ')
, passed: results.passed()
, os: capabilities.caps_.platform
, browser: {
name: capabilities.caps_.browserName
, version: capabilities.caps_.version
}
, message: ''
}
If I replace passed: results.passed() by this code -
passed: results.passed() ? 'Passed' : 'Failed'.
I get Passed/Failed instead of True/False but Failed also comes in Green.
How should I handle this scenario. Any suggestions are always welcome
You have to alter how the page is rendered. Looking at source code for protractor-html-screenshot-reporter I can see that the page is fully created in javascript file.
Go to this library source code to jsonparser.js(protractor-html-screenshot-reporter/lib/jsonparser.js) and modify function generateHTML(data). Currently it looks at data.passed to see if this boolean is true or false. Based on that it generates color and prints this value to the column. You want to edit this line to something like this:
str += '<td class="status-col" style="color:#fff;background-color: '+ bgColor+'">' + (data.passed ? "Passed" : "Failed") + '</td>';
Personally if you expect to modify this page even more, I would advice you to move this code into html page instead of generating it inside javascript file.
I can't figure out how to mix html with server code in this scenario.
#{
var i = 0;
foreach (var match in Model.StagingRooms)
{
if (i % 2 == 0)
{
<div class="row">
}
Html.Partial("_MatchCard", match.Value);
i++;
if (i % 2 == 0)
{
</div>
}
}
}
Using the code above, instead of rows of cards, I get an output of my code.
If I add # to Html.Partial and the increment
I also tried to append # to each server code line, and removing the #{} block, however this doesn't let me compile at all. I get a bunch of red squiggles in my code.
Edit:
When I add # to every server code snippet then I get squiggles, and can't compile
If I remove # from the last if statement, then I can run the app, but that piece of code is displayed back to me in the browser page.
You need to use an # here: #Html.Partial(....).
Also #foreach and #if, as Brad said.
And code lines go in brackets: #(i++)
Hi I am generating table from a 2-Dimensional array in perl.
But the output of my program is different if viewed in browser and viewing page source using developers tool in chrome:
Let me explain-
I have a subroutine to print the table from #RESULT array, the code is below
sub printTableFormattedEmpty {
my #array= #_ ;
print "<table border='0' cellspacing='0' bgcolor='#cfcfcf' cellpadding='0'>\n";
for(my $row_i = 0; $row_i < #array; $row_i++) {
print "<tr style='background-color:#B39DB3;'>\n";
for(my $column_i = 0; $column_i < #{ $array[$row_i] }; $column_i++) {
my $th = ($row_i == 0) ? "th" : "td";
print "</$th>";
print "$array[$row_i][$column_i]";
my $close = ($row_i == 0) ? 'th' : 'td';
print "</$close> \n";
}
print "</tr> \n";
}
print "</table> \n";
}
and i am calling the subroutine as
{
print "Table starts here!\n";
#$RESULT[0]- is array of many elements. u can see in output image
$RESULT[1][0]= 'No Active bookings available for you !';
$RESULT[2][0]= 'Click here to create new Booking !';
&printTableFormattedEmpty(#RESULT);
}
Now here i am not getting the expected output in a table , i am getting different output as shown in 2 figure:
when i inspect element and inspect the table i get:
But when i view page source of the page iam getting output formatted as table as shown in the fig:
I am really confused with this two types of Output, the both images are of the same page without refreshing.
How is this possible!
Did i do any mistake in my program or its something else.
Please Help me with This.
This is a typo!
There is a slash / in your opening HTML tag output.
for(my $column_i = 0; $column_i < #{ $array[$row_i] }; $column_i++) {
my $th = ($row_i == 0) ? "th" : "td";
# V HERE
print "</$th>";
print "$array[$row_i][$column_i]";
my $close = ($row_i == 0) ? 'th' : 'td';
print "</$close> \n";
}
Remove that slash and it will be fine.
As to why your two outputs are different: The HTML inspector shows the DOM structure after it has been parsed by the browser. It does not include invalid elements. Since stray closing elements are not valid, it's likely the parser just omitted them, so they are gone.
Viewing the source code on the other hand shows the real, unparsed code, which contains the wrong markup with the faulty HTML tags included. That is also where I saw the extra slashes. (read: your variable names are badly chosen. You would have seen it yourself had it been something like $open_tag and $closing_tag).
Formatting this part of code via Ctrl+E, D:
if (row % 3 == 0)
{
#:</div>
}
gives me:
if (row % 3 == 0)
{
#:
</div>
}
which makes my .cshtml document invalid.
Any suggestions on how to prevent this in VS2013, but that my other code still gets proper formatting using Ctrl+E, D?
I get the same issue as you. The only way I managed to stop it was by rewriting it like this (you won't need the initial # before the if as long as you are already in a code block, but I did when I pasted it into my page to test it):
#if (row % 3 == 0)
{
#Html.Raw("</div>")
}
I first learnt about using #: as a replacement for when #Html.Raw didn't work.