Passing objects to be used by partials inside a layout - asp.net-mvc

I have a page layout with a sidebar and a main content. Both are dynamic according to the user profile.
<html>
<body>
<%- partial('partials/sidebar')%>
<html with <% ejs code %> for body>
</body>
</html>
Sails view should be:
res.view( {mydata} );
How can I pass data to the partials so it can be dynamically rendered instead of static?
Example:
controller:
res.view( {mainContent:{mainContentJson}, sidebar:{sidebarJson} );
layout.ejs:
<html>
<body>
<%- partial('partials/sidebar', sidebar)%>
<html with ejs code for body>
</body>
</html>

Yes. Partials accept arguments to be passed, and they are rendered before being inserted into the calling layout.
controller:
res.view( {
layout:<yourCustomLayoutIfNeeded>,
mainContent: <mainContentObject>,
partialContent: <partialContentObject>
});
yourCustomLayoutIfNeeded:
<%- partial(<pathToPartial>, partialContent) %>
<mainContent>

Related

Rails insert automatically my code into body tag not into head tag

I did it in application.html.erb
<html>
<head>
<%if #hasAdsense == true%>
<script data-ad-client="ca-pub-64xxxxxxxxxxx" async
src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<%end%>
</head>
<body>
<%= yield %>
</body>
</html>
but when I see a page with chrome developer tools ,it's automtically inserted in body not in head.
so html code of page where #hasAdsense==true looks like this.
<html>
<head>
</head>
<body>
<script data-ad-client="ca-pub-64xxxxxxxxxxx"
async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
</body>
</html>
I don't know why this happens, but google bot inspect Adsense script in head tag,
so I think this can cause problem so that I can't use Adsense script in my website.
is there a way let rails not insert automatically adsense script into body tag?
do you have
<%if #hasAdsense == true%>
<script data-ad-client="ca-pub-64xxxxxxxxxxx" async
src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<%end%>
in any other views, because it might come from the yield.
We probs need a little more info here, is #hasAdsense a boolean value (can you confirm by showing your controller or checking on the page directly)? does this happen with other content? so if you made a if statement with <title>Hello World</title> does that render in the body as well?

How to conditionally add class to <body> in rails layout

My application.html.erb file looks like this
<!DOCTYPE html>
<html lang="en">
<head>
.
.
.
</head>
<body <%= "class=landing-bg" if !#landing.nil? %>>
<%= render 'layouts/header' %>
<div class="container">
.
.
.
</div>
<%= render 'layouts/footer' %>
<% if !#landing.nil? %>
<div class="landing-gradient"></div>
<% end %>
</body>
</html>
I've left out a bunch of meta tags in the <head> and a bunch of other code in the body that's unrelated to the question.
static_pages_controller
def home
#landing = "landing"
end
I need to add a class to <body> and a div below the footer only on my home page. I feel like what I am doing is a bit hacky. And, if I created a separate layout just for the landing page it wouldn't be DRY since I would be repeating alot of code that's on this layout.
What is the best way to do this? What is the rails way?
My typical pattern is to add the controller and action into the class of the body, and then use those as selectors in my css like this:
<body class="<%= controller_name %> <%= action_name %>">
<!-- etc -->
</body>
As for your second issue of a conditionally body on only the home page, you can simply add that into the body in the main layout and display it only on the landing page:
<body class="<%= controller_name %> <%= action_name %>">
<!-- etc -->
<div class="landing-gradient"></div>
</body>
# application.css
.landing-gradient {
display: none; // Hide the gradient
}
.landing .landing-gradient {
display: inline; // Show gradient b/c of more specific selector
}
This is my personal preference since you're leaving display of individual elements to the css / presentation layer instead of within your controllers.
Finally, you could also use a content_for around the footer so that you can customize the footer across individual pages. That's a bit heavy handed for your example though, so I'd only use that if you were customizing the footer a lot.
In my case I want to have a different class per layout (at least potentially), so in app/views/layouts/application.html.erb I do this:
<body class="<%= yield(:body_class) %>">
and then in my layout file I do this:
<% content_for(:body_class) { 'my-class' } %>
You can also manage it without creating an instance variable and verifying it in your view but using the params[:action] and params[:controller]:
<body class="<% 'class_value' if params[:controller] == 'static_pages' && params[:action] == 'home' %>"
You can just validate for the action (method) if you don't have more than one home method.

Why is my view file moving all my head content into the body section?

I'm very confused by how the application.html.erb file and the view files combine. I have in my application.html.erb file the following (minimized the content for brevity):
<!DOCTYPE html>
<html>
<head>
<!-- Bootstrap Css -->
<link href="/bootstrap-assets/css/bootstrap.css" rel="stylesheet">
<%= yield :head %>
</head>
<body>
<nav>
<!--Long navbar section -->
</nav>
<%= yield %>
</body>
</html>
In my view file:
<html>
<head>
<!-- View Specific Stylesheet -->
<link href="/css/ViewSpecific.css" rel="stylesheet" />
</head>
<body>
<!-- View Specific Body -->
</body>
What I would expect is that when these 2 files combine, the View specific stylesheet load in the <head> section, and the View Specific Body load in the <body> section. The final result actually sends the view specific stylesheet to the <body> section, after the navbar from application.html.erb is already loaded. This obviously produces the unwanted result that part of my body is loading before the stylsheet is loaded, and the first second the user views the page everything looks terrible.
You should make use of content_for if you want to render the view-specific head into the head section of application.html.erb. Use
<% content_for :head do %>
<!-- View specific head section -->
<link href="/css/ViewSpecific.css" rel="stylesheet" />
<% end %>
Since you yield :head in the application.html.erb, thats all to be done. And you don't need html tags in the views.
For more, see http://apidock.com/rails/v4.2.1/ActionView/Helpers/CaptureHelper/content_for

About some tags in rails program

i have got this program in ruby editor.My output comes out to be
<html>
<head><title> Ruby on Rails tutorial Sample App | <%= #title %></title>
<%= csrf_meta_tag %>
</head>
<body>
<%= yield %>
</body>
</html>
what is the error here and what is yield and csrf_meta_tag?
You don't seem to be using a server to render the views, it seems like you are rather loading the html directly on your browser.
Maybe the following link will help you get started:
http://guides.rubyonrails.org/getting_started.html
you need to rename your file from:
application.html to be application.html.erb so that it is going to interpret your embedded ruby commands.
Ref yield & content_for
Within the context of a layout, yield identifies a section where content from the view should be inserted. The simplest way to use this is to have a single yield, into which the entire contents of the view currently being rendered is inserted:
<html>
<head>
</head>
<body>
<%= yield %>
</body>
</html>
You can also create a layout with multiple yielding regions:
<html>
<head>
<%= yield :head %>
</head>
<body>
<%= yield %>
</body>
</html>
The main body of the view will always render into the unnamed yield. To render content into a named yield, you use the content_for method.

Grail partial view for ajax request

<g:if test="${!request?.xhr}">
<!doctype html>
<html>
<head>
<meta name="layout" content="home">
</head>
<body>
<div class="row-fluid">
</g:if>
AJAX
<g:if test="${!request?.xhr}">
</div>
</body>
</html>
</g:if>
I get error: Grails tag [sitemesh:captureBody] was not closed.
In Config.groovy I set grails.views.gsp.sitemesh.preprocess = false but this doesn't help.
What way to use partial view with if statement.
A better way to handle this in grails is to wrap a template containing the main content. For example:
//_body.gsp
AJAX
//view.gsp
<!doctype html>
<html>
<head>
<meta name="layout" content="home">
</head>
<body>
<div class="row-fluid">
<g:render template="body">
</div>
</body>
</html>
Then your controller can render the whole view on a regular request, or just the body on AJAX.
You can check request.xhr in the controller, and determine to render a template or a string based on the results of that if statement.

Resources