MVC, make conditional output in simplest way - asp.net-mvc

hi
i have a variable called isEnglish
if it is true I want to output something like this:
<div orientation="left"> </div>
otherwise:
<div orientation="right"> </div>
the following code failed to compile :
<div orientation="<%=isEnglish?? %>left<%:%>right<% %>"> </div>
I know a way which is long, by using the (if) and Writer.Write method
is there another simple way ?

<div orientation="<%= isEnglish? "left" : "right" %>"> </div>

You could use a conditional statement:
<div orientation="<%= isEnglish ? "left" : "right" %>"></div>
Or, preferably (to me at least), you would remove this logic from the View altogether and create a ViewModel. You can then put the logic in the mapping between the Model and the ViewModel.
That way you don't have spaghetti code in your View. It might look something like:
<div orientation="<%= Model.Orientation %>"></div>

The code you want is this:
<div class="<%= isEnglish ? "left" : "right" %>"></div>
But check out Razor if you're using MVC, much cleaner syntax.

Related

ERB String interpolation generating incorrect HTML syntax

I have these lines in a view:
<%
if current_user.is_fav?(a)
cls = "product fav"
else
cls = "product"
end
%>
and later
<div class=<%= cls %> >
This produces:
<div class="product" fav>
EDIT: as the answer says, it did NOT produce this, it was the browser "inspect" that showed it to me.
I can work my around it, but why doesn't the above code work?
This produces:
<div class="product" fav>
No, it doesn't. It produces <div class=product fav>. What you see (via something like "inspect element", correct?) is browser trying to interpret your broken markup as close to html spec as it can.
As noted in other answers, what you should do instead is something like this:
<div class="<%= cls %>">
<div class=<%= cls %> >
should probably be
<div class="<%= cls %>">
You're substituting a string into an erb placeholder, but the underlying template does not indicate that the placeholder itself is a string. ERB is likely making a best effort attempt to do a correction but it's oviously not able to cope. Edit: Sergio pointed out this is likely the browser doing this, not ERB.

MVC - Best way to add outer element

I have been developing with MVC for a few years and this is a nagging issue I have encountered several times. I do not like any of the ways I have handled this in the past so I thought I would ask here.
Let's say I have a series of nested DIVs on my view:
<div id="outer">
<div id="inner1">
<div id="inner2">
</div>
</div>
</div>
At runtime I want to add another element, a DIV or anchor, inside of the outer div but have it contain the inner DIVs.
<div id="outer">
<div id="newone">
<div id="inner1">
<div id="inner2">
</div>
</div>
</div>
</div>
How would you recommend handling this?
I imagine this would have more to do with JavaScript than with the server-side code. And since ASP.NET MVC comes with jQuery, you may as well make use of the wrap() function. Something like this:
$('#inner1').wrap('<div id="newone"></div>');

how to handle inline c# code to show div tag

I would like to show div tag based on TempData["flag"], which comes from controller.
Code 1:
<div class="error_text">
<div id="loginError" style="visibility:" +#TempData["flag"] class="errortext">
incorrect data
</div>
</div>
Code 2:
I tried with if condition as below. Its working, but I would like to go with the above code.
#if ((bool)TempData["flag"])
{
<div class="error_text">
<div id="loginError" class="errortext">
incorrect data
</div>
</div>
}
How can TempData be handled in the div tag (code 1)
You can integrate the razor code straight into the html attribute.
<div class="error_text">
<div id="loginError" style="visibility:#(((bool)TempData["flag"]) ? "visible" : "hidden")" class="errortext">
incorrect data
</div>
</div>
EDIT I just noticed based on your second code section that the flag is just a bool value in your program, so I updated the code sample above to account for this.

Can “field-with-errors” be attached to the parent of the input tag that raises the error?

So I have an input element like this. The wrapping element is about, you know, a visual thing.
<div class="input-wrap">
<input class="blah-blah" />
</div>
When the <input> contains the error, it'll be like this:
<div class="input-wrap">
<div class="field-with-errors">
<input class="blah-blah" />
</div>
</div>
But what I want to do is:
<div class="input-wrap field-with-errors">
<input class="blah-blah" />
</div>
I found this page, it's very close to my question
Rails 3: "field-with-errors" wrapper changes the page appearance. How to avoid this?
Now I know I can throw
config.action_view.field_error_proc = Proc.new { |html_tag, instance|
"#{html_tag}".html_safe
}
to avoid making a wrapping tag around the <input> tag that has an error on. But what I really wanna do is, again, adding "field-with-errors" class on the direct parent of the <input> tag. Can I do that? Does ActionView hold the tree structure of DOM Nodes?
You can put the code for handling errors wherever you like, just call it as a block on the instance variable, for example
if #instance.errors.any?
<div class="field with errors">
#instance.errors.full_messages.each do |msg|
<p><%= msg %></p>
end
end
If you user this a lot, it's good to pull it out into a helper and pass in the instance variable as a parameter.

Is there a way to do a rails "yield/content_for" in GRAILS?

In rails, I'm used to use the yield/content_for to site mesh. (http://guides.rubyonrails.org/layouts_and_rendering.html#using-content_for)
I can't find in Grails documentation a way to do this... Could you help me?
EDIT
Here is the situation:
I have a layout containing this:
<body>
<div id="wrapper">
<div id="container">
<div id="header">
<g:render template="/layouts/header"/>
</div>
<g:render template="/layouts/menu"/>
<div id="container-homepage">
<g:layoutBody/>
<div id="subfooter">
<div id="content">
<div id="left">
<div id="logo_sub_header"></div>
</div>
<div id="right"></div>
</div>
</div>
</div>
</div>
</div>
</body>
And I want to be able to add a html snippet (search tool bar for example) juste above the container-homepage div. A partial could do the trick.. if this search tool bar was always the same. The thing here is that this search bar depends on the page i'm visiting.
I could also just change the position of the container-homepage div to put it directly into the view, and not the layout, but then i'll have to to it in ALL the views, and that's not DRY.
Any ideas?
Regards,
I think you have two solutions:
the g:render tag is the best option if your content block will not change based on a custom page.
Anyway I would take a look ah this link
http://grails.org/Content+Blocks
because g:pageProperty it is the most elegant and flexible solution.
Perhaps the g:render tag is what you're looking for? It allows you to render a template anywhere within your view (including in other templates). You can also pass in a model for the template to use.
Note the section at the bottom of that page on naming conventions -- template view gsp filenames should begin with an underscore (though that underscore is not supplied in the render tag). There's mos def a way to override this, but things work automagically if you put the underscore there.

Resources