Pass Variable from View to Template in Rails - css

I have a layout template with a view set like this:
layout
header
container
h2.page-title
content-box
yield
sidebar
and
view
section-title
content
What I'm trying to do is have the page title render in my layout. I'm doing this for stylistic reasons.
I could put the view specific title in the view file, then use CSS to offset it -100px or something. Can I pass a variable from the view to the layout?

What you are trying to achieve is usually done with the content_for helper. See here
It actually uses the title in the example, so you could just copy paste.
Edit: Sorry, I mixed the HTML page's title with the page's title you want, as you pointed out, so the example is not good for you without changing it. But it seems the guide helped you to figure out what you need.

If anyone has this issue, like #Rails Fan said, I used a content_for helper (not quite what was linked to though, which is why I have my own answer).
My layout view:
header
container
<%= yield :custom_section %>
content-box
yield
sidebar
My view:
<% if content_for :custom_section %>
<h1>Page Title</h1>
<% end %>

Related

How can I hide <div> in an HTML page in ASP.NET?

I have one div as the following:
<div>
<% # IIF(DataBinder.Eval (Container.DataItem,"Specifiction2").ToString()<> "","")
<div>
I want to hide the above div when "spcefication2" is blank on a .aspx page.
How can I do it?
A couple of things.
You are using IIF, but IIF should never be used. Always use IF instead. The only reason to use IIF is you are stuck with a pre-2008 compiler, and even then, you should use something else.
The easiest way to do what you are describing is via an id and runat="server", you can then either set the visible property (which will mean that the div never gets emitted) or set the style to display:none
If you want to do it inline, per your example, the code is
<div style="display:
<%# IF(string.isnullorwhitespace(DataBinder.Eval(Container.DataItem,"Specifiction2").ToString), "none", "block") %>">
</div>
You are using Eval and container.dataitem, you should look into using ItemType For your grid/repeats, then referencing the value as item.Specification
The above would be much easier if you declared a function in your code behind, taking a string and returning the string none/block.

Rails Simple Form Bootstrap Radio Buttons

I am using simple form and bootstrap with my rails 4 app.
I'm becoming so frustrated with this that I just can't figure out something that should be 'simple'.
I am trying to make my radio buttons display:block. Instead, the text is all inline and squished up on top of each other because the container is too narrow to fit it all in on one line.
My form element is:
<%= f.collection_radio_buttons :public, [[true, 'Yes, anyone can view this project'] ,[false, 'No, I would like to invite specific recipients']], :first, :last, {style:'display:block', class: "response-project"} %>
I have also tried:
Neither of these work to disable the formatting that either Simple Form or Bootstrap is imposing. I have other form elements that are radio buttons that I want to remain display:inline. I can see from my google inspect element, that there is a label.collection_radio_buttons tag on the form element. I didn't create that and I can't find it anywhere in the css files. it might be an import from bootstrap or a part of the simple form styling but I don't know how to disarm it from my form element.
Can anyone help?
Thank you
If you want your CSS to work you have to user input_html or label_html.
So it would be something like this:
<%= f.collection_radio_buttons :public, [[true, 'Yes, anyone can view this project'] ,[false, 'No, I would like to invite specific recipients']], :first, :last, :input_html => {style:'display:block', class: "response-project"} %>
*label_html is to affect only the label div
Anyway you should read the doc https://github.com/plataformatec/simple_form under the 'Usage' section. Hope this helps.

One partial view on multiple pages

I have a Partial View which shows on multiple pages. The problem I am facing after implementing it is, The position of the partial view is beiong shuffled on different views. Each of the views have their own css layout. So, should I change the css layout of all the views or is there any workaround for it...
Typically you would render shared partials like here:
One way to do it is to have two Layouts for your pages.
Have the default _Layout.cshtml and _LayoutWithSomething.cshtml and then in your Views determine which one to use
#{
Layout = "~/Views/Shared/_LayoutWithSomething.cshtml";
}
Another way to do this is to put RenderSection block in your _Layout.cshtml
<div class="main-content">
#RenderSection("submenu", false)
#RenderBody()
</div>
And then use #section in your views, note that this doesn't work inside partial views.
#section submenu
{
#Html.Partial("_MyPartial")
}
If your css completely changes the layout I'd split the css into two: layout aspect of styling and then the others like colors etc.

Rails - notice is visible if CSS property width or padding is set

There is usual <%=flash[:notice] %> in layoyt.
When I tried to add it some styling, I faced such trouble. For example:
text-align: center;
background: #fde073;
If no notice were provided with redirect, nothing on the page, as expected.
But if I add padding or height to CSS, it shows notice(no text, but background color) even if no notice were provided with redirect.
I can fix it with JS, but it seems quite ugly to me. Any other options?
I suggest skipping rendering of notice div when notice isn't present. Like this:
<% if flash[:notice] %>
<div class='notice'><%= flash[:notice] %></div>
<% end %>

Orchard - Getting the Content's Title from the Theme Layout

This is going to drive me crazy at this rate. From inside of a Layout.cshtml file for a theme in Orchard, how can I determine the Title of the main body's contents?
I've tried using the Shape Tracer but it doesn't seem to help. None of these give me any text at all.
#Html.Title()
#Model.Title
#Model.Content.Parts_Common_Body.ContentItem.TitlePart
#Model.ContentItem.Parts_Common_Body.ContentItem.TitlePart
UPDATE:
Here's the HTML which should show what the end result needs to look like to keep the theme intact, along with a picture showing the theme before I started with it. This HTML is just a snippet of the parts that I'm concerned with. In the picture, the search is what is in the ContentHeader zone.
<div id="wrapper-header-inner">
<div id="header-inner">
#Zone(Model.ContentHeader)
<h1 class="pagetitle">
Title Here
</h1>
</div><!-- #header-inner -->
</div><!-- #wrapper-header-inner -->
}
<div id="wrapper-content">
<div id="content">
#if(Model.Content != null) {
<div class="main" class="#mainContentClass">
#if(Model.Content != null && Model.LeftAside == null && Model.RightAside == null) {
<div id="maincontentFull" class="positionleft">
#Zone(Model.Content)
</div>
}
#* Other layout possabilities if left and/or right asides are present *#
</div>
}
</div>
</div>
As a follow up, updates to Orchard since I have asked this question included the ability to use the placement.info file to reroute certain parts to other zones, in effect letting me accomplish what I was looking for.
<Placement>
<Match ContentType="Page">
<Place Parts_Title="/TitleZone"/>
</Match>
</Placement>
From Layout, Model is the Layout object. It has nothing to do whatsoever with whatever content is going to get rendered into the Content zone, but it does have a Title property that should be set by that content. It is not exactly what you are asking for though: it is what will end-up being the HTML title.
If you want to get to the title of the item that gets rendered into the top-level Content zone, well, Layout is really not a good place to look for that. I would need to know more about what exactly you are trying to achieve but this seems backwards. In fact, you can't even assume that there is one such content item, or that there will be only one.
So what is it exactly that you are trying to do?
Does this work for you?
#Html.Title()
That should work if you're looking for the page title. However, if you're looking to directly access the TitlePart...
#Model.Title
This works because if you look in the Parts.Title template there is a line of code that does this...
#{
Layout.Title = Model.Title;
}

Resources