Get erb view from inside layout - ruby

I am trying to get the name of the current view being loaded into layout.erb. I am trying to inject a js file and a css file into the layout head. I am building the name for those files based on the name of the erb view:
<link rel="stylesheet" href="/devcss/<%= viewName%>.css">
<script src="/devjs/<%= viewName %>.js" type="text/javascript"></script>
How can I do this from the layout template?
UPDATE:I am not interested in knowing how to inject the script in a specific place in layout, which I think content_for is used for. What I need to knok in the example above is how to determine the viewName variable

Insert the following into application.html.erb after inside head tag
<%= "#{yield(:scripts)}".html_safe if content_for?(:scripts) %>
and in the individual erb, insert similar to the following
<% content_for :scripts do %>
<link rel="stylesheet" href="/devcss/<%= viewName%>.css">
<script src="/devjs/<%= viewName %>.js" type="text/javascript"></script>
<% end %>
This will allow you to insert page specific codes into head and other tags not accessible to your individual erbs

Related

Sinatra doesn't load assets

I have a sinatra landing page. Server starts by execution index.rb in root folder.
For example:
get '/en' do
I18n.locale = 'en'
erb :index, locals: {langswitch: "/ru", current_locale: "en"}
end
renders index.erb and works well.
This page has:
<link rel="stylesheet" type="text/css" href="index.css">
And if I open this link, it points to http://0.0.0.0:9292/index.css, retrieves css file and renders page as expected.
However, there is also
get '/agreement/en' do
I18n.locale = 'en'
erb :agreement, locals: {langswitch: "/ru", current_locale: "en"}
end
If I visit the same link from this page, it instead leads to http://0.0.0.0:9292/agreement/bootstrap.css, instead of http://0.0.0.0:9292/index.css and fails to retrieve file.
Same with all of my images, other css files.
First you need to put all your assets in public folder. Then you need to modifie
<link rel="stylesheet" type="text/css" href="index.css">
to
<link rel="stylesheet" type="text/css" href="/index.css">.
So all you need to do is to put / before index.css

RenderSection and EditorForModel in asp.net MVC

This is my _Layout.cshtml
<html>
<head>
#RenderSection("Script", false)
</head>
...
</html>
This is a simple edit page edit.cshtml
#model Shop.Models.Product
#{
Layout = "~/Views/Shared/_Layout.cshtml";
}
#Html.EditorForModel()
and this is ~/Views/Shared/EditorTemplates/Product.cshtml
#model Shop.Models.Product
#section Script {
<script>alert("hello");</script>
}
...
#section Script{...} does not work in Product.cshtml because of EditorForModel. How can do it?
Sections work only in views, not in partials. An editor template is a special kind of partial. It's bad practice to put javascript in partials anyway, so I would simply declare the section in the Edit.cshtml view.
But if you very much insist on putting your scripts in the middle of your markup, since Razor doesn't support sections in partials, you could implement custom helpers to achieve that.

writing strings inside head-element in rails

I'm working with a site using client-side templates through knockout.js.
The backend api, and login, is written in rails.
What I want to do is have each client-side html template in a separate file, and then have those templates lifted into the page using the templates. Similar to how javascript files are lifted in.
So I have a directory in my app/assets called templates
Each template in the directory should be added to the page in a script tag with the type="text/html"
I've gotten so far as to product the actual templates content now I just want to put it in the html.erb file in the head property.
However it always lands in the Body as normal text, not as HTML.
I've defined the following method in my controller:
def html_templates
output = ''
templates = Dir.glob 'app/assets/templates/*'
templates.each { |template|
file = File.open(template, "rb")
output += '<script type="text/html" id="'+(File.basename template, '.html')+'">'
output += file.read
output += '</script>'
}
return output
end
I try to add it to the .erb layout file like so:
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<%= html_templates %>
<%= stylesheet_link_tag "application" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
</head>
Yet the output is always put in the body, with all my html escaped.
Also, if anyone has better solutions to have to solve this. Please, recommend.
Thanks :) so basically you should use raw method whenever you don't want output to be escaped

asp.net mvc 3, add <meta /> to <header> from any part of application

My question is: How can I add meta tag from my view or partial view? Basically I want to, from the view, write a meta tag, and I want it to be displayed in the header. Is it possible?
Thank you
H
You can use sections to do this.
In you layout page you need to define the section:
<head>
#RenderSection("Head", required: false)
</head>
This section can then be used in any page that uses this layout page:
#section Head {
<meta ... >
}

meta tag in ASP.NET MVC 3

How can I put meta tag to work only for one page. If I want to put it .aspx file, where is right place.
Thanks.
Since you haven't said yet, I'm assuming you're using the Razor engine (the "default" for new MVC3 projects). In that case, you just need to insert a new section into your layout view, and only render that section if you need to insert a meta tag.
For example, working from the stock New ASP.NET MVC 3 Project template, you would edit your Views\Shared\_Layout.cshtml file, and before the closing </head> tag, do something like this:
#this.RenderSection("MetaContent", false)
</head>
Then, in any of your views that you needed to, add this:
#section MetaContent
{
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8" />
}
If you're still using the ASPX layout engine for some reason, you can accomplish the same thing using the <asp:ContentPlaceHolder> tags in your master page and <asp:Content> tags in your views.
EDIT:
Since you're using the ASP.NET Forms layout engine still, here's the same basic idea as above in aspx syntax:
In your master page, you add the tag:
<asp:ContentPlaceHolder ID="MetaContent" runat="server" />
</head>
And in your .aspx views, you add a new content section (you should already have at least two -- a title and a body):
<asp:Content ID="Meta" ContentPlaceHolderID="MetaContent" runat="server">
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8" />
</asp:Content>
I was going to suggest exactly what Michael said (+1). Another option would be to put a boolean in the ViewBag, something like:
ViewBag.ForceIE8Mode = true;
For pages that you want to force into IE8 Mode.
and then in your view, wrap the meta tag in a conditional. either
#if(ViewBag.ForceIE8Mode == true) {
<meta... />
}
or
<% if(ViewBag.ForceIE8Mode == true) { %>
<meta... />
<% } %>

Resources