我正在尝试构建一个页面,该页面将网站上的所有页面收集在一起,并将它们与他们的孩子(如果有的话)一起显示。
到目前为止,我已经设法让它遍历所有顶级页面并显示它们,但是我很难让它在正确的位置显示子页面。似乎我可以检索它们但让它们出现在最后,或者根本不检索它们。
我需要它看起来像这样:
<div id="parent" class="guide-item">
<h2>Parent</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p><p>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.</p>
<div id="child" class="guide-item">
<h2>Child</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p><p>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.</p>
</div>
</div>
这是我的控制器(包括我试图让它循环遍历孩子:
// Render pages from database
public function sectorAction($sector)
{
$em = $this->getDoctrine()->getManager();
$pages = $this->getDoctrine()
->getRepository('acmeStyleGuideBundle:pageSector')
->findBySectorJoinedToUrlTopLevel($sector);
if (!$pages) throw $this->createNotFoundException('Unable to find any matching sectors');
foreach ($pages as $page) {
if ($page->getChildPages()) {
$children = $this->getDoctrine()
->getRepository('acmeStyleGuideBundle:pageContent')
->findBySectorAndParent($sector, $page->getPageUrl());
}
}
return $this->render(
'acmeStyleGuideBundle:Page:pages.html.twig',
array(
'Pages' => $pages,
'Children' => $children,
'header' => $sector
)
);
}
以下是相关存储库:
public function findBySectorJoinedToUrlTopLevel($sector)
{
$query = $this->getEntityManager()
->createQuery('
SELECT p, s FROM acmeStyleGuideBundle:PageContent p
JOIN p.pageSector s
LEFT JOIN p.pageTypes t
WHERE s.sectorName = :sector
AND t.typeName != :type
AND p.parentPage IS NULL'
)
->setParameter('type', 'Section Headers')
->setParameter('sector', $sector);
try {
return $query->getResult();
} catch (\Doctrine\ORM\NoResultException $e) {
return null;
}
}
public function findBySectorAndParent($sector, $pageParent)
{
$query = $this->getEntityManager()
->createQuery('
SELECT p, s, c FROM acmeStyleGuideBundle:PageContent p
JOIN p.pageSector s
LEFT JOIN p.pageTypes t
LEFT JOIN p.parentPage c
WHERE s.sectorName = :sector
AND p.pageUrl = :parent
AND t.typeName != :type'
)
->setParameter('sector', $sector)
->setParameter('parent', $pageParent)
->setParameter('type', 'Section Headers');
try {
return $query->getResult();
} catch (\Doctrine\ORM\NoResultException $e) {
return null;
}
}
这是我要输出代码的树枝模板:
{% extends 'acmeStyleGuideBundle::landing.html.twig' %}
{% block definition %}
<article class="js-load pageLoad">
<h1>
{% if header is defined %}
{{ header | title }}
{% else %}
{{ Pages[0].pageName }}
{% endif %}
</h1>
{% for pe in Pages %}
<div id="{{ pe.pageUrl | lower}}" class="guide-item">
<h2>{{ pe.pageName }}</h2>
{{ pe.richText | raw }}
</div>
{% endfor %}
</article>
{% endblock %}
我认为我可能能够遍历控制器中的子级,然后将其作为“子级”而不是“页面”应用到树枝模板,但这似乎不起作用,因为以下代码只是不断重复最后一个数据库中不是子页面的元素。:
{% for ce in Children %}
<div class="childPage">
<div id="{{ ce.pageUrl | lower}}" class="guide-item">
<h2>{{ ce.pageName }}</h2>
{{ ce.richText | raw }}
<div class="explanation">
<div class="card active">
<h3>Example</h3>
{# ce.example | raw#}
</div>
<div class="card">
<h3>Code Example</h3>
<pre name="code" class="{#ce.lang#}">{# ce.example #}</pre>
</div>
</div>
</div>
</div>
{% endfor %}
我已经研究这个问题一段时间了,所以我可能再也看不到树木的木材了,所以请随意将我的代码分解。无论如何,我是 symfony/twig/doctrine 的学习者,所以我很乐意接受您愿意给我的任何反馈。