How to pull in html from external url in GSP page - grails

I'm fairly new to Grails and can't seem to find a solution this problem.
I'm looking to pull in html from an external url and have it output on a GSP page. Something like this (knowing this doesn't actually work):
<g:render template="http://somesite.com/wp-admin/admin-ajax.php?action=include_banner" />
The reason I want to pull this html in is because we have duplicating html between our grails app and our wordpress instance.
Any help would be appreciated.

There can be two solution to this:
1) You can use iframe or object tag to render the html from external URL.
Example:
<object width="600px" height="600px" data="url">
</object>
or <iframe name="inlineframe" src="url" ></iframe>
2) Second, we can use the grails way:
You can define a custom tag which takes url as input
def myOwnRender={attrs,body->
out<<"""<div id="inline_div"></div><script type='text/javascript'>$.post("${attrs['url']}",function(result){
$("div#inline_div").html(result);
});</script>"""
}
First one seems to me a easier solution.
Hope that helps!!!
Thanks

Related

Is it possible to get the html produced by a script

I have a URL from a third party site which should be the source of a script tag, something like this:
<script src="<%= #url %>"></script>
The above code shows a webform.
I would like to get the html code resulted by that code and store into a variable, something like this :
html_code = get_html('<script src="<%= #url %>"></script>')
Is that possible using Ruby/Rails (maybe using nokogiri) ?
If you're using jQuery in the browser and that code shows up in a well defined location, for example a <div> with a specific id then you can just grab it using your browser's JavaScript console:
$('#the_id').html()
That will show the raw HTML of that element presuming it dumps its content in an element with the ID the_id. You can use whatever CSS selector works.
Where that HTML goes in your document is impossible to tell from your example. A <script> can add any elements it wants anywhere in your document. You'll have to look around to see where it goes.
Load the page into a browser and then do View/Source in the browser. This will let you view the generated web page and you can find what URL was put into the <script> tag.

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>

Rails: Interpolation, Attribute into iFrame Link

I have an video_link attribute.
When a User creates a Listing he can enter a Code (e.g. aabbcc).
I want to render this code inside my iframe link, like so
<iframe src="https://mywebsite.com/aabbcc/"></iframe>
So i tried
<iframe src="https://mywebsite.com/#{#vine.video_link}/"></iframe>
But this didn't work,
What am i doing wrong ?
I'm Getting no Error, but if i inspect the iframe on Page, the Interpolation is not rendering. im seeing just #{#vine.video_link}
Thank you
Oh! I get it... change it to:
<iframe src="https://mywebsite.com/<%= #vine.video_link %>/"></iframe>
You only need to do interp. like that if it's inside ruby code. In this case you're not escaping a string in ruby, you're just putting it in HTML (view).
Example.
If you were in a controller, let's say and had a string that looked like
variable = "<iframe src='https://mywebsite.com/#{#vine.video_link}/'></iframe>"
Edit to correct bad copy/paste job.

Creating a controller / action href

While Grails has several tags to generate either resource links or anchor tags, what about a plain href? Say I'm coding some AJAX functions on my own and I want to say something like
$.post("<g:href controller='mycontroller' action='update' />');
I think createLink is what you're looking for:
<a href="${createLink(controller:'mycontroller', action:'update')}" />

grails portlet pagination on Liferay

How to implement pagination in grails portlet on Liferay.
The basic pagination was not working on Liferay. When clicked on next the page is jumping from Liferay url to normal url
I used like this :
<g:paginate next="Forward" prev="Back"
maxsteps="10" controller="book"
action="list" total="${Book.count()}" />
thanks in advance.
The normal pagination tag won't work inside a portlet, you'll need to copy the tag code and modify it to post to the actionURL of your portlet (call the jsp tag provided by the portal to get this url from your gsp tag code), instead of the default url for the controller.
I've never done this myself, someone else my be able to post some example code.
cheers
Lee

Resources