Message Boards

Is there a way to insert a page fragment to a ftl file?

Tony Wang, modified 3 Years ago.

Is there a way to insert a page fragment to a ftl file?

New Member Posts: 3 Join Date: 11/14/20 Recent Posts

Is there a way to insert the content of a page fragment to a ftl file? Like create a marco to get the page fragment.. not sure if it's possbile. Any example would be greatly appreciated. 

thumbnail
Christoph Rabel, modified 3 Years ago.

RE: Is there a way to insert a page fragment to a ftl file?

Liferay Legend Posts: 1554 Join Date: 9/24/09 Recent Posts

In the most general sense, the answer is probably no.

But what do you want to achieve in detail? There are several things that can be done. Do you want to reuse the content somewhere? Do you need to move things around for mobile devices?

Tony Wang, modified 3 Years ago.

RE: RE: Is there a way to insert a page fragment to a ftl file?

New Member Posts: 3 Join Date: 11/14/20 Recent Posts

Thanks for the response! I've created a page fragment and place it to a page. You're right, I now need to show the same content for mobile in different way, not in the page, but to show it in a popup dialog. I don't want to display the entire page in the popup, just need my fragment. Is there a way I can reuse the fragment without embedding it in a page? 

thumbnail
Christoph Rabel, modified 3 Years ago.

RE: Is there a way to insert a page fragment to a ftl file? (Answer)

Liferay Legend Posts: 1554 Join Date: 9/24/09 Recent Posts

Let me assume a few things:

You habe a page A and a page B. Page A contains your fragment. Now, for the mobile case, you want to show page B, but show some part of page A in a modal dialog? Correct?

So:

If you want to reuse content, I would use a webcontent instead of a fragment. Fragments are bound to a page, they are not really accessible independently from the rest of the page, Webcontents instead are "free" and can be used everywhere.

If you really, really, really need to do it with fragments, it gets really tricky. I would avoid that.

a) You could fetch page A with Javascript, find the relevant part, cut it out and insert it into the modal dialog.

b) You could implement a FragmentEntryProcessor. This won't work always since the processor is called when the page is rendered (and if the content isn't cached), so if you have personalized content in the fragment, it won't work (since User X might never access page A on desktop). The idea is: When page A is rendered, you could take the content in the processor and store it somewhere (probably in the DB). Later, when page B needs it, you fetch it from there and insert it.

 

To me, b) looks quite fragile and I wouldn't do it this way. a) could be an option if you can live with the overhead of reading the whole page just to display a part of it. It works, I have seen an implementation that does something like that.

Using Webcontent would be my favorite.

 

Tony Wang, modified 3 Years ago.

RE: RE: Is there a way to insert a page fragment to a ftl file?

New Member Posts: 3 Join Date: 11/14/20 Recent Posts

Thank you so much for the suggestions! I did some research on WebContent, expected to find a way to embed the page fragment to a new webconten, but couldn't. Not sure if I missed anything here. My fragement includes css and javascript logic. I also couldn't find a way to achieve the same functionality with static webcontent. I took the option A and made it works. I used a jquery.get() to get the page content, then find the fragment section and load it to the modal dialog. I think It would be great if liferay provides some APIs that front end can connect to retrieve the fragement content instead of like me retrieving the whole page to get the fragment section. But anyway, it works. Thank you again for your advice!