I wanted to know the command which is exactly equal to php require in my GSP (Grails server pages) or in Groovy.
I know I can use <g:include/> but wanted to know is there any command which will fulfill php require in groovy/grails?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
An exact equivalent really depends on the context of what
require()is being used for in the PHP script.PHP and Servlet environments operate differently. Using
require()in PHP simply locates another PHP script and executes it. That imperative-style operation doesn’t apply as well to the more object-oriented Java/Grails/Servlets.There are a couple of possible equivalents, depending on what you’re trying to accomplish:
<g:include/>e.g.
What this will do is invoke a different controller/action and insert the response into the current page. This would be similar to PHP if your
require()was rendering some markup.View templates:
If you’re just trying to include common pieces of markup in several pages, these might be what you’re looking for. You can create template views and use
<g:render/>to include them in your GSPs. I suspect this is what you’re after, but see my “Update” below for some advice about this.@page importe.g.
This will make
MyClassavailable to the GSP, which would be similar torequire()if the require was specifying some library classes or functions to be used in other PHP scripts. However, using this pretty much screams code smell, since almost anything you’d be using this for would be more appropriate in a controller action or a service.Update:
Seeing your other question, I’ll venture you’re simply trying to include a common piece of GSP/HTML in several different views, which kind of goes against what Grails provides for you with its layouts and templates.
If you’re trying to “require”, say, “blog-header.php” in all of your GSPs, you’d more likely want to just include the contents of the header within a layout, e.g.
grails-app/views/layouts/main.gsp, and then use that layout in the views that require the header.