Symfony2 syntax for XML output

I am using Symfony2 and am trying to use a branch to output XML data. What happens instead is that the branch just throws a massive block of text to the browser, only when I right clicked to view the source I see the nicely laid out XML.

Is there a way to get Twig to actually output formatted XML instead of a block of text without having to look at the page source ...?

sitemap.xml.twig:

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <url>
        {% for entry in sitemapresp %}
            <loc>{{ entry['url'] }}</loc>
            <lastmod>{{ entry['date'] }}</lastmod>
            <changefreq>{{ entry['frequency'] }}</changefreq>
            <priority>{{ entry['priority'] }}</priority>

        {% endfor %}
    </url>
</urlset>

      

Browser output:

http://www.sitemappro.com/2015-01-27T23:55:42+01:00daily0.5http://www.sitemappro.com/download.html2015-01-26T17:24:27+01:00daily0.5

      

Source output:

    <?xml version="1.0" encoding="UTF-8"?>
    <urlset xmlns="http://www.google.com/schemas/sitemap/0.90">
      <url>
        <loc>http://www.sitemappro.com/</loc>
        <lastmod>2015-01-27T23:55:42+01:00</lastmod>
        <changefreq>daily</changefreq>
        <priority>0.5</priority>
      </url>
      <url>
        <loc>http://www.sitemappro.com/download.html</loc>
        <lastmod>2015-01-26T17:24:27+01:00</lastmod>
        <changefreq>daily</changefreq>
        <priority>0.5</priority>
      </url>
</urlset>

      

Any suggestions..?

+5


source to share


3 answers


If you want a page for XML, you will need to set the response content type.

$response = new Response($this->render('sitemap.xml.twig'));
$response->headers->set('Content-Type', 'application/xml; charset=utf-8');
return $response;

      



If you want a portion of the page to render the code on the HTML page, use:

{% autoescape %}
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <url>
        {% for entry in sitemapresp %}
            <loc>{{ entry['url'] }}</loc>
            <lastmod>{{ entry['date'] }}</lastmod>
            <changefreq>{{ entry['frequency'] }}</changefreq>
            <priority>{{ entry['priority'] }}</priority>

        {% endfor %}
    </url>
</urlset>
{% endautoescape %}

      

+10


source


Controller side:



$response = new Response();
$response->headers->set('Content-Type', 'text/xml');
return $this->render(
   'Bundle:Controller:sitemap.xml.twig',
   array(
        'param1' => $param1,// ...
   ),
   $response
);

      

+7


source


You only have to render the view in order to send it in response.

$response = new Response($this->renderView('sitemap.xml.twig'));
$response->headers->set('Content-Type', 'application/xml; charset=utf-8');
return $response;

      

So, replace $this->render(...)

with$this->renderView(...)

HTTP/1.0 200 OK Cache-Control: no-cache....

disappear

+1


source







All Articles