Is there a way to do a rails "yield/content_for" in GRAILS? - 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.

Related

How to avoid empty <div> tags from th:if in Thymeleaf?

<div th:if="${user.admin}">
Welcome, Admin
</div>
Above code leaves an empty tag in the document if the user is admin. These empty divs creating alignment problems, how to avoid this?
You can achieve this by using th:block instead of <div> for conditional display.
<th:block th:if="${user.admin}">
Welcome, Admin
</th:block>
courtesy : https://anshulgnit.blogspot.com/2019/10/th-block-tag-in-thymeleaf.html

How to divide a Certain Model into two parts in Razor View?

I am using Articulate plugin in Umbraco for Blog Style and I need to show something default inside every Articulate Post.
Here is what is happening now.
<section class="post-content">
#Model.Body
</section>
And I need to do something below
<section class="post-content">
#Model.Body.part1
"Something Very Important"
#Model.Body.part2
</section>
Thanks in advance.
Create two partial views and pass the same model to both. On each view just render the part you want.
<section class="post-content">
#* the model will be passed to the partial view *#
#Html.Partial("~/Views/Partial/PartialView1.cshtml");
<p>Something very important here</p>
#Html.Partial("~/Views/Partial/PartialView2.cshtml");
</section>
Then your partial views would be something like:
PartialView1.cshtml
<div>
#Model.Body.part1
</div>
PartialView2.cshtml
<div>
#Model.Body.part2
</div>
You don't really need the <div> but you get the point, right?

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>');

MVC4 have a view working as a layout/masterpage

As Im building sort of a window/tool setup in MVC4 Im wondering is it possible to do like a #Html.Partial("_PartialToolWindow") In a page including all the tools to be loaded.
And then have this _PartialToolWindow load a defalut html template that should surround this code that the _PartialToolWindow contains?
For example...
<div class='boarder'>
<div class='innerBorder'>
--HERE IS THE SPECIFIC CONTENS--
</div>
</div>
This _PartialToolWindow should have loaded this border and innerBorder to surrond its own HTML.
You can use a section in your layout.
_Layout.cshtml
<div class='boarder'>
<div class='innerBorder'>
#RenderSection("PartialToolWindow")
</div>
</div>
Then you will be able to load specific content in your views:
#section PartialToolWindow
{
<span>content</span>
}

Filling Sidebar Implementation with Rails and Bootstrap

This is mostly an implementation question for a webapp I'm trying to develop. I'm relatively inexperienced so I want to check if my idea for implementation is decent or if there's a much easier way.
The Problem
I want to create a scrollable sidebar that automatically fills with small containers that hold a user profile photo and their name. The basic idea is that a user is a member of a class and the sidebar should hold all of their classmates. The sidebar should be a fixed size and the user should be able scroll down through their classmates and click on a user if they want visit their page.
My Ideas For Implementing
It seems to me that I will need to use some embedded ruby to direct the filling process. Some psuedocode would be something along the lines of "For every user in this class, create a container with their picture and name". I haven't given a lot of thought as to actually handle this step but I'm mostly concerned with the actual html structure.
My idea is to do something like this:
<div class="container-fluid">
<div class="row-fluid">
<div class="span2">
<!-- for each user in class -->
<div class="container-fluid">
<div class="row-fluid">
<div class="span">
<!-- Load appropriate user data -->
</div> where appropriate...
Is this the "proper" way to go about implenting this? I haven't been able to find much information on the idea and visit an example site's source code isn't all that helpful as the sidebar is already filled...
I would separate your sidebar HTML and logic into a partial, E.g. "app/views/shared/_sidebar.html.erb". From there on, you can accept a collection of data from whatever view you're rendering.
View file
<div class="container-fluid">
<div class="row-fluid">
<%= render "shared/sidebar", collection: #users %>
</div>
</div>
Sidebar partial
<div class="span2">
.....
<% #users.classmates.each do |e| %>
<%= e.name %>
<% end %>
.....
</div>

Resources