Jsp, Can It Work Similar to Yield, Layout, Content_For in Ruby/Rails/Erb

JSP, can it work similar to yield, layout, content_for in Ruby/Rails/Erb

I'm not familiar with what yield and content_for provide, but JSP tag files allow you a more robust way to template pages than JSP includes.

Example:

layout.tag

<%@ tag body-content="scriptless" %>
<%@ attribute name="pageTitle" required="true" type="java.lang.String" %>

<html>
<head>
<title>${pageTitle}</title>
</head>
<body>
<jsp:doBody/>
</body>
</html>

An individual JSP

<%@ taglib prefix="z" tagdir="/WEB-INF/tags" %>
<z:layout pageTitle="A simple page">
<p>Hello, JSP!</p>
</z:layout>

Just place your layout.tag in the /WEB-INF/tags directory. You can use any available prefix you want, I just used "z" for the example.

Rails what is the difference between content_for and yield?

yield is how you specify where your content areas is going to go within a layout. You might have something like this:

<div>
<h1> This is the wrapper!</h1>
<%= yield :my_content %>
</div>

content_for is how you specify which content is going to be rendered into which content area. You might have something like this:

<% content_for :my_content do %>
This is the content.
<% end %>

The result would be

<div>
<h1> This is the wrapper!</h1>
This is the content.
</div>

They are opposite ends of the rendering process, with yield specifying where content goes, and content_for specifying what the actual content is.

Is there a generally accepted best practice?

The best practice is to use yield in your layouts, and content_for in your views. There is a special second use for content_for, where you give it no block and it returns the previously rendered content. This is primarily for use in helper methods where yield cannot work. Within your views, the best practice is to stick to yield :my_content to recall the content, and content_for :my_content do...end to render the content.

How do I yield from an ERB code block without rendering it?

ERB has an internal buffer, which makes using blocks a bit more complicated, as you can see in your code example.

Rails provides a capture method, which allows you to capture a string inside this buffer and return it from a block.

So your helper would become the following:

def make_backwards
capture do
yield.reverse
end
end

yield in Rails versus yield in Ruby

It's the same thing. A block is passed to the layout method that will render the document body when called.

yield is actually a keyword in Ruby. It is possible to define a method named "yield" (Enumerator does this, for example), but whenever you see yield without a receiver, it will always be the same keyword that passes control to a block.

Is there anything better than Tiles for Struts2?

Yes. Use the JSP 2.0 tag file feature. My answer to a previous, similar question may be of some use to you.

JSP, can it work similar to yield, layout, content_for in Ruby/Rails/Erb

Rails - How can I detect if the content_for content was provided?

You can streamline the code further:

def content_exists?(name)
return instance_variable_get("@content_for_#{name}")
end


Related Topics



Leave a reply



Submit