In my cshtml files I have a lot of blocks with stuff like this:
#if(Model.foo)
{
<span>Hello World</span>
}
The only reason the span is there is because I can't find any other way to force it to recognize that "Hello World" is part of the html unless I surround it in html tags. Is there a good way to escape the code that doesn't involve adding meaningless tags to the display?
You could use #: to escape:
#if(Model.foo)
{
#:Hello World
}
or the special <text> tag which is not outputted in the response:
#if(Model.foo)
{
<text>Hello World</text>
}
#if(Model.foo)
{
#:Hello World
}
<text>Explicit HTML<text>
#(Explicit C#)
You can add text in as below:
#if(Model.foo)
{
#:Hello World
}
when you use # razor switch it to code block mode. Hence you need to specify text as above.
many developers has provided many ways above .. here is one more which is working fine in MVC 4 .. I hope it will work for MVC 3 also ..
#if(Model.foo)
{
#Html.Label("Hello World")
}
The above answers are great. I'm going to include a link to Scott Guthrie's article on this, since it shows some more examples and explanations.
https://weblogs.asp.net/scottgu/asp-net-mvc-3-razor-s-and-lt-text-gt-syntax
#if (p.UnitsInStock == 0 {
<text>
Donec in ante vitae purus consequat laoreet ut elementum
purus. Ut ut tempus nulla, quis ultrices est. Integer
pharetra ante in lectus porta, a lacinia ex faucibus.
Aliquam magna risus, pretium vel neque at, laoreet
ultrices lectus. Morbi posuere luctus risus. Nullam
tincidunt massa egestas nunc tempor scelerisque.
</text>
}
#if (p.UnitsInStock == 0 {
#: Line 1
#: Line 2
#: Line 3
}
Related
Is there a convention saying how long a line of comment should be?
Different line lengths will not work for any monitor size, or even font size, and other collaborators reading the code will have to scroll back and forth horizontally to read the entire line. I am wondering how this type of stuff is solved in the real world where people collaborate with other coders. Or is there some kind of auto formatter where people can change this kind of stuff on-the-fly to their own taste maybe?
For example:
/**
* Lorem ipsum dolor sit amet, consectetur
* adipisicing elit. Libero pariatur maxime
* praesentium. Similique qui, non aliquam
* nulla libero tenetur voluptatibus delectus,
* ex ab, aspernatur nam animi ipsa. Beatae
**/
versus:
/**
* Lorem ipsum dolor sit amet, consectetur adipisicing elit. Veniam iste magni optio
* impedit rem labore minima cumque quaerat corrupti suscipit dicta, possimus commodi
* laudantium ipsam, tenetur. Omnis qui, veritatis eligendi dignissimos rem quos
* voluptatum quaerat neque at excepturi quod fugit veniam sapiente sunt blanditiis,
* autem illum alias delectus illo, nostrum, dolore aspernatur culpa. Omnis ea deserunt,
* vero odio praesentium repellendus enim recusandae at temporibus officia, obcaecati
**/
(I apologise if this is the wrong stackexchange site to ask this, I honestly didn't know where to put it)
Is it possible to create such a design with Bootstrap: the image taking the top left and surrounded by text on wide devices and the image on top of the text on narrow devices? I am not sure how this positioning would be called and couldn't find a similar example.
I can't say too much without seeing your HTML or CSS. But I can provide an example of how this may work. I will assume your image will be large enough to fill a narrow device.
The CSS attribute and the terminology you would be looking for is "float" some documentation can be found here.
The HTML below is an example of an image with some text in it.
<p>
<img src="example.png" />
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed id pulvinar urna. Cras scelerisque finibus aliquam. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Ut nec interdum metus. Donec iaculis, nisl quis dignissim tincidunt, lorem nulla rhoncus turpis, eu fermentum ante orci a libero. Suspendisse varius nisl ut dui tincidunt, et feugiat eros egestas. Sed rhoncus viverra tellus ut pulvinar. Morbi id enim eget mauris bibendum tristique. Duis gravida ligula metus, quis porta massa mattis facilisis.
<p>
Now for some CSS.
/* This will align your image to the left within a p tag */
.image {
float: left;
margin: 10px;
}
/* To accommodate a smaller screen you will need a media query */
#media screen and (max-width: 640px) {
.image {
width: 100%;
margin: 10px auto;
}
}
I have made a quick JSfiddle with an example here. Hopefully I have provided you with enough to help you with your problem.
I'm trying to add swype support to my winjs applicaiton. It works great as long as the content has no scroll area. However as soon as the content overflows the page, the swype gestures don't work anymore.
If I set body { overflow-y: auto; } to hidden it works fine, but when there is a scrollbar, the swype gestures are not triggered anymore. Why?
Here is my sample:
<html>
<head>
<meta charset="utf-8" />
<title>PointerInput</title>
<link href="gestures.css" rel="stylesheet" />
<script src="js/jquery-1.11.1.min.js"></script>
<script src="js/jquery.touchSwipe.min.js"></script>
<link href="/css/ui-themed.css" rel="stylesheet" />
<link href="/css/default.css" rel="stylesheet" />
<style>
body {
overflow-y: auto;
}
</style>
<script>
$(function () {
$("body").swipe({
swipe: function (event, direction, distance, duration, fingerCount) {
$("#lastswype").text(direction);
}
});
});
</script>
</head>
<body>
<div class="TargetContainer" id="targetContainer">
<h1>Hello world</h1>
<h2>Last swype was: <span id="lastswype"></span></h2>
<div id="longText">
<p>
Lorem ipsum dolor sit amet, usu no illum petentium. Deterruisset definitiones reprehendunt eu mei. Cum falli volutpat at. Diceret splendide sed in. Sint noster deseruisse quo in. Vix nihil iisque dissentias at, harum facilis eu vel.
Pro eu dico decore consul. Ea posse incorrupte eam, ea affert verear dissentiunt eos. Mentitum intellegam at sit, stet repudiandae vel eu, sea error harum postea ut. Quod habeo tibique est an, vivendo salutatus pri an. Per tamquam tacimates in. Ut odio autem semper sed, cum ne quod aeterno praesent.
Ut oblique adversarium vis, et commodo discere eloquentiam eum, impetus tritani detracto ius ea. Ne sea expetenda deterruisset. Ea has populo ornatus feugait, assum voluptua interesset cum et. Mel ad aliquip periculis, ad eam vide altera splendide, alienum explicari ut mel. Qui laoreet tibique expetenda eu.
Mel nullam iuvaret ex, vix te tollit nullam. Illud quaestio reformidans cum et, et alia melius eam. Cum summo malorum ex. An decore nonumy appareat pro, alterum elaboraret concludaturque nec an. Ut vim aperiam oblique delectus. Duo et cibo labitur definiebas, ne affert rationibus sed, fuisset invenire vis ne.
Ad discere dolores accusata vix, aperiam vivendo cu vel, quo id nominavi dignissim temporibus. Nec summo democritum in, porro nominavi appareat nec cu. Similique ullamcorper vim ad, at epicuri phaedrum pri, est no alii facilis definitionem. Possim numquam et duo, quo an partem urbanitas, eu efficiendi scripserit vim. Ius putant dolores at, ut assum delenit definiebas sed. Tamquam fabellas sed at, ad ius quem affert nominavi, id elit oratio accusamus eam. Quando nostrum platonem vim ex, per ut tation nonumy assueverit.
Lorem ipsum dolor sit amet, usu no illum petentium. Deterruisset definitiones reprehendunt eu mei. Cum falli volutpat at. Diceret splendide sed in. Sint noster deseruisse quo in. Vix nihil iisque dissentias at, harum facilis eu vel.
Pro eu dico decore consul. Ea posse incorrupte eam, ea affert verear dissentiunt eos. Mentitum intellegam at sit, stet repudiandae vel eu, sea error harum postea ut. Quod habeo tibique est an, vivendo salutatus pri an. Per tamquam tacimates in. Ut odio autem semper sed, cum ne quod aeterno praesent.
Ut oblique adversarium vis, et commodo discere eloquentiam eum, impetus tritani detracto ius ea. Ne sea expetenda deterruisset. Ea has populo ornatus feugait, assum voluptua interesset cum et. Mel ad aliquip periculis, ad eam vide altera splendide, alienum explicari ut mel. Qui laoreet tibique expetenda eu.
Mel nullam iuvaret ex, vix te tollit nullam. Illud quaestio reformidans cum et, et alia melius eam. Cum summo malorum ex. An decore nonumy appareat pro, alterum elaboraret concludaturque nec an. Ut vim aperiam oblique delectus. Duo et cibo labitur definiebas, ne affert rationibus sed, fuisset invenire vis ne.
Ad discere dolores accusata vix, aperiam vivendo cu vel, quo id nominavi dignissim temporibus. Nec summo democritum in, porro nominavi appareat nec cu. Similique ullamcorper vim ad, at epicuri phaedrum pri, est no alii facilis definitionem. Possim numquam et duo, quo an partem urbanitas, eu efficiendi scripserit vim. Ius putant dolores at, ut assum delenit definiebas sed. Tamquam fabellas sed at, ad ius quem affert nominavi, id elit oratio accusamus eam. Quando nostrum platonem vim ex, per ut tation nonumy assueverit.
</p>
</div>
</div>
</body>
</html>
After some research, I found out that when an area has a scrolling area doesn't propergate touch events, if not specified otherwise. For eaxample, if an area has horzontal scrolling, I have to spezify, that vertical touch events are still allowed. This can be done with the touch-action property (http://msdn.microsoft.com/en-us/library/windows/apps/hh767313.aspx ).
This fixes my problem:
<style>
body {
overflow-y: auto;
touch-action: pan-x:
}
</style>
I use this jquery plugin for "see more" text:
http://keith-wood.name/moreRef.html
I save from tinymce editor html text into mysql database. Then I display html text in a php page, and I use "see more" text jquery plugin for show only 100 words of text.
My problem is that that plugin doesn't retain html formatted text, but it changes html formatted text into simple text, it remove html tag.
How can I retain text with html tag for display formatting text properly and have also "see more" link?
You can do it with pure css and keep the html in place. Here is a JS fiddle (less and more will not be displayed on IE6,7,8 and the content will be expanded)
http://jsfiddle.net/6sj4e/45/
.text {
height: 50px;
overflow: hidden;
height: 100%\9;
}
input.less-more,
input.less-more:checked ~ label.more,
input.less-more:not(:checked) ~ label.less {
display: none;
}
and the html :
<input id="chbox" class="less-more" type="checkbox">
<div class="text">Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum. Typi non habent claritatem insitam; est usus legentis in iis qui facit eorum claritatem. Investigationes demonstraverunt lectores legere me lius quod ii legunt saepius. Claritas est etiam processus dynamicus, qui sequitur mutationem consuetudium lectorum. Mirum est notare quam littera gothica, quam nunc putamus parum claram, anteposuerit litterarum formas humanitatis per seacula quarta decima et quinta decima. Eodem modo typi, qui nunc nobis videntur parum clari, fiant sollemnes in futurum.</div>
<label for="chbox" class='more'>more</label>
<label for="chbox" class='less'>less</label>
Given an HTML document in Nokogiri, I want to remove all <p> nodes with no actual text. This includes <p> elements with whitespace and/or <br/> tags. What's the most elegant way to do this?
This is a simpler fix: it removes both the whitespace and the br tags.
given the HTML
"<p> </p><p>Foo<p/><p><br/> <br> </p>"
Solution:
document.css('p').find_all.each do |p|
# Ruby on Rails Solution:
p.remove if p.content.blank?
# Ruby solution, as pointed out by Michael Hartl:
p.remove if p.content.strip.empty?
end
# document => <p>Foo</p>
I would start with a method like this one (feel free to monkeypatch Nokogiri::XML::Node if you want to)
def is_blank?(node)
(node.text? && node.content.strip == '') || (node.element? && node.name == 'br')
end
Then continue with another method that checks that all children are blank:
def all_children_are_blank?(node)
node.children.all?{|child| is_blank?(child) }
# Here you see the convenience of monkeypatching... sometimes.
end
And finally, get the document and
document.css('p').find_all{|p| all_children_are_blank?(p) }.each do |p|
p.remove
end
There's a more elegant way:
require "nokogiri"
doc = Nokogiri::HTML.parse <<-EOHTML
<div>
<p class="empty_p"></p>
<p class="full_p">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
</div>
EOHTML
# Magic happens here...
doc.at_css("p:first-child:empty").remove
puts doc.to_html