You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Davide Vecchi <dv...@amc.dk> on 2013/10/08 16:07:30 UTC

Getting Block content as String

I have an instance of org.apache.tapestry5.Block and I would like to get this block's content as a String.

When I inspect that Block instance, I see the block content that I need to get: there is a field called "elements" which is an ArrayList with 3 elements. One of them is an ExpansionPageElement and it has a field called "binding" which has a field called "label" which contains my text.

However Block seems to have no methods I can call to get the value of that "label" field.

I imagine this means that it's wrong of me to try to get that value, but I'm not clear why.

Should I just give up and try to change the design, or there is a way to get the value of that "label" field ? After all that value is there and it was actually supposed to be there, so I don't know the reason why Tapestry doesn't want that content to be retrievable.

Re: Getting Block content as String

Posted by Lance Java <la...@googlemail.com>.
It's probably overkill but depending on what you are doing, you might find
tapestry-offline useful

https://github.com/uklance/tapestry-offline

Re: Getting Block content as String

Posted by Lance Java <la...@googlemail.com>.
Here's some discussion which has a couple of answers

http://apache-tapestry-mailing-list-archives.1045711.n5.nabble.com/Encoding-a-template-as-a-valid-JSON-String-td5723504.html

RE: Getting Block content as String

Posted by Lance Java <la...@googlemail.com>.
As Thiago said, can you specify the value somewhere (message catalogue,
database etc) and reference the same value in both the template and the
action?

RE: Getting Block content as String

Posted by Lance Java <la...@googlemail.com>.
You can only get the rendered HTML after the point at which your block is
rendered to tapestry's MarkupWriter.

So, you only have an opportunity to get the HTML in a full page render
request or an AJAX event request which causes a partial page render

Getting the HTML in a non-ajax action (ie your action) is not (easily)
possible since tapestry doesn't render anything to the MarkupWriter in the
action request. Instead, tapestry returns a 304 redirect and the render
happens in the subsequent request.

RE: Getting Block content as String

Posted by Lance Java <la...@googlemail.com>.
> because if at that point I inspect the block instance (Eclipse debug) I
see the instance already contains that text.

I've never really looked into the inner workings of a block. The only thing
you should ever need to know about a block is that it can be coerced to a
RenderCommand. I'm sure it holds a reference to the static content etc in
your template but this is considered as private implementation and is 'off
limits'

> I'm attaching a screenshot of the Inspect window.

Attachments don't work in this forum.

RE: Getting Block content as String

Posted by Davide Vecchi <dv...@amc.dk>.
Thanks for all the info, good learning tool for me.

> Getting the HTML in a non-ajax action (ie your action) is not (easily)
> possible since tapestry doesn't render anything to the MarkupWriter in the
> action request. Instead, tapestry returns a 304 redirect and the render
> happens in the subsequent request.

Got it. The reason why I thought it was possible is that it looks to me that my content is already rendered at the point where I was hoping to get it, because if at that point I inspect the block instance (Eclipse debug) I see the instance already contains that text. I'm attaching a screenshot of the Inspect window.

> As Thiago said, can you specify the value somewhere (message catalogue, database etc)
> and reference the same value in both the template and the action?

My specific case is a bit complicated, however I noted your suggestion as one of the right ways to do that and in the future I will certainly benefit from it, thanks.

> There's not a way because in Tapestry it makes no sense for doing that.
> Blocks are pieces of template.

Actually the fact that blocks are pieces of template is probably where my misunderstanding started. I thought it was possible to put some text in a template block so that I could just retrieve it from Java. Now I realized that's not what blocks are for.

> Block names (id) are static, so they cannot be generated at runtime. You
> can use them dynamically, but their declaration is completely static.

I think I explained myself badly. I meant that in Java I compose the block name at runtime, so I can't have a Block field named like the template block id. In my example above it would be like if instead of

	Block block = this.componentResources.getBlock("myBlock"); 

there was

	String var1 = "my";
	String var2 = "Block";
	Block block = this.componentResources.getBlock(var1 + var2);

except of course that the 2 strings are not hardcoded like that, they come from somewhere.

This whole problem started just because I didn't go the Tapestry way since the beginning, so now that I realized that everything is sweet :)

Re: Getting Block content as String

Posted by Thiago H de Paula Figueiredo <th...@gmail.com>.
On Wed, 09 Oct 2013 11:21:57 -0300, Davide Vecchi <dv...@amc.dk> wrote:

> Thiago: thanks for the clarification. I actually went for some redesign;  
> I gave up trying to get the block content, although it still feels a bit  
> weird to me, probably just because I'm new to Tapestry.
>
> Where the data is coming from is just a block that I had put in the  
> template with the exact purpose of holding some text that the Java code  
> might need under some circumstances. Now I realized this is not what  
> blocks are intended for, so I'm going another route.

You were probably confusing the use of blocks with the one of components.

> However I had already prepared a trivial example of the problem of  
> getting the block content, so although I no longer need to solve that  
> I'm posting the example anyway because it would still be interesting to  
> see if there was a way.

There's not a way because in Tapestry it makes no sense for doing that.  
Blocks are pieces of template.

> Lance: thanks for the tips; I believe in the thread you linked I was  
> supposed to look at post "Sep 19, 2013; 12:13pm", which actually does  
> what I was asking for, get the block content as String. However in my  
> specific case I wouldn't have been able to use it because if I  
> understood correctly that requires having in Java a Block field  
> (myBlock) named like the block in the template, while in my case the  
> block name is generated at runtime and it might or might not match an  
> existing block.

Block names (id) are static, so they cannot be generated at runtime. You  
can use them dynamically, but their declaration is completely static.

-- 
Thiago H. de Paula Figueiredo
Tapestry, Java and Hibernate consultant and developer
http://machina.com.br

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


RE: Getting Block content as String

Posted by Davide Vecchi <dv...@amc.dk>.
Thiago: thanks for the clarification. I actually went for some redesign; I gave up trying to get the block content, although it still feels a bit weird to me, probably just because I'm new to Tapestry.

Where the data is coming from is just a block that I had put in the template with the exact purpose of holding some text that the Java code might need under some circumstances. Now I realized this is not what blocks are intended for, so I'm going another route.

However I had already prepared a trivial example of the problem of getting the block content, so although I no longer need to solve that I'm posting the example anyway because it would still be interesting to see if there was a way.

Lance: thanks for the tips; I believe in the thread you linked I was supposed to look at post "Sep 19, 2013; 12:13pm", which actually does what I was asking for, get the block content as String. However in my specific case I wouldn't have been able to use it because if I understood correctly that requires having in Java a Block field (myBlock) named like the block in the template, while in my case the block name is generated at runtime and it might or might not match an existing block.

TML: ---------------------------------------------------------------------
<html	xmlns:t="http://tapestry.apache.org/schema/tapestry_5_1_0.xsd"
		xmlns:p="tapestry:parameter"> 
<body>
	
	<div>TestGetBlockContent example</div>

	<t:zone t:id="aZone">

		<t:block id="myBlock">
		
			This is the content I want to get.
			In the Java code I have this block as an instance of Block .
			
		</t:block>
		
		<div>
			<t:actionlink t:id="getIt">Get it</t:actionlink>
		</div>
		
	</t:zone>

</body>
</html>

Java: ---------------------------------------------------------------------

import org.apache.tapestry5.Block;
import org.apache.tapestry5.ComponentResources;
import org.apache.tapestry5.ioc.annotations.Inject;

public class TestGetBlockContent {
	
	@Inject
	ComponentResources componentResources;

	Object onActionFromGetIt()
	{
		Block block = this.componentResources.getBlock("myBlock");
		
		// Now I'd like to extract the content of the block as a String, but all I'm
		// able to do is calling toString, which does not return the block content :
		
		String blockText = block.toString();
		
		System.out.println(blockText);
		
		return this;
	}
	
}


Re: Getting Block content as String

Posted by Thiago H de Paula Figueiredo <th...@gmail.com>.
On Wed, 09 Oct 2013 04:10:55 -0300, Davide Vecchi <dv...@amc.dk> wrote:

> Hi, thanks for the assistance. The template is quite big so I will have  
> to strip it down much, to avoid posting a lot of unrelated stuff; I will  
> do that as soon as possible.

Hi! Ok! I guess just the part containing the "label" field should be  
enough.

> In the meantime, does it mean that I better stop trying to get the value  
> of that "label" field from that Block instance ? I believe so but I'm  
> not sure.

I think so. Instead, think that the property you're editing with that  
field comes from some other place, so you should get its value from that  
place, not from the block. A block is just a delimited portion of a  
template, not a source of data. That's why I'm asking the relevant parts  
of the block and the Java code related to it: to know where the data is  
coming from.

-- 
Thiago H. de Paula Figueiredo
Tapestry, Java and Hibernate consultant and developer
http://machina.com.br

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


RE: Getting Block content as String

Posted by Davide Vecchi <dv...@amc.dk>.
Hi, thanks for the assistance. The template is quite big so I will have to strip it down much, to avoid posting a lot of unrelated stuff; I will do that as soon as possible.

In the meantime, does it mean that I better stop trying to get the value of that "label" field from that Block instance ? I believe so but I'm not sure.


-----Original Message-----
From: Thiago H de Paula Figueiredo [mailto:thiagohp@gmail.com] 
Sent: Tuesday, October 8, 2013 19:20
To: Tapestry users
Subject: Re: Getting Block content as String

Hi!

Please post the block template and the code it uses so we can have a better picture.

On Tue, 08 Oct 2013 11:07:30 -0300, Davide Vecchi <dv...@amc.dk> wrote:

> I have an instance of org.apache.tapestry5.Block and I would like to 
> get this block's content as a String.
>
> When I inspect that Block instance, I see the block content that I 
> need to get: there is a field called "elements" which is an ArrayList 
> with 3 elements. One of them is an ExpansionPageElement and it has a 
> field called "binding" which has a field called "label" which contains my text.
>
> However Block seems to have no methods I can call to get the value of 
> that "label" field.
>
> I imagine this means that it's wrong of me to try to get that value, 
> but I'm not clear why.
>
> Should I just give up and try to change the design, or there is a way 
> to get the value of that "label" field ? After all that value is there 
> and it was actually supposed to be there, so I don't know the reason 
> why Tapestry doesn't want that content to be retrievable.


--
Thiago H. de Paula Figueiredo
Tapestry, Java and Hibernate consultant and developer http://machina.com.br

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org

Re: Getting Block content as String

Posted by Thiago H de Paula Figueiredo <th...@gmail.com>.
Hi!

Please post the block template and the code it uses so we can have a  
better picture.

On Tue, 08 Oct 2013 11:07:30 -0300, Davide Vecchi <dv...@amc.dk> wrote:

> I have an instance of org.apache.tapestry5.Block and I would like to get  
> this block's content as a String.
>
> When I inspect that Block instance, I see the block content that I need  
> to get: there is a field called "elements" which is an ArrayList with 3  
> elements. One of them is an ExpansionPageElement and it has a field  
> called "binding" which has a field called "label" which contains my text.
>
> However Block seems to have no methods I can call to get the value of  
> that "label" field.
>
> I imagine this means that it's wrong of me to try to get that value, but  
> I'm not clear why.
>
> Should I just give up and try to change the design, or there is a way to  
> get the value of that "label" field ? After all that value is there and  
> it was actually supposed to be there, so I don't know the reason why  
> Tapestry doesn't want that content to be retrievable.


-- 
Thiago H. de Paula Figueiredo
Tapestry, Java and Hibernate consultant and developer
http://machina.com.br

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org