2

Is it possible to use the same CT for multiple components(based on same schema) considering the mapping required at ViewModels Class for CT name. Because if we see the source for CT is same as using same TBBs "Generate dynamic component", "Publish binaries for page".

Idea is not to create new CT for each component which are based on same schema to reduce no. of CP items created in DB as end result which are huge.

Trying to achieve something as displayed below in snapshot: diagram

1
  • When you mention "based on same schema" are you describing a setup with a Component Template per Component? Or are you talking about a CT or two per Schema? You would rarely need a CT per Component, regardless if it's dd4t or not. Commented Jan 27, 2016 at 8:02

2 Answers 2

2

Yes, it is possible to use the same CT for multiple components (based on a single schema), per ViewModel though.

It is sufficient to built one component template for every view model that you have. In Tridion, you will continue to use the same TBBs with your CT unless component template metadata is different based on your entity or view. You can add more TBBs to suit your requirement but the output should be "JSON".

Refer an useful article from Albert here

You will offcourse have to add your schemas to the linked schemas within the CT in Tridion.

Update

Well, I am having trouble interpreting your snapshot but I would like to point out that you would need one View Model for every schema that you built. Based on the code that you have below, you are missing the getter / setter methods for each of the fields that you have defined for your schema.

I think I already sent the article in another response to your other question but the following diagram (from Raimond's post) explains a lot.

I would appreciate if you can update your question with the code in comments and try to come up with clarification so that the community can address.

7
  • Hi Shiva Are you suggesting this - one CT(linked to schema S1) and Two components C1 and C2 (based on schema S1) and ViewModel Class VM1 (mapped to C1) and VM2(mapped to C2) . If yes can you give one sample for CT(Metafields) and Viewmodel Class; not sure how the linking will work Commented Jan 27, 2016 at 0:03
  • 1
    You link VM1 to either S1 and / or CT1. All components published with that CT will then be able to use the view model. In your case, a rootElementNames = {"S1"} property on the @ViewModel annotation should be enough.
    – Raimond
    Commented Jan 27, 2016 at 9:09
  • I need separate viewModel class for C1 and C2. because C1 and C2 has different set of content(based on same schema) and C1 and C2 has separate component JSP or component view. Commented Jan 27, 2016 at 21:26
  • A component template is different from a component presentation. You can have multiple component templates associated with the same component rendering different component presentations (ofc, you will have to code this). This is true if you are using Tridion with or without DD4T. I suggest trying out a simple example (try some use cases too) from online documentation (though multimedia) and I am hoping that it would clarify your question docs.sdl.com/LiveContent/content/en-US/…
    – Shiva
    Commented Jan 27, 2016 at 23:10
  • My issue is specific to - how to define the mapping in ViewModel Class if I am using same CT for multiple components(based on same schema); In below example - I have View Model class where I am forced to use 2 CT because I have 2 different component jsp view. Commented Feb 4, 2016 at 22:46
0

After reading the comment:

I need separate viewModel class for C1 and C2. because C1 and C2 has different set of content(based on same schema) and C1 and C2 has separate component JSP or component view.

I think I know what you mean. While having the different models under the same view model name or schema is a future improvement, in the mean time the dd4t-databind framework is flexible enough to have two different model classes for Component Presentations, with the same combination of Schema and Component Template. It's a bit hacky, but this is possible:

ViewModel A:

@ViewModel(
    viewModelNames = { "header_ct", "footer_ct"},
)
public class HeaderAndFooter extends TridionViewModelBase { } 

ViewModel B:

@ViewModel(
    rootElementNames = { "content" },
)
public class Content extends TridionViewModelBase { } 

This way, in your JSPs you can do:

View A:

<jsp:useBean id="header_ct" type="com.foo.HeaderAndFooter" scope="request"/>

View B:

<jsp:useBean id="content" type="com.foo.Content" scope="request"/>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.