Adding html in an if statement nested in a for loop - razor

I have code like this in my razor index view:
#for (decimal hour=7m; hour<=20.5m; hour +=0.5m)
{
var item = #Model.Items.FirstOrDefault( i=> i.Hour == hour);
if (item != null)
{
<td colspan="#item.TimeBlocks">
#item.Description
</td>
hour += (item.TimeBlocks-1)* 0.5m;
}
else
{
<td>
+
</td>
}
}
For some reason, the VS2013 consistently tells me "} expected" in the Error List window. The syntax highlighter shows everything highlighted fine until the curly brace right before the else.
I have tried using #: for the html tags. It works fine in the if block, but in the else block I get the following error:
":" is not valid at the start of a code block.
I have tried wrapping the html tags in a tag as well, but that doesn't work either.
How would I do this to get it to render?

You have a mistake in your code.
The following line
var item = #Model.Items.FirstOrDefault( i=> i.Hour == hour);
Should actually read:
var item = Model.Items.FirstOrDefault( i=> i.Hour == hour);
Removing that will fix your code

Related

HTML inside success parantheses

I am simply trying to display some custom messaging on success/failure scenario.
.subrcribe (
success => {
this.clear();
this.success("<img>First sentence.<br><img>Second sentence!");
},
err => {
this.error("error");
}
)
everything works fine, no syntax errors or anything. I know there is a way to display custom html in success/failure scenario. when I do it the way mentioned above, the message is displayed in a single line like..
" < img > First sentence. < br > < img >Second sentence!" like a text.
Any inputs? thankyou, in advance.

creating table from 2-Dimensional array in perl has different outputs

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).

VS2013 Razor and Format Document

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.

Why won't CSS render around an element containing nil?

I have a problem with CSS not working properly in a combination of HAML, Backbone-text-injection, and some Rails JSON rendering.
The offending code is here:
%td{:class => 'zone_a'}
= start_time(z.zone, 'A')
start_time just does :
def start_time(zone, letter)
if zone
program = zone.programs.where(:identifier => letter).first
if program
program_zone = ProgramZone.where(:program_id => program.id, :zone_id => zone.id).first
if program_zone.run_time.to_i > 946684800
return program_zone.run_time.strftime("%H:%M")
else
return nil
end
else
return nil
end
else
return nil
end
end
And the nil which is returned renders in the page.
Let's look at what it looks like when we have a box with a value and then three boxes with nil:
The top and bottom black borders called from the %tr.inspection_zone element, that is supposed to affect all %td elements in the entire row, is having no effect here.
So this is the question - a legit SO question.
I "solved" this by putting in a non-breaking space:
%td{:class => 'zone_a'}
= " ".html_safe + start_time(z.zone, 'A')
What I'm looking for is not a work-around but rather a reason why CSS won't effect an element with a nil text/html value. And from that reason, there is probably a better solution than what I've done. I feel like my approach is just duct-taping a busted pipe.
EDIT:
Adding the rendered HTML:
Without the
<td class="zone_b"></td>
With the
<td class="zone_b">
</td>
Here is an image of the CSS - it's the same either way:

Macro broken after upgrade to 4.10

I recently upgraded from 4.09 to Umbraco 4.10. I have a pretty common/simple little macro that I use to generate my top navigation. I check the record to see if it's the last in the count, and if not add a pipe symbol as a visual separator.
This worked as expected with all version thru 4.09 but after upgrade all item get the pipe symbol even the last one.
#inherits umbraco.MacroEngines.DynamicNodeContext
#{
var level = String.IsNullOrEmpty(Parameter.Level) ? 1 : int.Parse(Parameter.Level);
var ulClass = String.IsNullOrEmpty(Parameter.UlClass) ? "" : String.Format(" class=\"{0}\"", Parameter.UlClass);
var parent = #Model.AncestorOrSelf(level);
var pages = parent.Children.Where("Visible");
if (parent != null) {
<ul class="topNav">
#foreach (var item in pages) {
var selected = Array.IndexOf(Model.Path.Split(','), item.Id.ToString()) >= 0 ? " class=\"selected\"" : "";
if (item != (pages.Last()))
{
<li#Html.Raw(selected)>
#item.Name |
</li>
}
else
{
<li#Html.Raw(selected)>
#item.Name
</li>
}
}
</ul>
}
}
I'm not sure what changed, but it seems like (in my code) pages.Last() is never true.
Anyone have any ideas? - Thanks
You should be able to get the result you want by using the IsNotLast() helper, like so:
<li#Html.Raw(selected)>
#item.Name#(item.IsNotLast() ? " |" : "")
</li>
Refer to the Umbraco Razor Feature Walkthrough–Part 7 for more information on the IsHelpers that are available. They're pretty handy when you want information about the nodes you're traversing.
I ended up going at it a little bit differently, but I still have no idea why this changed.
instead of
if (item != (pages.Last()))
I went with
if (item.Index() != (pages.Count()-1))
solved for now, but I welcome any feedback if anyone else stumbles on this issue