Forum
Pull in Nested Related Content
Issue
I have a template (For a Host) that pulls in related content (For that Host’s Events). The second template pulls in related items to that Event (Hosts & Shows). When the Event template is used in a Events collection loop, the Hosts & Shows related content populates properly. When called inside the Host template, those related fields do not populate.
Report:
Perch Runway: 3.1.4, PHP: 7.2.13, MySQL: mysqlnd 5.0.12-dev - 20150407 - $Id: 38fea24f2847fa7519001be390c98ae0acafe387 $, with PDO
Server OS: Linux, fpm-fcgi
Installed apps: content (3.1.4), assets (3.1.4), categories (3.1.4)
App runtimes: <?php $apps_list = [ ];
PERCH_LOGINPATH: /perch
PERCH_PATH: /app/perch
PERCH_CORE: /app/perch/core
PERCH_RESFILEPATH: /app/perch/resources
Image manipulation: GD
PHP limits: Max upload 2M, Max POST 8M, Memory: 512M, Total max file upload: 2M
F1: 3b606135b33e6a102526838f4152a807
Resource folder writeable: Yes
SCRIPT_NAME: /perch/core/settings/diagnostics/index.php
REQUEST_URI: /perch/core/settings/diagnostics/
DOCUMENT_ROOT: /app/
HTTP_HOST: vmpn.local
Templates:
This is the Event Card Template
In it you’ll see there‘s 2 related templates for pulling in Hosts & Shows related to this event. This works when just displaying the Event template in a loop, but when this template is called inside another template, the related templates don‘t get populated.
<article class="card:event row">
<div class="col-lg-6 card:event::col-1">
<figure class="card:event::image">
<perch:content id="event-img" type="image" output="tag" width="1000" height="1000">
</figure>
</div>
<div class="col-lg-6 card:event::col-2">
<h2 class="card:event::heading"><perch:content id="event-title" type="text"></h2>
<div class="card:event::content">
<perch:content id="event-body" type="textarea">
</div>
<div class="card:event::meta">
<div class="card:event::metabox">
<time class="card:event::datetime">
<div class="card:event::date">
<perch:content id="event-date" type="date" label="Date" format="%b %d, %Y">
</div>
<div class="card:event::time">
<perch:content id="event-time" type="text">
</div>
</time>
</div>
<div class="card:event::metabox">
<div class="card:event::location">
<div class="card:event::venue">
<perch:content id="event-venue" type="text" label="Venue" >
</div>
<div class="card:event::city">
<perch:content id="event-city" type="text" label="City" >
</div>
</div>
</div>
<div class="card:event::metabox">
<a
class="component:button"
href="<perch:content id="event-tickets">"
>
Get Tickets
</a>
</div>
</div>
<div class="card:events::hosts row »person@tiny">
<perch:related id="event-hosts" collection="Hosts">
<perch:template path="content/hosts/card.html">
</perch:related>
</div>
<div class="card:events::shows row »show@tiny" >
<perch:related id="event-shows" collection="Shows">
<perch:template path="content/shows/card.html">
</perch:related>
</div>
</div>
</article>
Host Template
This is the template that calls the above template ^
<article class="block:host">
<div class="container">
<div class="row middle-lg block:host::section">
<div class="col-lg-6 block:host::col-1">
<figure class="block:host::image">
<perch:content id="host-img" type="image" output="tag" width="1000" height="1000">
</figure>
</div>
<div class="col-lg-6 block:host::col-1">
<perch:if exists="host-twitter" exists="host-facebook">
<aside class="block:host::social">
<perch:if exists="host-twitter">
<a href="<perch:content id="host-twitter" type="url">" target="_blank">
<img src="/perch/addons/feathers/vmpn/images/twitter.svg">
</a>
</perch:if>
<perch:if exists="host-facebook">
<a href="<perch:content id="host-facebook" type="url">" target="_blank">
<img src="/perch/addons/feathers/vmpn/images/facebook.svg">
</a>
</perch:if>
</aside>
</perch:if>
<header class="block:host::header">
<h2 class="block:host::heading"><perch:content id="host-fullname" type="composite" for="host-firstname host-lastname"></h2>
<h3 class="block:host::subheading">
<perch:content id="host-lead" type="textarea">
</h3>
</header>
<div class="block:host::content">
<perch:content id="host-body" type="textarea" label="Body" markdown editor="simplemde" required imagewidth="800">
</div>
</div>
</div>
<section class="block:shows@featured block:host::section">
<div class="row">
<div class="col-lg-4">
<h2 class="component:heading"><span>Shows</span></h2>
</div>
</div>
<div class="row">
<perch:related id="host-show" collection="Shows" label="Shows">
<div class="col-lg-6">
<perch:template path="content/shows/card.html">
</div>
</perch:related>
</div>
</section>
<section class="block:events block:host::section">
<div class="row">
<div class="col-lg-4">
<h2 class="component:heading"><span>Upcoming Events</span></h2>
</div>
</div>
<perch:related id="host-event" collection="Events" label="Events">
<perch:template path="content/events/card.html">
</perch:related>
</section>
<section class="block:shows@related block:host::section">
<div class="row">
<div class="col-lg-4">
<h2 class="component:heading"><span>Related Shows</span></h2>
</div>
</div>
<div class="row">
<perch:related id="host-show" collection="Shows" label="Shows">
<div class="col-lg-3">
<perch:template path="content/shows/card.html">
</div>
</perch:related>
</div>
</section>
</div>
</article>
It looks like you're creating relationships in both directions, which probably isn't ideal. I'd stick to one direction only.
For example, if you have Hosts and Shows, presumably a Show has one or more Hosts. In the Show master template, I'd create a relationship to Hosts.
Then if you want to have a list of shows on a host's page, for example, you'd filter Shows by
host.slug
(or whatever) matching your currently viewed Host.Ok! I’ll try that!