2

I have a Page Template where Region and Entities exists. I want to get an attribute value in EntityModel in a PageTemplate and render it.

I had kept the variable in request,page scopes but was not successful.

Is there anyway we can accomplish it.

  • If I understand your requirement properly, you want to render one attribute value of entity model into page template. is it correct ? – Sayantan Basu Feb 6 '17 at 16:39
  • Yes Sayantan Basu. You are right. – Suresh Feb 6 '17 at 18:00
4

The Page View has access to the Page Model, which contains the Region Models, which in turn contain the Entity Models.

That means that a Page View has direct access to al the Entity Models on the Page. For example, to access the first Article in the "Main" Region: var article = (Article) Model.Regions["Main"].Entities[0].

Theoretically, you could render everything in a Page View (and not use Region or Entity Views at all), but it is good practice to delegate rendering to Region/Entity Views.

Note that Region and Entity Views render in a rather isolated manner, but since DXA 1.6, you can also access context the Page Model from a Region or Entity View.

| improve this answer | |
  • Thanks Rick. I tried the above methodology and good the values like Main, Header, Footer. But was unable to retrieve the actual model under Footer Region. In our case, we are calling Footer as below. – Suresh Feb 7 '17 at 15:50
  • Thanks Rick. I tried using the below code but could not get the actual Footer Entity residing in Footer Region. <% Iterator iter=pageModel.getRegions().iterator(); while(iter.hasNext()) { RegionModel regionModel=((RegionModel)iter.next()); if(regionModel.getName().equals("Footer")) { List<EntityModel> entities=regionModel.getEntities(); for(int i=0;i<entities.size();i++) { %> <%=entities.get(i)%> <%}}}%> – Suresh Feb 7 '17 at 15:57
  • The "Footer" Region is most likely a Region representing an Include Page. Such Include Page Regions contain another level of Region Models before you get to the Entity Models. Try requesting the Page in a browser with ?format=json in the URL. That yields the JSON representation of the Page Model which shows how it is structured. – Rick Pannekoek Feb 7 '17 at 18:49
  • Thanks Rick. i am able to get the value of that region and render it. Also thanks for giving one more tip to see the json format. – Suresh Feb 7 '17 at 21:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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