How to use projection query in custom template?

Topics: General
Oct 25, 2013 at 12:40 PM
I've created custom template for content Id-12.
I want to use one projection query result on that custom template page. I don't want to put only link. Instead of putting only link, I want to display projection query result.

How can achieve this?
Oct 26, 2013 at 6:20 AM
You should never be doing database queries from the view. You need to provide more details. Does that content item have a projection part?
Oct 28, 2013 at 9:39 AM
I'm not trying to use direct database queries on view. I've defined some of queries using projections. I want to render those queries on custom template.

For example,

I've content and its id is 12 and I've separate template Content-12.cshtml to apply custom styles and mark up. On another side I've number of projection queries like List of News, List of Offers and List of Events that returns list of News content type, list of offers content type and list of events content type respectively.

I want to use those projection queries on Contetn-12.cshtml. So How can I use this?

No, Content does not have projection part. but I'm not clear that how it useful?
Oct 28, 2013 at 11:06 AM
Now I'm sure you will find some answers that are more helpful, but I can perhaps chime in.

To render the projection on your custom template I'm pretty sure it has to be injected into the view, like a Model is. So this Custom template that renders content with id 12 can render in any way all items belonging to that content.

If you explain better what you are trying to accomplish then perhaps there are other ways the people here can suggest. If you just want to show some queries in the same place as the content is rendered then you could always just put the projection widget in the same zone and use placement to align it correctly.

You can also use the Shape layout for projections to customize how the projections are rendered.

Also note that if your content should include a collection of items you might want to have it as a container/containable relationship instead.
Oct 28, 2013 at 12:01 PM
Thanks IngoVals for your kind reply.

You are right, I can accomplish by rendering different projection widgets in the same zone.
Let me try once and will update you.
Oct 28, 2013 at 4:41 PM
You said "I want to use those projection queries on Contetn-12.cshtml". Projection queries are queries against the database. So yes, you are absolutely trying to query the database from the view.

I think the widget approach is a little convoluted: any time a widget is associated with a single content item, something is likely to be wrong. Instead, you can create a content type that has a projection part. That will enable you to add query results to items of that type.