在Symfony2中如何在Twig文件中获取控制器变量或返回...?即我有一个返回结果数组的函数,我想在twig文件中显示这些....?
Php控制器:
<?php
namespace TalkTalk\NewsInfrastructure\Sitemap\Factory;
use TalkTalk\NewsInfrastructure\Article\Factory\DbArticleReadRepository;
use TalkTalk\NewsInfrastructure\DatabaseRepository;
Class SitemapFactory extends DatabaseRepository{
/**
* @var string
*/
private $baseUrl = 'news/';
/**
* @var string
*/
private $feedsSlug;
/**
* @var string
*/
private $articleSlug;
/**
* @var string
*/
private $siteMap;
/**
* @array URLforSitemap[]
*/
private $urls = array();
/**
* @return mixed
*/
public function getURLs()
{
$this->urls = $this->db->createQueryBuilder()
->select('faf.article_id', 'faf.feed_id')
->addSelect('f.slug AS feedSlug')
->addSelect('a.slug AS articleSlug')
->addSelect("CONCAT('news/' , f.slug, '/' , a.slug) AS url")
->from('article_feeds', 'faf')
->leftJoin('faf', 'articles', 'a', 'faf.article_id = a.id')
->leftJoin('faf', 'feeds', 'f', 'faf.feed_id = f.id');
return $this->urls->execute()->fetch(\PDO::FETCH_OBJ);
}
public function setSiteMap()
{
$this->siteMap = '<?xml version="1.0" encoding="UTF-8"?>';
$this->siteMap .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
foreach($this->urls as $url){
$this->siteMap .= '<url>';
$this->siteMap .= '<loc>'.$url.'</loc>';
$this->siteMap .= '<lastmod>2014-11-26</lastmod>';
$this->siteMap .= '<changefreq>daily</changefreq>';
$this->siteMap .= '<priority>0.5</priority>';
$this->siteMap .= '<url>';
}
echo $this->siteMap .= '</urlset>';
}
public function getSiteMap()
{
return $this->siteMap;
}
}
Twig文件(SoFar):
{# src/TalkTalk/NewsInfrastructure/Sitemap/Views/sitemap.twig #}
<! DOCTYPE html>
<html>
<head>
{% block head %}
<title>{% block title %}Sitemao{% endblock %} </title>
{% endblock %}
</head>
<body>
<h1>{{ Sitemap }}</h1>
<ul>
{% foreach test as url %}
<li>{{ }}</li>
</ul>
</body>
</html>
我现在可以在功能包围{%}}之间的树枝中使用for循环,并在这些包围{{}}之间显示结果但是我如何将控制器的结果带入树枝文件中??< / p>