使用.html.twig与.twig有区别吗?

What is the difference between using .html.twig and .twig? Is there some kind of standard, is it framework specific, or is it just user preference?

According to Symfony Documentation

"this is simply an organizational tactic used in case the same resource needs to be rendered as HTML (index.html.twig), XML (index.xml.twig), or any other format."

So sounds like this is user preference, but a good standard to follow either way.

Under Symfony you have the possibility to deliver different formats automatically. So you can create files like test.json.twig, test.xml.twig for example. If you define all that extensions in your controller you can deliver all that formats under one action.

For example:

/**
 * @Route("/hello/{name}.{_format}", defaults={"_format"="html"}, name="_demo_hello")
 * @Template()
 */
public function helloAction($name) {
    return array('name' => $name);
}

Something like this. So you can use the format in your route to define the response format.

The only difference is the behavior of auto escaping.

Imagine you have a var variable containing: <div>I'm happy</div>.

On index.twig, {{ var }} will render <div>I'm happy</div>.

On index.html.twig, {{ var }} will render &lt;div&gt;I&#039;m happy&lt;div&gt;

On index.js.twig, {{ var }} will render \x3Cdiv\x3EI\x27m\x20happy\x3Cdiv\x3E

And so on.

Always use the right extension to avoid any XSS vulnerability, and always use |raw wisely because it overlaps this extension's implicit protection.