Grails: Get the View name in the Layout GSP - grails

With Grails, is it possible to get the name of the view that is being rendered while in the layout GSP file? I'd like to change one minor thing in the layout file depending on which view is going to be rendered.

If found this to be rather difficult to determine. It's easy if you use conventions, but you can always explicitly render any view you want. See the code in How to detect which view file has been rendered in grails for how to determine this in a filter.

The name of the view is normally the same as the action that has just been executed (e.g. show action normally renders the show.gsp view) - if this is the case in your view, then you can get it in your layout gsp using ${actionName}.

Related

What is layout indicates in view page of nopcommerce 3.90?

I want to know that what is layout in view page of nopcommerce which given the path of other view page. But if I remove that layout then also there is no change in nopcommerce.
Like in index.cshtml there is Layout = "~/Views/Shared/_ColumnsOne.cshtml";.
Now, my question is why this other cshtml path has given, and if I remove this line then why is there no change in nopcommerce?
The Layout property allows you to configure a "parent" view, the system renders the views from parent, in your case _Root.Head.cshtml -> _Root.cshtml -> _ColumnsOne.cshtml -> Index.cshtml, inside every layout cshtml you can find a #RenderBody() call where the child view is rendered.
When you remove the layout line inside Index.cshtml the system looks for the a default value and that value is configured inside the _ViewStart.cshtml, and this layout has configured the _ColumnsOne.cshtml, thats the reason that you cant see any changes.
The content of the _ViewStart.cshtml
#{
Layout = "~/Views/Shared/_ColumnsOne.cshtml";
}
Into nopcommerce layout means a master page.
That can be used into all pages as master layout.
If you remove that line than nopcommerce affect design layout.
If your design is not change than this page called as partial page, means call with in any other view page.

Given razor file, how do I differentiate whether it is a view or a partial view?

When I add a new ActionResult in controller like this
public ActionResult Step8(AddPropertyStep7ViewModel model)
{
return View();
}
I need to specify what kind of view I want to return.{View with layout, View , Partial View} I know that I want to return a file like this one below
This is the Step1.cshtml. How do I determine whether it is a view or a partial view of sth?
#using W.Resources
#model W.Models.ViewModels.Agent.AddPropertyStep1ViewModel
#{
Layout = "~/Areas/Agent/Views/Shared/_Layout.cshtml";
}
<div class="side site--dashboard"> ... <div>
#section scripts
{
<script> ... </script>
}
The basic way to define partial view is that it has Layout = null.
BUT
even if you have value in your Layout property, and you return the view like following from controller.
return PartialView("Your_view_name");
it will still get rendered as a partial view (it will not get wrapped in master page content).
As MVC works by Convention over configuration, the convention to define partial view is that the view name should start with "_" (_MyPartialView.cshtml)
As you mentioned in your edited question you need to specify which kind of view you want to return. i don't see need where you just need to return layout and nothing else. the solution i proposed above either to use return View() or return PartialView() from your action method will work perfectly and you can decide if you want to render page with master page or as partial view.
Views are views are views. The terms "layout" and "partial" just make it easier to talk about how a view is being used, but there's no functional difference between them. In other words, a layout is a view used as a layout. A partial is merely a view used as a partial. There's no way to just look at a view and know what type of view it is because it's entirely contextual.
That said, layouts are kind of an exception, since in order to truly function as a layout, they need #RenderBody() to be called somewhere. However, you could still use a view that didn't call this as a "layout" for another view. It just wouldn't actually render the view's HTML: all that would be returned is the layout's HTML.
There is a convention of prefixing layouts/partials with an underscore. However, that is merely a way to quickly see within the project tree that there's something special about a view file. It's not required and doesn't really give you any information about what exactly is special about the view.
That said, your question here isn't entirely clear. You say you want to specify what type of view is returned, presumably from your action. Well, you're already doing that, in effect. When you return View(), you're saying you want the view to be treated like a standard view. If you wanted to return a partial, you would have to return PartialView() instead. There's no way to specify that the returned view must utilize a layout, though.

Rails: Nested views or partials or..?

I new to Ruby on Rails, and I am coming from Drupal/PHP background.
I am trying to do something simple. A "Static" controller for some pages like about us, disclaimer etc. and a "Video" controller for videos.
The whole application has same look. I am trying to figure out how I can avoid duplicating views.
In drupal the theming goes like this:
html.tpl.php (renders page.tpl.php)
- page.tpl.php (renders multiple block.tpl.php)
- block.tpl.php (renders more nested tpls or some html generated by theme functions).
I find this to be great.
In rails, what I read is there is application.html.erb (Which I think is similar to html.tpl.php of Drupal, as it normally contains stylesheets, js, etc. available throughout the application) called "Layout". Then there are "Views", which "yield" content in this layout. We specify the view in controller, or it gets one automatically. I think of a view as "page.tpl.php" of Drupal.
Most of my site uses same structure i.e. header, top menu, content, right sidebar, footer. But content of each area changes depending on the path. So I am thinking of doing it like this:
Both 'Static' & 'Video' will call the same view, with different variable values i.e. values for content area and sidebar.
This View will the use 'render' to render another view, with a subset of variables passed from controller i.e. it will render content & sidebar views.
If needed, those views will render more views, and so on.
Another one I read about is partials. I can't seem to differentiate between rendering a view inside another view, and rendering a partial.
Which method is better? Or is there a better "Rails Way"?
This is quite a large topic to cover but edited highlights are
If you want to repeat content on every page, like a header or navbar for example then put it in layouts/application.html.erb
if you want to keep your views tidy with content specific to that page then you can use partials. Normally in views I create a folder called shared and then put my partials in there. for example if i save a partial called _form.html.erb (make sure you name partials with the underscore at the beginning of the file name), i would then call that in my view like
<%= render 'shared/form' %>
you can also use partials in the layouts/application file to keep that clean aswell
This is just my way of doing it, I'm sure more experienced rails guys have better ways
Hope that helps, any more questions then just ask

The following sections have been defined but have not been rendered for the layout page "~/Views/Shared/_Layout.cshtml"

I know there are a few questions that have been answered but I didn't find something specific to my case.
I'm using the mobile capabilities of MVC4. So I created a _layout.mobile.cshtml and the corresponding views.
The error above happens when I go in with a mobile device. As you can see, it is trying to display the regular _layout.cshtml instead of the _layout.mobile.cshtml. So I'm assuming it is also trying to display the view (say Index.mobile.cshtm) which doesn't have the section in question. Basically it is mixing the regular layout with the mobile views.
This doesn't happen all the time. If I recycle the pool it works again for a while and then all of the sudden it goes back to having the error and it will continue until I recycle the pool again.
Has anyone seen this problem before that can shed some light?
Thanks
John
In the _ViewStart.cshtml available under the Views folder, change the Layout value to your custom layout. I think this may help.. (Make sure that you are returning View instead of partial view)
for example
#{
Layout = "~/Views/Shared/_layout.mobile.cshtml";
}
In case if you want to change the layout for a specific page you can explicitly define it at the top of the page as a page directive.
in the index.cshtml there is a section being called defined in the original layout file "_LayoutHome.cshtml" that is not defined in the new bootstrap layout.
specifically: #RenderSection("featured", required: false)
So the solution is to either add this section to the new layout (look for it in the original layout and paste it) or simply delete it from the index.cshtml.
I had also face the same problem I removed
#section featured {
From View
Another way to do this is to use a conditional block in your _ViewStart.cshtml page. For example, you may have two layouts depending on the device regular user. Using pseudo-code for the reading of the device/browser type bit, it would look something like this:
#{
if(userIsMobile)
{
Layout = "~/Views/Shared/_MobileLayout.cshtml";
}
else
{
Layout = "~/Views/Shared/_Layout.cshtml";
}
}
I have used this to display or hide sections or menu items as needed for different classes of user; it should work as well for device-specific layouts.
Joey Morgan

Rendering controller/action within a different view

I am looking for a way in Ruby on Rails to render a completely different controller's action within other views.
For example I am writing a band website that displays their albums. So I have /albums/list that shows a list of all of their albums with other stuff around the page as the layout. Now what I am looking to do is also render /news/list in every page as part of the layout as well so that every page you go to you can see it.
I cannot find a way to call the news controller list action and display it in the list view using partial views. Any help would be appreciated.
http://api.rubyonrails.org/classes/ActionView/Helpers/RenderingHelper.html#method-i-render
render() method give you ability to render specific file, placed in any directory :)
also read this article - http://blog.plataformatec.com.br/2012/01/my-five-favorite-hidden-features-in-rails-3-2 especially about "Custom partial paths".

Resources