Expand my Community achievements bar.

SOLVED

How to pull content from JCR into other components

Avatar

Level 1

Hi, our company is new to CQ5.

We have blog post pages, and part of their data is title, hero image and short description of the blog post.

We want to pull that data (I assume it lives in /content folder in the JCR) and put it into other components we use around the site. Specifically we want a sort of "blog sneak peak" component that can go on any page and links to the whole blog post. Surely that's right up CQ's alley, but we're not sure how. Can someone layout the general process we'd use to do this? To us it seems strange to have to use an API to access this data.

Regards,

Roneesh

1 Accepted Solution

Avatar

Correct answer by
Employee

Hi Ron,

There's probably a half dozen ways to do this depending on the particulars. Here's one...

For your blog post page component, add a new script, called (for example) "teaser.jsp". In this script, output the HTML for the blog entry which would be visible in this sneak peak context. Maybe it is just the headline, title, and link. I don't really know.

Then, in your "blog sneak peak" component, give the author a pathfield allowing them to select the blog entry (assuming this what you want). Let's call that property blogEntryPath.

Then, in the script for the sneak peak component, do something like this:

<sling:include path="${properties.blogEntryPath}" replaceSelectors="teaser"/>

This will cause the blog entry to be rendered using the teaser script. 

Regards,

Justin

View solution in original post

6 Replies

Avatar

Correct answer by
Employee

Hi Ron,

There's probably a half dozen ways to do this depending on the particulars. Here's one...

For your blog post page component, add a new script, called (for example) "teaser.jsp". In this script, output the HTML for the blog entry which would be visible in this sneak peak context. Maybe it is just the headline, title, and link. I don't really know.

Then, in your "blog sneak peak" component, give the author a pathfield allowing them to select the blog entry (assuming this what you want). Let's call that property blogEntryPath.

Then, in the script for the sneak peak component, do something like this:

<sling:include path="${properties.blogEntryPath}" replaceSelectors="teaser"/>

This will cause the blog entry to be rendered using the teaser script. 

Regards,

Justin

Avatar

Level 1

Justin, thanks for your reply, I really appreciate it!

 

Can you explain this part of your proposed solution?

replaceSelectors="teaser"

what exactly is that part doing?

Avatar

Level 1

Also Justin, let's say in our "blog teaser" component, we want our content authors to just be able to select the blog post the teaser is for via a drop-down field in the dialog.

 

Is there a way we could do something like:

${sling:getResource(resourceResolver,'/content/blogs')}"

And they could get a drop-down of blog content nodes?

Sorry for the vagueness, I'm new to CQ, and not even sure if sling:getResource is correct. If you're not clear on what I'm saying, please let me know! Slowly but surely I'm learning though!

-Roneesh

Avatar

Level 8

Typically you would not use a drop down for that functionality, but rather a pathfield widget (which is what Justin mentions in his comment). The path field widget is used to select pages from the repository and stores the path to the page in the component's content.

Avatar

Level 2

just like the way you can output node content using .xml suffix to a url, you can write some selector which outputs text in html format 

for your component jsp. (you cannot output image, so give a reference path to image etc). 

>>"blog sneak peak" component that can go on any page and links to the whole blog post.

its not clear what you mean by component can go any page ... if you are referring to an author utility component which 

author can use to link another post then you can go on implementing a "path" type which lets author browse the repository and get the path. 

Avatar

Level 1

Hi Venu, thanks for your reply, can you explain how to do a few things you mentioned?

1. How can you write a selector which outputs text in html format? Can you show me a code example?

2. Can you show me how to implement a "path" type which lets the author browse the repository and get the path?

To clarify, we have multiple pages for products and multiple different pages for blog posts. On the product pages we want to be able to place a "blog teaser" component, which is information gotten from the blog post pages and the component has a link to the full blog post. So rather than have our authors write that themselves, we'd rather they just select the blog post they want to reference and that "blog teaser" component populate from the blog post node content.

Thanks everyone!

Ron