Using one views gsp in another views gsp - grails

So my question is, hopefully, very simple.
I have two different domains with corresponding controllers/view folders etc.
I want to call the second _form.gsp from the first _form.gsp (within a g:each as there will be multiple things to display) and pass in the relevant data for the second _form.gsp to render from, how do I do that?
I know I can use g:render template = "myotherForm" but I don't know how to point it to another view folder or how to pass in the details...
Thanks in advance and let me know if more information is required...

You can Refer this
Grails uses the convention of placing underscore before the name of a view to identify it as a template.
Example grails-app/views/book/_bookTemplate.gsp:
<div class="book" id="${book?.id}">
<div>Title: ${book?.title}</div>
<div>Author: ${book?.author?.name}</div>
</div>
Then use render tag
<g:render template="bookTemplate" model="[book: myBook]" />

You're right, <g:render is the right tool. By default, for template="myOtherTempalte" it looks for tempalte in current directory, but you could pass full path, if it's from another controller. Like template="/forms/myOtherTemplate". And use model="" to pass parameters, same as inside controller:
<g:each in="${things}" var="x">
<g:render template="/forms/myOtherTemplate" model="${thing: x}"/>
</g:each>

Related

Dereferecing in gsp inner html

I have a layout file. I want something like this in each of my pages inheriting that layout:
Just ${step} Steps Away From The Awesome!!
So in my layout I have defined a string as above with a placeholder step. I dont want to pass the value of this placeholder from controller. I wish to define it in the gsp that inherits this layout.
I was looking for something like <g:set var="step" value="1"/> (or 2 or 3 depending on the gsp). But it does not work if I define it like that.So how do I dereference the value of "step" inside each extending layout?
One of the best ways to accomplish this is to make use of content blocks and page properties. These are both features derived from Sitemesh.
I assume you only want to conditionally include this information when the page using the layout provides a value. So in my example here I have wrapped it in a quick if check.
In your layout:
<g:if test="${pageProperty(name: 'page.step')}">
Just <g:pageProperty name="page.step" /> Steps Away From The Awesome!!
</g:if>
Then in any page that uses the layout you can include the content for the variable step
<content tag="step">3</content>
Note that the value within the content tag can be whatever you like. It will be evaluated when the page is rendered.

Create custom GSP tag without writing Groovy code

Is it possible to create a custom GSP tag without writing Groovy code and embedding my HTML in the code (i.e. more of a JSP style way of creating a custom tag)?
I have a menu that consists of a bunch of items something like:
<li class="menu-item">
<g:link controller="someController" action="someAction" id="123">
My Item Text
</g:link>
</li>
I would like to create a new GSP tag to simplify my pages since it will be repeated multiple times. So, I'd like to create something like:
<my:menuitem controller="someController" action="someAction" id="123" text="My Item Text"/>
I know that I can create a custom taglib and create the tag using Groovy code. However I really don't like the idea of embedding HTML into a Groovy file. In the past I have created JSP taglibs in essentially a JSP file without writing Java code. So far looking at the documentation for Grails I haven't seen a similar style.
As a side note, can custom JSP tags be used within GSP?
You can do this with a template via the render tag, as explained in the "Views and Templates" section of the docs. Its worth noting you name the template file with a leading underscore but you refer to it in the render tag without the underscore.
The other alternative is to use a custom taglib as you described but to create your HTML with the Groovy MarkupBuilder. It takes a bit of getting used to (syntax is a bit strange) but once you've done a few times it becomes second nature.
The only way I can see to do what you want to do without a Taglib is to use g:render and pass in your values into the model attribute. Like this:
<g:render template="myTemplate" model="[controller: 'someController', action: 'someAction', id: 123, text: 'My Text Item']" />
Then in your actual template you will have the following:
<li class="menu-item">
<g:link controller="${controller}" action="${action}" id="${id}">
${text}
</g:link>
</li>

I need an explanation about how to use Grails (with different controllers)

I'm trying to create a little and basical Twitter-like site with Grails.
Here is the main part of my project arborescence (UserCwitter handles users, MessageCwitter handles messages and GroupCwitter handles groups like followers/followings) :
I'm trying to insert a text field to write a new message in the index (here it's index_final.gsp).
So I added this piece of code (in every controller my function to create a new user/message/groupe is called save()) :
<g:form action="save">
<fieldset class="form">
<g:render template="form"/>
</fieldset>
</g:form>
But I don't know why, the form that appears is the one to create a new user and not a message.
Why and what should I do ?
Thanks for your help. Sorry if this is something really easy or even stupid, I'm really new to Grails.
From render tag documentation, related to template attribute:
Note that if the value of the template attribute starts with a '/' it
will be resolved relative to the views directory. This is useful for
sharing templates between views. Without the leading '/' it will be
first be resolved relative to the current controller's view directory
then, failing that, the top level views directory.
So you should use
<g:render template="/messageCwitter/form"/>
if you want to render form template that is in messageCwitter folder.

How to loop through properties in a tab and display them using Razor?

I have a Document Type, that has a tab with some properties.
The properties are Upload types, and Simple Editor types.
(Users are supposed to upload images with some image text).
I have not grouped the "Upload" and "Simple Editor" properties, so how do i do this?
Next question,
I want to loop through each group (there should be 3 currently) and display them on my website.
The markup should look like the following:
<div>
<img src="PATH-TO-UPLOAD-TYPE" />
<div>"TEXT FROM SIMPLE EDTIOR TYPE"</div>
</div>
..
<div>
<img src="PATH-TO-UPLOAD-TYPE" />
<div>"TEXT FROM SIMPLE EDTIOR TYPE"</div>
</div>
...
I would like to use Razor for this. Thanks in advance!
For the first part, using the Razor model, you can't. The content object that you get on the front end only contains the properties, the tabs are not included, as they're only really for organising things in the back office.
You CAN get that information using the Umbraco API, but it's pretty database intensive and could potentially be quite slow if you have a lot of properties/tabs.
You'd be better grouping them yourself in your Razor Macro.
for the second part, you can acces the properties of a page via #Model.property. For example:
<div>
<div>#Model.simpleProperty</div>
</div>

I can't get the grails controller "render" method to work with an explicit template

I'm just getting started with grails, and I'm having an issue.
I have a "controller" and "view" for the projects home page (there's no model for the home page)
I called the view "index.gsp", and put it in a directory views/home
However, no matter what I do, grails is trying to read the page "home.gsp" (and then home.jsp), despite me having explicitly specified the index with the "template" attribute in the render call.
class HomeController {
String someparameter = "xyzzy"
def index = {
render(view:"home", template:"index") // I also tried "index.gsp" and "home/index.gsp"
}
}
I think I may be using the "template" attribute wrong, because I only see it used in examples for view-less template rendering. However the documentation gives no such limitation.
Is there any way to explicitly specify the name of a template? I just caved in and renamed it "home.gsp", but I'd like to understand what's going wrong.
(The home page in this application has no "model". Grails will use the controller has the model. In this example, you can access "someparameter" in the gsp template as ${someparameter}.)
I think you may be misunderstanding what a Grails template is. Think of a template as a reusable fragment. A template is a GSP that starts with an underscore like _menu.gsp that you would typically render from within another GSP with the a tag like <g:render template="menu"/>.
It doesn't make sense to render both a view and a template at the same time. They are mutually exclusive at this point.
Are you looking to implement a Layout? If so, see the docs or the grails.org explaination.
Basically, your view you would have a <meta name="layout" content="main"> tag in the <head/> tag of your view -- which indicates that the view will be meshed together with the main layout located in grails-app/views/layouts/main.gsp

Resources