Tomazi
Tomazi

Reputation: 847

Generating xml within twig template

I am trying to generate a sitemap, which works just fine if I do it in a standard xml file. The problem I am facing is that I need to generate a xml sitemap within twig file.

So i have this file: sitemap.xml.twig with this content:

{{ <?xml version="1.0" encoding="UTF-8"?> }}

{{ <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> }}
    {{ <url> }}
        {{ <loc>http://www.example.com/</loc> }}
        {{ <lastmod>2005-01-01</lastmod> }}
        {{ <changefreq>monthly</changefreq> }}
        {{ <priority>0.8</priority> }}
    {{ </url> }}
{{ </urlset> }}

But I get an error: "message":"Unexpected token

how can i make xml go into twig...? sorry I am new to twig

Upvotes: 0

Views: 1495

Answers (1)

ravenixx
ravenixx

Reputation: 311

{{ literal }} is the twig syntax for variable output, so you can't (and why should you?) use that to embrace your xml syntax. Simply write your xml as-is, and insert dynamic content by this twig syntax (and, of course, by {% literal %} for methods and language constructs.

Upvotes: 1

Related Questions