10000 symfony-docs/templating.rst at 3.0 · symfony/symfony-docs · GitHub
[go: up one dir, main page]

Skip to content
{"payload":{"allShortcutsEnabled":false,"fileTree":{"":{"items":[{"name":".github","path":".github","contentType":"directory"},{"name":".platform","path":".platform","contentType":"directory"},{"name":"_build","path":"_build","contentType":"directory"},{"name":"_images","path":"_images","contentType":"directory"},{"name":"_includes","path":"_includes","contentType":"directory"},{"name":"assetic","path":"assetic","contentType":"directory"},{"name":"best_practices","path":"best_practices","contentType":"directory"},{"name":"bundles","path":"bundles","contentType":"directory"},{"name":"components","path":"components","contentType":"directory"},{"name":"configuration","path":"configuration","contentType":"directory"},{"name":"console","path":"console","contentType":"directory"},{"name":"contributing","path":"contributing","contentType":"directory"},{"name":"controller","path":"controller","contentType":"directory"},{"name":"create_framework","path":"create_framework","contentType":"directory"},{"name":"debug","path":"debug","contentType":"directory"},{"name":"deployment","path":"deployment","contentType":"directory"},{"name":"doctrine","path":"doctrine","contentType":"directory"},{"name":"email","path":"email","contentType":"directory"},{"name":"event_dispatcher","path":"event_dispatcher","contentType":"directory"},{"name":"form","path":"form","contentType":"directory"},{"name":"frontend","path":"frontend","contentType":"directory"},{"name":"getting_started","path":"getting_started","contentType":"directory"},{"name":"http_cache","path":"http_cache","contentType":"directory"},{"name":"introduction","path":"introduction","contentType":"directory"},{"name":"logging","path":"logging","contentType":"directory"},{"name":"profiler","path":"profiler","contentType":"directory"},{"name":"quick_tour","path":"quick_tour","contentType":"directory"},{"name":"reference","path":"reference","contentType":"directory"},{"name":"request","path":"request","contentType":"directory"},{"name":"routing","path":"routing","contentType":"directory"},{"name":"security","path":"security","contentType":"directory"},{"name":"serializer","path":"serializer","contentType":"directory"},{"name":"service_container","path":"service_container","contentType":"directory"},{"name":"session","path":"session","contentType":"directory"},{"name":"setup","path":"setup","contentType":"directory"},{"name":"templating","path":"templating","contentType":"directory"},{"name":"testing","path":"testing","contentType":"directory"},{"name":"translation","path":"translation","contentType":"directory"},{"name":"validation","path":"validation","contentType":"directory"},{"name":".editorconfig","path":".editorconfig","contentType":"file"},{"name":".gitignore","path":".gitignore","contentType":"file"},{"name":".platform.app.yaml","path":".platform.app.yaml","contentType":"file"},{"name":".travis.yml","path":".travis.yml","contentType":"file"},{"name":"README.markdown","path":"README.markdown","contentType":"file"},{"name":"assetic.rst","path":"assetic.rst","contentType":"file"},{"name":"bundles.rst","path":"bundles.rst","contentType":"file"},{"name":"changelog.rst","path":"changelog.rst","contentType":"file"},{"name":"configuration.rst","path":"configuration.rst","contentType":"file"},{"name":"console.rst","path":"console.rst","contentType":"file"},{"name":"controller.rst","path":"controller.rst","contentType":"file"},{"name":"debug.rst","path":"debug.rst","contentType":"file"},{"name":"deployment.rst","path":"deployment.rst","contentType":"file"},{"name":"doctrine.rst","path":"doctrine.rst","contentType":"file"},{"name":"email.rst","path":"email.rst","contentType":"file"},{"name":"event_dispatcher.rst","path":"event_dispatcher.rst","contentType":"file"},{"name":"expressions.rst","path":"expressions.rst","contentType":"file"},{"name":"forms.rst","path":"forms.rst","contentType":"file"},{"name":"frontend.rst","path":"frontend.rst","contentType":"file"},{"name":"http_cache.rst","path":"http_cache.rst","contentType":"file"},{"name":"index.rst","path":"index.rst","contentType":"file"},{"name":"logging.rst","path":"logging.rst","contentType":"file"},{"name":"page_creation.rst","path":"page_creation.rst","contentType":"file"},{"name":"performance.rst","path":"performance.rst","contentType":"file"},{"name":"profiler.rst","path":"profiler.rst","contentType":"file"},{"name":"request.rst","path":"request.rst","contentType":"file"},{"name":"routing.rst","path":"routing.rst","contentType":"file"},{"name":"security.rst","path":"security.rst","contentType":"file"},{"name":"serializer.rst","path":"serializer.rst","contentType":"file"},{"name":"service_container.rst","path":"service_container.rst","contentType":"file"},{"name":"session.rst","path":"session.rst","contentType":"file"},{"name":"setup.rst","path":"setup.rst","contentType":"file"},{"name":"templating.rst","path":"templating.rst","contentType":"file"},{"name":"testing.rst","path":"testing.rst","contentType":"file"},{"name":"translation.rst","path":"translation.rst","contentType":"file"},{"name":"validation.rst","path":"validation.rst","contentType":"file"}],"totalCount":75}},"fileTreeProcessingTime":4.282341000000001,"foldersToFetch":[],"incompleteFileTree":false,"repo":{"id":521583,"defaultBranch":"7.3","name":"symfony-docs","ownerLogin":"symfony","currentUserCanPush":false,"isFork":false,"isEmpty":false,"createdAt":"2010-02-17T08:43:51.000Z","ownerAvatar":"https://avatars.githubusercontent.com/u/143937?v=4","public":true,"private":false,"isOrgOwned":true},"codeLineWrapEnabled":false,"symbolsExpanded":false,"treeExpanded":true,"refInfo":{"name":"3.0","listCacheKey":"v0:1751870253.0","canEdit":false,"refType":"branch","currentOid":"fdadad00a2d72106512b155f462aab1a86f2eaeb"},"path":"templating.rst","currentUser":null,"blob":{"rawLines":null,"stylingDirectives":null,"colorizedLines":null,"csv":null,"csvError":null,"dependabotInfo":{"showConfigurationBanner":false,"configFilePath":null,"networkDependabotPath":"/symfony/symfony-docs/network/updates","dismissConfigurationNoticePath":"/settings/dismiss-notice/dependabot_configuration_notice","configurationNoticeDismissed":null},"displayName":"templating.rst","displayUrl":"https://github.com/symfony/symfony-docs/blob/3.0/templating.rst?raw=true","headerInfo":{"blobSize":"32.7 KB","deleteTooltip":"You must be signed in to make or propose changes","editTooltip":"You must be signed in to make or propose changes","ghDesktopPath":"https://desktop.github.com","isGitLfs":false,"onBranch":true,"shortPath":"1380fb6","siteNavLoginPath":"/login?return_to=https%3A%2F%2Fgithub.com%2Fsymfony%2Fsymfony-docs%2Fblob%2F3.0%2Ftemplating.rst","isCSV":false,"isRichtext":true,"toc":[{"level":2,"text":"Creating and Using Templates","anchor":"creating-and-using-templates","htmlText":"Creating and Using Templates"},{"level":3,"text":"Templates","anchor":"templates","htmlText":"Templates"},{"level":4,"text":"Twig Template Caching","anchor":"twig-template-caching","htmlText":"Twig Template Caching"},{"level":3,"text":"Template Inheritance and Layouts","anchor":"template-inheritance-and-layouts","htmlText":"Template Inheritance and Layouts"},{"level":3,"text":"Template Naming and Locations","anchor":"template-naming-and-locations","htmlText":"Template Naming and Locations"},{"level":4,"text":"Referencing Templates in a Bundle","anchor":"referencing-templates-in-a-bundle","htmlText":"Referencing Templates in a Bundle"},{"level":4,"text":"Template Suffix","anchor":"template-suffix","htmlText":"Template Suffix"},{"level":3,"text":"Tags and Helpers","anchor":"tags-and-helpers","htmlText":"Tags and Helpers"},{"level":4,"text":"Including other Templates","anchor":"including-other-templates","htmlText":"Including other Templates"},{"level":4,"text":"Linking to Pages","anchor":"linking-to-pages","htmlText":"Linking to Pages"},{"level":4,"text":"Linking to Assets","anchor":"linking-to-assets","htmlText":"Linking to Assets"},{"level":3,"text":"Including Stylesheets and JavaScripts in Twig","anchor":"including-stylesheets-and-javascripts-in-twig","htmlText":"Including Stylesheets and JavaScripts in Twig"},{"level":3,"text":"Referencing the Request, User or Session","anchor":"referencing-the-request-user-or-session","htmlText":"Referencing the Request, User or Session"},{"level":3,"text":"Output Escaping","anchor":"output-escaping","htmlText":"Output Escaping"},{"level":3,"text":"Final Thoughts","anchor":"final-thoughts","htmlText":"Final Thoughts"},{"level":3,"text":"Keep Going!","anchor":"keep-going","htmlText":"Keep Going!"},{"level":3,"text":"Learn more","anchor":"learn-more","htmlText":"Learn more"}],"lineInfo":{"truncatedLoc":"971","truncatedSloc":"716"},"mode":"file"},"image":false,"isCodeownersFile":null,"isPlain":false,"isValidLegacyIssueTemplate":false,"issueTemplate":null,"discussionTemplate":null,"language":"reStructuredText","languageID":419,"large":false,"planSupportInfo":{"repoIsFork":null,"repoOwnedByCurrentUser":null,"requestFullPath":"/symfony/symfony-docs/blob/3.0/templating.rst","showFreeOrgGatedFeatureMessage":null,"showPlanSupportBanner":null,"upgradeDataAttributes":null,"upgradePath":null},"publishBannersInfo":{"dismissActionNoticePath":"/settings/dismiss-notice/publish_action_from_dockerfile","releasePath":"/symfony/symfony-docs/releases/new?marketplace=true","showPublishActionBanner":false},"rawBlobUrl":"https://github.com/symfony/symfony-docs/raw/refs/heads/3.0/templating.rst","renderImageOrRaw":false,"richText":"\u003carticle class=\"markdown-body entry-content container-lg\" itemprop=\"text\"\u003e\u003cpre\u003e.. index::\n single: Templating\n\n\u003c/pre\u003e\n\u003ca name=\"user-content-creating-and-using-templates\"\u003e\u003c/a\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch2 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eCreating and Using Templates\u003c/h2\u003e\u003ca id=\"user-content-creating-and-using-templates\" class=\"anchor\" aria-label=\"Permalink: Creating and Using Templates\" href=\"#creating-and-using-templates\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eAs you know, the \u003ca href=\"#id1\"\u003e\u003cspan id=\"user-content-id2\"\u003e:doc:`controller \u0026lt;/controller\u0026gt;`\u003c/span\u003e\u003c/a\u003e is responsible for\nhandling each request that comes into a Symfony application. In reality,\nthe controller delegates most of the heavy work to other places so that\ncode can be tested and reused. When a controller needs to generate HTML,\nCSS or any other content, it hands the work off to the templating engine.\nIn this chapter, you'll learn how to write powerful templates that can be\nused to return content to the user, populate email bodies, and more. You'll\nlearn shortcuts, clever ways to extend templates and how to reuse template\ncode.\u003c/p\u003e\n\u003cdiv dir=\"auto\"\u003e\n\u003cp dir=\"auto\"\u003eNote\u003c/p\u003e\n\u003cp dir=\"auto\"\u003eHow to render templates is covered in the\n\u003ca href=\"#id3\"\u003e\u003cspan id=\"user-content-id4\"\u003e:ref:`controller \u0026lt;controller-rendering-templates\u0026gt;`\u003c/span\u003e\u003c/a\u003e article.\u003c/p\u003e\n\u003c/div\u003e\n\u003cpre\u003e.. index::\n single: Templating; What is a template?\n\n\u003c/pre\u003e\n\u003ca name=\"user-content-templates\"\u003e\u003c/a\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch3 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eTemplates\u003c/h3\u003e\u003ca id=\"user-content-templates\" class=\"anchor\" aria-label=\"Permalink: Templates\" href=\"#templates\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eA template is simply a text file that can generate any text-based format\n(HTML, XML, CSV, LaTeX ...). The most familiar type of template is a \u003cem\u003ePHP\u003c/em\u003e\ntemplate - a text file parsed by PHP that contains a mix of text and PHP code:\u003c/p\u003e\n\u003cdiv class=\"highlight highlight-text-html-php notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"\u0026lt;!DOCTYPE html\u0026gt;\n\u0026lt;html\u0026gt;\n \u0026lt;head\u0026gt;\n \u0026lt;title\u0026gt;Welcome to Symfony!\u0026lt;/title\u0026gt;\n \u0026lt;/head\u0026gt;\n \u0026lt;body\u0026gt;\n \u0026lt;h1\u0026gt;\u0026lt;?php echo $page_title ?\u0026gt;\u0026lt;/h1\u0026gt;\n\n \u0026lt;ul id=\u0026quot;navigation\u0026quot;\u0026gt;\n \u0026lt;?php foreach ($navigation as $item): ?\u0026gt;\n \u0026lt;li\u0026gt;\n \u0026lt;a href=\u0026quot;\u0026lt;?php echo $item-\u0026gt;getHref() ?\u0026gt;\u0026quot;\u0026gt;\n \u0026lt;?php echo $item-\u0026gt;getCaption() ?\u0026gt;\n \u0026lt;/a\u0026gt;\n \u0026lt;/li\u0026gt;\n \u0026lt;?php endforeach ?\u0026gt;\n \u0026lt;/ul\u0026gt;\n \u0026lt;/body\u0026gt;\n\u0026lt;/html\u0026gt;\"\u003e\u003cpre\u003e\u0026lt;!\u003cspan class=\"pl-c1\"\u003eDOCTYPE\u003c/span\u003e html\u0026gt;\n\u0026lt;html\u0026gt;\n \u0026lt;head\u0026gt;\n \u0026lt;title\u0026gt;Welcome to Symfony!\u0026lt;/title\u0026gt;\n \u0026lt;/head\u0026gt;\n \u0026lt;body\u0026gt;\n \u0026lt;h1\u0026gt;\u003cspan class=\"pl-ent\"\u003e\u0026lt;?php\u003c/span\u003e \u003cspan class=\"pl-k\"\u003eecho\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003e\u003cspan class=\"pl-c1\"\u003e$\u003c/span\u003epage_title\u003c/span\u003e \u003cspan class=\"pl-ent\"\u003e?\u0026gt;\u003c/span\u003e\u0026lt;/h1\u0026gt;\n\n \u0026lt;ul id=\"navigation\"\u0026gt;\n \u003cspan class=\"pl-ent\"\u003e\u0026lt;?php\u003c/span\u003e \u003cspan class=\"pl-k\"\u003eforeach\u003c/span\u003e (\u003cspan class=\"pl-s1\"\u003e\u003cspan class=\"pl-c1\"\u003e$\u003c/span\u003enavigation\u003c/span\u003e \u003cspan class=\"pl-k\"\u003eas\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003e\u003cspan class=\"pl-c1\"\u003e$\u003c/span\u003eitem\u003c/span\u003e): \u003cspan class=\"pl-ent\"\u003e?\u0026gt;\u003c/span\u003e\n \u0026lt;li\u0026gt;\n \u0026lt;a href=\"\u003cspan class=\"pl-ent\"\u003e\u0026lt;?php\u003c/span\u003e \u003cspan class=\"pl-k\"\u003eecho\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003e\u003cspan class=\"pl-c1\"\u003e$\u003c/span\u003eitem\u003c/span\u003e-\u0026gt;\u003cspan class=\"pl-en\"\u003egetHref\u003c/span\u003e() \u003cspan class=\"pl-ent\"\u003e?\u0026gt;\u003c/span\u003e\"\u0026gt;\n \u003cspan class=\"pl-ent\"\u003e\u0026lt;?php\u003c/span\u003e \u003cspan class=\"pl-k\"\u003eecho\u003c/span\u003e \u003cspan class=\"pl-s1\"\u003e\u003cspan class=\"pl-c1\"\u003e$\u003c/span\u003eitem\u003c/span\u003e-\u0026gt;\u003cspan class=\"pl-en\"\u003egetCaption\u003c/span\u003e() \u003cspan class=\"pl-ent\"\u003e?\u0026gt;\u003c/span\u003e\n \u0026lt;/a\u0026gt;\n \u0026lt;/li\u0026gt;\n \u003cspan class=\"pl-ent\"\u003e\u0026lt;?php\u003c/span\u003e \u003cspan class=\"pl-k\"\u003eendforeach\u003c/span\u003e \u003cspan class=\"pl-ent\"\u003e?\u0026gt;\u003c/span\u003e\n \u0026lt;/ul\u0026gt;\n \u0026lt;/body\u0026gt;\n\u0026lt;/html\u0026gt;\u003c/pre\u003e\u003c/div\u003e\n\u003cpre\u003e.. index:: Twig; Introduction\n\n\u003c/pre\u003e\n\u003cp dir=\"auto\"\u003eBut Symfony packages an even more powerful templating language called \u003ca href=\"http://twig.sensiolabs.org\" rel=\"nofollow\"\u003eTwig\u003c/a\u003e.\nTwig allows you to write concise, readable templates that are more friendly\nto web designers and, in several ways, more powerful than PHP templates:\u003c/p\u003e\n\u003cpre lang=\"html+twig\"\u003e\u0026lt;!DOCTYPE html\u0026gt;\n\u0026lt;html\u0026gt;\n \u0026lt;head\u0026gt;\n \u0026lt;title\u0026gt;Welcome to Symfony!\u0026lt;/title\u0026gt;\n \u0026lt;/head\u0026gt;\n \u0026lt;body\u0026gt;\n \u0026lt;h1\u0026gt;{{ page_title }}\u0026lt;/h1\u0026gt;\n\n \u0026lt;ul id=\"navigation\"\u0026gt;\n {% for item in navigation %}\n \u0026lt;li\u0026gt;\u0026lt;a href=\"{{ item.href }}\"\u0026gt;{{ item.caption }}\u0026lt;/a\u0026gt;\u0026lt;/li\u0026gt;\n {% endfor %}\n \u0026lt;/ul\u0026gt;\n \u0026lt;/body\u0026gt;\n\u0026lt;/html\u0026gt;\n\u003c/pre\u003e\n\u003cp dir=\"auto\"\u003eTwig defines three types of special syntax:\u003c/p\u003e\n\u003cdl\u003e\n\u003cdt\u003e\u003ccode\u003e{{ ... }}\u003c/code\u003e\u003c/dt\u003e\n\u003cdd\u003e\"Says something\": prints a variable or the result of an expression to the\ntemplate.\u003c/dd\u003e\n\u003cdt\u003e\u003ccode\u003e{% ... %}\u003c/code\u003e\u003c/dt\u003e\n\u003cdd\u003e\"Does something\": a \u003cstrong\u003etag\u003c/strong\u003e that controls the logic of the template; it is\nused to execute statements such as for-loops for example.\u003c/dd\u003e\n\u003cdt\u003e\u003ccode\u003e{# ... #}\u003c/code\u003e\u003c/dt\u003e\n\u003cdd\u003e\"Comment something\": it's the equivalent of the PHP \u003ccode\u003e/* comment */\u003c/code\u003e syntax.\nIt's used to add single or multi-line comments. The content of the comments\nisn't included in the rendered pages.\u003c/dd\u003e\n\u003c/dl\u003e\n\u003cp dir=\"auto\"\u003eTwig also contains \u003cstrong\u003efilters\u003c/strong\u003e, which modify content before being rendered.\nThe following makes the \u003ccode\u003etitle\u003c/code\u003e variable all uppercase before rendering\nit:\u003c/p\u003e\n\u003cdiv class=\"highlight highlight-text-html-twig notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"{{ title|upper }}\"\u003e\u003cpre\u003e{{ \u003cspan class=\"pl-smi\"\u003etitle\u003c/span\u003e\u003cspan class=\"pl-k\"\u003e|\u003c/span\u003e\u003cspan class=\"pl-c1\"\u003eupper\u003c/span\u003e }}\u003c/pre\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eTwig comes with a long list of \u003ca href=\"http://twig.sensiolabs.org/doc/tags/index.html\" rel=\"nofollow\"\u003etags\u003c/a\u003e, \u003ca href=\"http://twig.sensiolabs.org/doc/filters/index.html\" rel=\"nofollow\"\u003efilters\u003c/a\u003e and \u003ca href=\"http://twig.sensiolabs.org/doc/functions/index.html\" rel=\"nofollow\"\u003efunctions\u003c/a\u003e that are available\nby default. You can even add your own \u003cem\u003ecustom\u003c/em\u003e filters, functions (and more) via\na \u003ca href=\"#id5\"\u003e\u003cspan id=\"user-content-id6\"\u003e:doc:`Twig Extension \u0026lt;/templating/twig_extension\u0026gt;`\u003c/span\u003e\u003c/a\u003e.\u003c/p\u003e\n\u003cp dir=\"auto\"\u003eTwig code will look similar to PHP code, with subtle, nice differences. The following\nexample uses a standard \u003ccode\u003efor\u003c/code\u003e tag and the \u003ccode\u003ecycle\u003c/code\u003e function to print ten div tags,\nwith alternating \u003ccode\u003eodd\u003c/code\u003e, \u003ccode\u003eeven\u003c/code\u003e classes:\u003c/p\u003e\n\u003cpre lang=\"html+twig\"\u003e{% for i in 1..10 %}\n \u0026lt;div class=\"{{ cycle(['even', 'odd'], i) }}\"\u0026gt;\n \u0026lt;!-- some HTML here --\u0026gt;\n \u0026lt;/div\u0026gt;\n{% endfor %}\n\u003c/pre\u003e\n\u003cp dir=\"auto\"\u003eThroughout this chapter, template examples will be shown in both Twig and PHP.\u003c/p\u003e\n\u003cdiv dir=\"auto\"\u003e\n\u003cp dir=\"auto\"\u003eWhy Twig?\u003c/p\u003e\n\u003cp dir=\"auto\"\u003eTwig templates are meant to be simple and won't process PHP tags. This\nis by design: the Twig template system is meant to express presentation,\nnot program logic. The more you use Twig, the more you'll appreciate\nand benefit from this distinction. And of course, you'll be loved by\nweb designers everywhere.\u003c/p\u003e\n\u003cp dir=\"auto\"\u003eTwig can also do things that PHP can't, such as whitespace control,\nsandboxing, automatic HTML escaping, manual contextual output escaping,\nand the inclusion of custom functions and filters that only affect templates.\nTwig contains little features that make writing templates easier and more concise.\nTake the following example, which combines a loop with a logical \u003ccode\u003eif\u003c/code\u003e\nstatement:\u003c/p\u003e\n\u003cpre lang=\"html+twig\"\u003e\u0026lt;ul\u0026gt;\n {% for user in users if user.active %}\n \u0026lt;li\u0026gt;{{ user.username }}\u0026lt;/li\u0026gt;\n {% else %}\n \u0026lt;li\u0026gt;No users found\u0026lt;/li\u0026gt;\n {% endfor %}\n\u0026lt;/ul\u0026gt;\n\u003c/pre\u003e\n\u003c/div\u003e\n\u003cpre\u003e.. index::\n pair: Twig; Cache\n\n\u003c/pre\u003e\n\u003ca name=\"user-content-twig-template-caching\"\u003e\u003c/a\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch4 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eTwig Template Caching\u003c/h4\u003e\u003ca id=\"user-content-twig-template-caching\" class=\"anchor\" aria-label=\"Permalink: Twig Template Caching\" href=\"#twig-template-caching\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eTwig is fast because each template is compiled to a native PHP class and cached.\nBut don't worry: this happens automatically and doesn't require \u003cem\u003eyou\u003c/em\u003e to do anything.\nAnd while you're developing, Twig is smart enough to re-compile you templates after\nyou make any changes. That means Twig is fast in production, but easy to use while\ndeveloping.\u003c/p\u003e\n\u003cpre\u003e.. index::\n single: Templating; Inheritance\n\n\u003c/pre\u003e\n\u003ca name=\"user-content-template-inheritance-and-layouts\"\u003e\u003c/a\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch3 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eTemplate Inheritance and Layouts\u003c/h3\u003e\u003ca id=\"user-content-template-inheritance-and-layouts\" class=\"anchor\" aria-label=\"Permalink: Template Inheritance and Layouts\" href=\"#template-inheritance-and-layouts\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eMore often than not, templates in a project share common elements, like the\nheader, footer, sidebar or more. In Symfony, this problem is thought about\ndifferently: a template can be decorated by another one. This works\nexactly the same as PHP classes: template inheritance allows you to build\na base \"layout\" template that contains all the common elements of your site\ndefined as \u003cstrong\u003eblocks\u003c/strong\u003e (think \"PHP class with base methods\"). A child template\ncan extend the base layout and override any of its blocks (think \"PHP subclass\nthat overrides certain methods of its parent class\").\u003c/p\u003e\n\u003cp dir=\"auto\"\u003eFirst, build a base layout file:\u003c/p\u003e\n\u003cpre\u003e.. configuration-block::\n\n .. code-block:: html+twig\n\n {# app/Resources/views/base.html.twig #}\n \u0026lt;!DOCTYPE html\u0026gt;\n \u0026lt;html\u0026gt;\n \u0026lt;head\u0026gt;\n \u0026lt;meta charset=\"UTF-8\"\u0026gt;\n \u0026lt;title\u0026gt;{% block title %}Test Application{% endblock %}\u0026lt;/title\u0026gt;\n \u0026lt;/head\u0026gt;\n \u0026lt;body\u0026gt;\n \u0026lt;div id=\"sidebar\"\u0026gt;\n {% block sidebar %}\n \u0026lt;ul\u0026gt;\n \u0026lt;li\u0026gt;\u0026lt;a href=\"/\"\u0026gt;Home\u0026lt;/a\u0026gt;\u0026lt;/li\u0026gt;\n \u0026lt;li\u0026gt;\u0026lt;a href=\"/blog\"\u0026gt;Blog\u0026lt;/a\u0026gt;\u0026lt;/li\u0026gt;\n \u0026lt;/ul\u0026gt;\n {% endblock %}\n \u0026lt;/div\u0026gt;\n\n \u0026lt;div id=\"content\"\u0026gt;\n {% block body %}{% endblock %}\n \u0026lt;/div\u0026gt;\n \u0026lt;/body\u0026gt;\n \u0026lt;/html\u0026gt;\n\n .. code-block:: html+php\n\n \u0026lt;!-- app/Resources/views/base.html.php --\u0026gt;\n \u0026lt;!DOCTYPE html\u0026gt;\n \u0026lt;html\u0026gt;\n \u0026lt;head\u0026gt;\n \u0026lt;meta charset=\"UTF-8\"\u0026gt;\n \u0026lt;title\u0026gt;\u0026lt;?php $view['slots']-\u0026gt;output('title', 'Test Application') ?\u0026gt;\u0026lt;/title\u0026gt;\n \u0026lt;/head\u0026gt;\n \u0026lt;body\u0026gt;\n \u0026lt;div id=\"sidebar\"\u0026gt;\n \u0026lt;?php if ($view['slots']-\u0026gt;has('sidebar')): ?\u0026gt;\n \u0026lt;?php $view['slots']-\u0026gt;output('sidebar') ?\u0026gt;\n \u0026lt;?php else: ?\u0026gt;\n \u0026lt;ul\u0026gt;\n \u0026lt;li\u0026gt;\u0026lt;a href=\"/\"\u0026gt;Home\u0026lt;/a\u0026gt;\u0026lt;/li\u0026gt;\n \u0026lt;li\u0026gt;\u0026lt;a href=\"/blog\"\u0026gt;Blog\u0026lt;/a\u0026gt;\u0026lt;/li\u0026gt;\n \u0026lt;/ul\u0026gt;\n \u0026lt;?php endif ?\u0026gt;\n \u0026lt;/div\u0026gt;\n\n \u0026lt;div id=\"content\"\u0026gt;\n \u0026lt;?php $view['slots']-\u0026gt;output('body') ?\u0026gt;\n \u0026lt;/div\u0026gt;\n \u0026lt;/body\u0026gt;\n \u0026lt;/html\u0026gt;\n\n\u003c/pre\u003e\n\u003cdiv dir=\"auto\"\u003e\n\u003cp dir=\"auto\"\u003eNote\u003c/p\u003e\n\u003cp dir=\"auto\"\u003eThough the discussion about template inheritance will be in terms of Twig,\nthe philosophy is the same between Twig and PHP templates.\u003c/p\u003e\n\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eThis template defines the base HTML skeleton document of a simple two-column\npage. In this example, three \u003ccode\u003e{% block %}\u003c/code\u003e areas are defined (\u003ccode\u003etitle\u003c/code\u003e,\n\u003ccode\u003esidebar\u003c/code\u003e and \u003ccode\u003ebody\u003c/code\u003e). Each block may be overridden by a child template\nor left with its default implementation. This template could also be rendered\ndirectly. In that case the \u003ccode\u003etitle\u003c/code\u003e, \u003ccode\u003esidebar\u003c/code\u003e and \u003ccode\u003ebody\u003c/code\u003e blocks would\nsimply retain the default values used in this template.\u003c/p\u003e\n\u003cp dir=\"auto\"\u003eA child template might look like this:\u003c/p\u003e\n\u003cpre\u003e.. configuration-block::\n\n .. code-block:: html+twig\n\n {# app/Resources/views/blog/index.html.twig #}\n {% extends 'base.html.twig' %}\n\n {% block title %}My cool blog posts{% endblock %}\n\n {% block body %}\n {% for entry in blog_entries %}\n \u0026lt;h2\u0026gt;{{ entry.title }}\u0026lt;/h2\u0026gt;\n \u0026lt;p\u0026gt;{{ entry.body }}\u0026lt;/p\u0026gt;\n {% endfor %}\n {% endblock %}\n\n .. code-block:: html+php\n\n \u0026lt;!-- app/Resources/views/blog/index.html.php --\u0026gt;\n \u0026lt;?php $view-\u0026gt;extend('base.html.php') ?\u0026gt;\n\n \u0026lt;?php $view['slots']-\u0026gt;set('title', 'My cool blog posts') ?\u0026gt;\n\n \u0026lt;?php $view['slots']-\u0026gt;start('body') ?\u0026gt;\n \u0026lt;?php foreach ($blog_entries as $entry): ?\u0026gt;\n \u0026lt;h2\u0026gt;\u0026lt;?php echo $entry-\u0026gt;getTitle() ?\u0026gt;\u0026lt;/h2\u0026gt;\n \u0026lt;p\u0026gt;\u0026lt;?php echo $entry-\u0026gt;getBody() ?\u0026gt;\u0026lt;/p\u0026gt;\n \u0026lt;?php endforeach ?\u0026gt;\n \u0026lt;?php $view['slots']-\u0026gt;stop() ?\u0026gt;\n\n\u003c/pre\u003e\n\u003cdiv dir=\"auto\"\u003e\n\u003cp dir=\"auto\"\u003eNote\u003c/p\u003e\n\u003cp dir=\"auto\"\u003eThe parent template is identified by a special string syntax\n(\u003ccode\u003ebase.html.twig\u003c/code\u003e). This path is relative to the \u003ccode\u003eapp/Resources/views\u003c/code\u003e\ndirectory of the project. You could also use the logical name equivalent:\n\u003ccode\u003e::base.html.twig\u003c/code\u003e. This naming convention is explained fully in\n\u003ca href=\"#id7\"\u003e\u003cspan id=\"user-content-id8\"\u003e:ref:`template-naming-locations`\u003c/span\u003e\u003c/a\u003e.\u003c/p\u003e\n\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eThe key to template inheritance is the \u003ccode\u003e{% extends %}\u003c/code\u003e tag. This tells\nthe templating engine to first evaluate the base template, which sets up\nthe layout and defines several blocks. The child template is then rendered,\nat which point the \u003ccode\u003etitle\u003c/code\u003e and \u003ccode\u003ebody\u003c/code\u003e blocks of the parent are replaced\nby those from the child. Depending on the value of \u003ccode\u003eblog_entries\u003c/code\u003e, the\noutput might look like this:\u003c/p\u003e\n\u003cdiv class=\"highlight highlight-text-html-basic notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"\u0026lt;!DOCTYPE html\u0026gt;\n\u0026lt;html\u0026gt;\n \u0026lt;head\u0026gt;\n \u0026lt;meta charset=\u0026quot;UTF-8\u0026quot;\u0026gt;\n \u0026lt;title\u0026gt;My cool blog posts\u0026lt;/title\u0026gt;\n \u0026lt;/head\u0026gt;\n \u0026lt;body\u0026gt;\n \u0026lt;div id=\u0026quot;sidebar\u0026quot;\u0026gt;\n \u0026lt;ul\u0026gt;\n \u0026lt;li\u0026gt;\u0026lt;a href=\u0026quot;/\u0026quot;\u0026gt;Home\u0026lt;/a\u0026gt;\u0026lt;/li\u0026gt;\n \u0026lt;li\u0026gt;\u0026lt;a href=\u0026quot;/blog\u0026quot;\u0026gt;Blog\u0026lt;/a\u0026gt;\u0026lt;/li\u0026gt;\n \u0026lt;/ul\u0026gt;\n \u0026lt;/div\u0026gt;\n\n \u0026lt;div id=\u0026quot;content\u0026quot;\u0026gt;\n \u0026lt;h2\u0026gt;My first post\u0026lt;/h2\u0026gt;\n \u0026lt;p\u0026gt;The body of the first post.\u0026lt;/p\u0026gt;\n\n \u0026lt;h2\u0026gt;Another post\u0026lt;/h2\u0026gt;\n \u0026lt;p\u0026gt;The body of the second post.\u0026lt;/p\u0026gt;\n \u0026lt;/div\u0026gt;\n \u0026lt;/body\u0026gt;\n\u0026lt;/html\u0026gt;\"\u003e\u003cpre\u003e\u003cspan class=\"pl-c1\"\u003e\u0026lt;!DOCTYPE html\u003cspan class=\"pl-kos\"\u003e\u0026gt;\u003c/span\u003e\u003c/span\u003e\n\u003cspan class=\"pl-kos\"\u003e\u0026lt;\u003c/span\u003e\u003cspan class=\"pl-ent\"\u003ehtml\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e\u0026gt;\u003c/span\u003e\n \u003cspan class=\"pl-kos\"\u003e\u0026lt;\u003c/span\u003e\u003cspan class=\"pl-ent\"\u003ehead\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e\u0026gt;\u003c/span\u003e\n \u003cspan class=\"pl-kos\"\u003e\u0026lt;\u003c/span\u003e\u003cspan class=\"pl-ent\"\u003emeta\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003echarset\u003c/span\u003e=\"\u003cspan class=\"pl-s\"\u003eUTF-8\u003c/span\u003e\"\u003cspan class=\"pl-kos\"\u003e\u0026gt;\u003c/span\u003e\n \u003cspan class=\"pl-kos\"\u003e\u0026lt;\u003c/span\u003e\u003cspan class=\"pl-ent\"\u003etitle\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e\u0026gt;\u003c/span\u003eMy cool blog posts\u003cspan class=\"pl-kos\"\u003e\u0026lt;/\u003c/span\u003e\u003cspan class=\"pl-ent\"\u003etitle\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e\u0026gt;\u003c/span\u003e\n \u003cspan class=\"pl-kos\"\u003e\u0026lt;/\u003c/span\u003e\u003cspan class=\"pl-ent\"\u003ehead\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e\u0026gt;\u003c/span\u003e\n \u003cspan class=\"pl-kos\"\u003e\u0026lt;\u003c/span\u003e\u003cspan class=\"pl-ent\"\u003ebody\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e\u0026gt;\u003c/span\u003e\n \u003cspan class=\"pl-kos\"\u003e\u0026lt;\u003c/span\u003e\u003cspan class=\"pl-ent\"\u003ediv\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003eid\u003c/span\u003e=\"\u003cspan class=\"pl-s\"\u003esidebar\u003c/span\u003e\"\u003cspan class=\"pl-kos\"\u003e\u0026gt;\u003c/span\u003e\n \u003cspan class=\"pl-kos\"\u003e\u0026lt;\u003c/span\u003e\u003cspan class=\"pl-ent\"\u003eul\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e\u0026gt;\u003c/span\u003e\n \u003cspan class=\"pl-kos\"\u003e\u0026lt;\u003c/span\u003e\u003cspan class=\"pl-ent\"\u003eli\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e\u0026gt;\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e\u0026lt;\u003c/span\u003e\u003cspan class=\"pl-ent\"\u003ea\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003ehref\u003c/span\u003e=\"\u003cspan class=\"pl-s\"\u003e/\u003c/span\u003e\"\u003cspan class=\"pl-kos\"\u003e\u0026gt;\u003c/span\u003eHome\u003cspan class=\"pl-kos\"\u003e\u0026lt;/\u003c/span\u003e\u003cspan class=\"pl-ent\"\u003ea\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e\u0026gt;\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e\u0026lt;/\u003c/span\u003e\u003cspan class=\"pl-ent\"\u003eli\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e\u0026gt;\u003c/span\u003e\n \u003cspan class=\"pl-kos\"\u003e\u0026lt;\u003c/span\u003e\u003cspan class=\"pl-ent\"\u003eli\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e\u0026gt;\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e\u0026lt;\u003c/span\u003e\u003cspan class=\"pl-ent\"\u003ea\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003ehref\u003c/span\u003e=\"\u003cspan class=\"pl-s\"\u003e/blog\u003c/span\u003e\"\u003cspan class=\"pl-kos\"\u003e\u0026gt;\u003c/span\u003eBlog\u003cspan class=\"pl-kos\"\u003e\u0026lt;/\u003c/span\u003e\u003cspan class=\"pl-ent\"\u003ea\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e\u0026gt;\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e\u0026lt;/\u003c/span\u003e\u003cspan class=\"pl-ent\"\u003eli\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e\u0026gt;\u003c/span\u003e\n \u003cspan class=\"pl-kos\"\u003e\u0026lt;/\u003c/span\u003e\u003cspan class=\"pl-ent\"\u003eul\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e\u0026gt;\u003c/span\u003e\n \u003cspan class=\"pl-kos\"\u003e\u0026lt;/\u003c/span\u003e\u003cspan class=\"pl-ent\"\u003ediv\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e\u0026gt;\u003c/span\u003e\n\n \u003cspan class=\"pl-kos\"\u003e\u0026lt;\u003c/span\u003e\u003cspan class=\"pl-ent\"\u003ediv\u003c/span\u003e \u003cspan class=\"pl-c1\"\u003eid\u003c/span\u003e=\"\u003cspan class=\"pl-s\"\u003econtent\u003c/span\u003e\"\u003cspan class=\"pl-kos\"\u003e\u0026gt;\u003c/span\u003e\n \u003cspan class=\"pl-kos\"\u003e\u0026lt;\u003c/span\u003e\u003cspan class=\"pl-ent\"\u003eh2\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e\u0026gt;\u003c/span\u003eMy first post\u003cspan class=\"pl-kos\"\u003e\u0026lt;/\u003c/span\u003e\u003cspan class=\"pl-ent\"\u003eh2\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e\u0026gt;\u003c/span\u003e\n \u003cspan class=\"pl-kos\"\u003e\u0026lt;\u003c/span\u003e\u003cspan class=\"pl-ent\"\u003ep\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e\u0026gt;\u003c/span\u003eThe body of the first post.\u003cspan class=\"pl-kos\"\u003e\u0026lt;/\u003c/span\u003e\u003cspan class=\"pl-ent\"\u003ep\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e\u0026gt;\u003c/span\u003e\n\n \u003cspan class=\"pl-kos\"\u003e\u0026lt;\u003c/span\u003e\u003cspan class=\"pl-ent\"\u003eh2\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e\u0026gt;\u003c/span\u003eAnother post\u003cspan class=\"pl-kos\"\u003e\u0026lt;/\u003c/span\u003e\u003cspan class=\"pl-ent\"\u003eh2\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e\u0026gt;\u003c/span\u003e\n \u003cspan class=\"pl-kos\"\u003e\u0026lt;\u003c/span\u003e\u003cspan class=\"pl-ent\"\u003ep\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e\u0026gt;\u003c/span\u003eThe body of the second post.\u003cspan class=\"pl-kos\"\u003e\u0026lt;/\u003c/span\u003e\u003cspan class=\"pl-ent\"\u003ep\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e\u0026gt;\u003c/span\u003e\n \u003cspan class=\"pl-kos\"\u003e\u0026lt;/\u003c/span\u003e\u003cspan class=\"pl-ent\"\u003ediv\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e\u0026gt;\u003c/span\u003e\n \u003cspan class=\"pl-kos\"\u003e\u0026lt;/\u003c/span\u003e\u003cspan class=\"pl-ent\"\u003ebody\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e\u0026gt;\u003c/span\u003e\n\u003cspan class=\"pl-kos\"\u003e\u0026lt;/\u003c/span\u003e\u003cspan class=\"pl-ent\"\u003ehtml\u003c/span\u003e\u003cspan class=\"pl-kos\"\u003e\u0026gt;\u003c/span\u003e\u003c/pre\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eNotice that since the child template didn't define a \u003ccode\u003esidebar\u003c/code\u003e block, the\nvalue from the parent template is used instead. Content within a \u003ccode\u003e{% block %}\u003c/code\u003e\ntag in a parent template is always used by default.\u003c/p\u003e\n\u003cdiv dir=\"auto\"\u003e\n\u003cp dir=\"auto\"\u003eTip\u003c/p\u003e\n\u003cp dir=\"auto\"\u003eYou can use as many levels of inheritance as you want! See \u003ca href=\"#id9\"\u003e\u003cspan id=\"user-content-id10\"\u003e:doc:`/templating/inheritance`\u003c/span\u003e\u003c/a\u003e\nfor more info.\u003c/p\u003e\n\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eWhen working with template inheritance, here are some tips to keep in mind:\u003c/p\u003e\n\u003cul dir=\"auto\"\u003e\n\u003cli\u003e\u003cp dir=\"auto\"\u003eIf you use \u003ccode\u003e{% extends %}\u003c/code\u003e in a template, it must be the first tag in\nthat template;\u003c/p\u003e\n\u003c/li\u003e\n\u003cli\u003e\u003cp dir=\"auto\"\u003eThe more \u003ccode\u003e{% block %}\u003c/code\u003e tags you have in your base templates, the better.\nRemember, child templates don't have to define all parent blocks, so create\nas many blocks in your base templates as you want and give each a sensible\ndefault. The more blocks your base templates have, the more flexible your\nlayout will be;\u003c/p\u003e\n\u003c/li\u003e\n\u003cli\u003e\u003cp dir=\"auto\"\u003eIf you find yourself duplicating content in a number of templates, it probably\nmeans you should move that content to a \u003ccode\u003e{% block %}\u003c/code\u003e in a parent template.\nIn some cases, a better solution may be to move the content to a new template\nand \u003ccode\u003einclude\u003c/code\u003e it (see \u003ca href=\"#id11\"\u003e\u003cspan id=\"user-content-id12\"\u003e:ref:`including-templates`\u003c/span\u003e\u003c/a\u003e);\u003c/p\u003e\n\u003c/li\u003e\n\u003cli\u003e\u003cp dir=\"auto\"\u003eIf you need to get the content of a block from the parent template, you\ncan use the \u003ccode\u003e{{ parent() }}\u003c/code\u003e function. This is useful if you want to add\nto the contents of a parent block instead of completely overriding it:\u003c/p\u003e\n\u003cpre lang=\"html+twig\"\u003e{% block sidebar %}\n \u0026lt;h3\u0026gt;Table of Contents\u0026lt;/h3\u0026gt;\n\n {# ... #}\n\n {{ parent() }}\n{% endblock %}\n\u003c/pre\u003e\n\u003c/li\u003e\n\u003c/ul\u003e\n\u003cpre\u003e.. index::\n single: Templating; Naming conventions\n single: Templating; File locations\n\n\u003c/pre\u003e\n\u003ca name=\"user-content-template-naming-and-locations\"\u003e\u003c/a\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch3 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eTemplate Naming and Locations\u003c/h3\u003e\u003ca id=\"user-content-template-naming-and-locations\" class=\"anchor\" aria-label=\"Permalink: Template Naming and Locations\" href=\"#template-naming-and-locations\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eBy default, templates can live in two different locations:\u003c/p\u003e\n\u003cdl\u003e\n\u003cdt\u003e\u003ccode\u003eapp/Resources/views/\u003c/code\u003e\u003c/dt\u003e\n\u003cdd\u003eThe application's \u003ccode\u003eviews\u003c/code\u003e directory can contain application-wide base templates\n(i.e. your application's layouts and templates of the application bundle) as\nwell as templates that override third party bundle templates\n(see \u003ca href=\"#id13\"\u003e\u003cspan id=\"user-content-id14\"\u003e:doc:`/templating/overriding`\u003c/span\u003e\u003c/a\u003e).\u003c/dd\u003e\n\u003cdt\u003e\u003ccode\u003evendor/path/to/CoolBundle/Resources/views/\u003c/code\u003e\u003c/dt\u003e\n\u003cdd\u003eEach third party bundle houses its templates in its \u003ccode\u003eResources/views/\u003c/code\u003e\ndirectory (and subdirectories). When you plan to share your bundle, you should\nput the templates in the bundle instead of the \u003ccode\u003eapp/\u003c/code\u003e directory.\u003c/dd\u003e\n\u003c/dl\u003e\n\u003cp dir=\"auto\"\u003eMost of the templates you'll use live in the \u003ccode\u003eapp/Resources/views/\u003c/code\u003e\ndirectory. The path you'll use will be relative to this directory. For example,\nto render/extend \u003ccode\u003eapp/Resources/views/base.html.twig\u003c/code\u003e, you'll use the\n\u003ccode\u003ebase.html.twig\u003c/code\u003e path and to render/extend\n\u003ccode\u003eapp/Resources/views/blog/index.html.twig\u003c/code\u003e, you'll use the\n\u003ccode\u003eblog/index.html.twig\u003c/code\u003e path.\u003c/p\u003e\n\u003ca name=\"user-content-referencing-templates-in-a-bundle\"\u003e\u003c/a\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch4 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eReferencing Templates in a Bundle\u003c/h4\u003e\u003ca id=\"user-content-referencing-templates-in-a-bundle\" class=\"anchor\" aria-label=\"Permalink: Referencing Templates in a Bundle\" href=\"#referencing-templates-in-a-bundle\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003e\u003cem\u003eIf\u003c/em\u003e you need to refer to a template that lives in a bundle, Symfony uses a \u003cstrong\u003ebundle\u003c/strong\u003e:\u003cstrong\u003edirectory\u003c/strong\u003e:\u003cstrong\u003efilename\u003c/strong\u003e\nstring syntax. This allows for several types of templates, each which lives in a\nspecific location:\u003c/p\u003e\n\u003cul dir=\"auto\"\u003e\n\u003cli\u003e\u003cp dir=\"auto\"\u003e\u003ccode\u003eAcmeBlogBundle:Blog:index.html.twig\u003c/code\u003e: This syntax is used to specify a\ntemplate for a specific page. The three parts of the string, each separated\nby a colon (\u003ccode\u003e:\u003c/code\u003e), mean the following:\u003c/p\u003e\n\u003cul dir=\"auto\"\u003e\n\u003cli\u003e\u003ccode\u003eAcmeBlogBundle\u003c/code\u003e: (\u003cem\u003ebundle\u003c/em\u003e) the template lives inside the AcmeBlogBundle\n(e.g. \u003ccode\u003esrc/Acme/BlogBundle\u003c/code\u003e);\u003c/li\u003e\n\u003cli\u003e\u003ccode\u003eBlog\u003c/code\u003e: (\u003cem\u003edirectory\u003c/em\u003e) indicates that the template lives inside the\n\u003ccode\u003eBlog\u003c/code\u003e subdirectory of \u003ccode\u003eResources/views\u003c/code\u003e;\u003c/li\u003e\n\u003cli\u003e\u003ccode\u003eindex.html.twig\u003c/code\u003e: (\u003cem\u003efilename\u003c/em\u003e) the actual name of the file is\n\u003ccode\u003eindex.html.twig\u003c/code\u003e.\u003c/li\u003e\n\u003c/ul\u003e\n\u003cp dir=\"auto\"\u003eAssuming that the AcmeBlogBundle lives at \u003ccode\u003esrc/Acme/BlogBundle\u003c/code\u003e, the\nfinal path to the layout would be \u003ccode\u003esrc/Acme/BlogBundle/Resources/views/Blog/index.html.twig\u003c/code\u003e.\u003c/p\u003e\n\u003c/li\u003e\n\u003cli\u003e\u003cp dir=\"auto\"\u003e\u003ccode\u003eAcmeBlogBundle::layout.html.twig\u003c/code\u003e: This syntax refers to a base template\nthat's specific to the AcmeBlogBundle. Since the middle, \"directory\", portion\nis missing (e.g. \u003ccode\u003eBlog\u003c/code\u003e), the template lives at\n\u003ccode\u003eResources/views/layout.html.twig\u003c/code\u003e inside AcmeBlogBundle. Yes, there are 2\ncolons in the middle of the string when the \"controller\" subdirectory part is\nmissing.\u003c/p\u003e\n\u003c/li\u003e\n\u003c/ul\u003e\n\u003cp dir=\"auto\"\u003eIn the \u003ca href=\"#id15\"\u003e\u003cspan id=\"user-content-id16\"\u003e:doc:`/templating/overriding`\u003c/span\u003e\u003c/a\u003e section, you'll find out how each\ntemplate living inside the AcmeBlogBundle, for example, can be overridden\nby placing a template of the same name in the \u003ccode\u003eapp/Resources/AcmeBlogBundle/views/\u003c/code\u003e\ndirectory. This gives the power to override templates from any vendor bundle.\u003c/p\u003e\n\u003cdiv dir=\"auto\"\u003e\n\u003cp dir=\"auto\"\u003eTip\u003c/p\u003e\n\u003cp dir=\"auto\"\u003eHopefully the template naming syntax looks familiar - it's similar to\nthe naming convention used to refer to \u003ca href=\"#id17\"\u003e\u003cspan id=\"user-content-id18\"\u003e:ref:`controller-string-syntax`\u003c/span\u003e\u003c/a\u003e.\u003c/p\u003e\n\u003c/div\u003e\n\u003ca name=\"user-content-template-suffix\"\u003e\u003c/a\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch4 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eTemplate Suffix\u003c/h4\u003e\u003ca id=\"user-content-template-suffix\" class=\"anchor\" aria-label=\"Permalink: Template Suffix\" href=\"#template-suffix\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eEvery template name also has two extensions that specify the \u003cem\u003eformat\u003c/em\u003e and\n\u003cem\u003eengine\u003c/em\u003e for that template.\u003c/p\u003e\n\u003cmarkdown-accessiblity-table\u003e\u003ctable\u003e\n\n\n\n\n\n\u003cthead valign=\"bottom\"\u003e\n\u003ctr\u003e\u003cth\u003eFilename\u003c/th\u003e\n\u003cth\u003eFormat\u003c/th\u003e\n\u003cth\u003eEngine\u003c/th\u003e\n\u003c/tr\u003e\n\u003c/thead\u003e\n\u003ctbody valign=\"top\"\u003e\n\u003ctr\u003e\u003ctd\u003e\u003ccode\u003eblog/index.html.twig\u003c/code\u003e\u003c/td\u003e\n\u003ctd\u003eHTML\u003c/td\u003e\n\u003ctd\u003eTwig\u003c/td\u003e\n\u003c/tr\u003e\n\u003ctr\u003e\u003ctd\u003e\u003ccode\u003eblog/index.html.php\u003c/code\u003e\u003c/td\u003e\n\u003ctd\u003eHTML\u003c/td\u003e\n\u003ctd\u003ePHP\u003c/td\u003e\n\u003c/tr\u003e\n\u003ctr\u003e\u003ctd\u003e\u003ccode\u003eblog/index.css.twig\u003c/code\u003e\u003c/td\u003e\n\u003ctd\u003eCSS\u003c/td\u003e\n\u003ctd\u003eTwig\u003c/td\u003e\n\u003c/tr\u003e\n\u003c/tbody\u003e\n\u003c/table\u003e\u003c/markdown-accessiblity-table\u003e\n\u003cp dir=\"auto\"\u003eBy default, any Symfony template can be written in either Twig or PHP, and\nthe last part of the extension (e.g. \u003ccode\u003e.twig\u003c/code\u003e or \u003ccode\u003e.php\u003c/code\u003e) specifies which\nof these two \u003cem\u003eengines\u003c/em\u003e should be used. The first part of the extension,\n(e.g. \u003ccode\u003e.html\u003c/code\u003e, \u003ccode\u003e.css\u003c/code\u003e, etc) is the final format that the template will\ngenerate. Unlike the engine, which determines how Symfony parses the template,\nthis is simply an organizational tactic used in case the same resource needs\nto be rendered as HTML (\u003ccode\u003eindex.html.twig\u003c/code\u003e), XML (\u003ccode\u003eindex.xml.twig\u003c/code\u003e),\nor any other format. For more information, read the \u003ca href=\"#id19\"\u003e\u003cspan id=\"user-content-id20\"\u003e:doc:`/templating/formats`\u003c/span\u003e\u003c/a\u003e\nsection.\u003c/p\u003e\n\u003cdiv dir=\"auto\"\u003e\n\u003cp dir=\"auto\"\u003eNote\u003c/p\u003e\n\u003cp dir=\"auto\"\u003eThe available \"engines\" can be configured and even new engines added.\nSee \u003ca href=\"#id21\"\u003e\u003cspan id=\"user-content-id22\"\u003e:ref:`Templating Configuration \u0026lt;template-configuration\u0026gt;`\u003c/span\u003e\u003c/a\u003e for more details.\u003c/p\u003e\n\u003c/div\u003e\n\u003cpre\u003e.. index::\n single: Templating; Tags and helpers\n single: Templating; Helpers\n\n\u003c/pre\u003e\n\u003ca name=\"user-content-tags-and-helpers\"\u003e\u003c/a\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch3 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eTags and Helpers\u003c/h3\u003e\u003ca id=\"user-content-tags-and-helpers\" class=\"anchor\" aria-label=\"Permalink: Tags and Helpers\" href=\"#tags-and-helpers\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eYou already understand the basics of templates, how they're named and how\nto use template inheritance. The hardest parts are already behind you. In\nthis section, you'll learn about a large group of tools available to help\nperform the most common template tasks such as including other templates,\nlinking to pages and including images.\u003c/p\u003e\n\u003cp dir=\"auto\"\u003eSymfony comes bundled with several specialized Twig tags and functions that\nease the work of the template designer. In PHP, the templating system provides\nan extensible \u003cem\u003ehelper\u003c/em\u003e system that provides useful features in a template\ncontext.\u003c/p\u003e\n\u003cp dir=\"auto\"\u003eYou've already seen a few built-in Twig tags (\u003ccode\u003e{% block %}\u003c/code\u003e \u0026amp; \u003ccode\u003e{% extends %}\u003c/code\u003e)\nas well as an example of a PHP helper (\u003ccode\u003e$view['slots']\u003c/code\u003e). Here you will learn a\nfew more.\u003c/p\u003e\n\u003cpre\u003e.. index::\n single: Templating; Including other templates\n\n\u003c/pre\u003e\n\u003ca name=\"user-content-including-other-templates\"\u003e\u003c/a\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch4 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eIncluding other Templates\u003c/h4\u003e\u003ca id=\"user-content-including-other-templates\" class=\"anchor\" aria-label=\"Permalink: Including other Templates\" href=\"#including-other-templates\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eYou'll often want to include the same template or code fragment on several\npages. For example, in an application with \"news articles\", the\ntemplate code displaying an article might be used on the article detail page,\non a page displaying the most popular articles, or in a list of the latest\narticles.\u003c/p\u003e\n\u003cp dir=\"auto\"\u003eWhen you need to reuse a chunk of PHP code, you typically move the code to\na new PHP class or function. The same is true for templates. By moving the\nreused template code into its own template, it can be included from any other\ntemplate. First, create the template that you'll need to reuse.\u003c/p\u003e\n\u003cpre\u003e.. configuration-block::\n\n .. code-block:: html+twig\n\n {# app/Resources/views/article/article_details.html.twig #}\n \u0026lt;h2\u0026gt;{{ article.title }}\u0026lt;/h2\u0026gt;\n \u0026lt;h3 class=\"byline\"\u0026gt;by {{ article.authorName }}\u0026lt;/h3\u0026gt;\n\n \u0026lt;p\u0026gt;\n {{ article.body }}\n \u0026lt;/p\u0026gt;\n\n .. code-block:: html+php\n\n \u0026lt;!-- app/Resources/views/article/article_details.html.php --\u0026gt;\n \u0026lt;h2\u0026gt;\u0026lt;?php echo $article-\u0026gt;getTitle() ?\u0026gt;\u0026lt;/h2\u0026gt;\n \u0026lt;h3 class=\"byline\"\u0026gt;by \u0026lt;?php echo $article-\u0026gt;getAuthorName() ?\u0026gt;\u0026lt;/h3\u0026gt;\n\n \u0026lt;p\u0026gt;\n \u0026lt;?php echo $article-\u0026gt;getBody() ?\u0026gt;\n \u0026lt;/p\u0026gt;\n\n\u003c/pre\u003e\n\u003cp dir=\"auto\"\u003eIncluding this template from any other template is simple:\u003c/p\u003e\n\u003cpre\u003e.. configuration-block::\n\n .. code-block:: html+twig\n\n {# app/Resources/views/article/list.html.twig #}\n {% extends 'layout.html.twig' %}\n\n {% block body %}\n \u0026lt;h1\u0026gt;Recent Articles\u0026lt;h1\u0026gt;\n\n {% for article in articles %}\n {{ include('article/article_details.html.twig', { 'article': article }) }}\n {% endfor %}\n {% endblock %}\n\n .. code-block:: html+php\n\n \u0026lt;!-- app/Resources/article/list.html.php --\u0026gt;\n \u0026lt;?php $view-\u0026gt;extend('layout.html.php') ?\u0026gt;\n\n \u0026lt;?php $view['slots']-\u0026gt;start('body') ?\u0026gt;\n \u0026lt;h1\u0026gt;Recent Articles\u0026lt;/h1\u0026gt;\n\n \u0026lt;?php foreach ($articles as $article): ?\u0026gt;\n \u0026lt;?php echo $view-\u0026gt;render(\n 'Article/article_details.html.php',\n array('article' =\u0026gt; $article)\n ) ?\u0026gt;\n \u0026lt;?php endforeach ?\u0026gt;\n \u0026lt;?php $view['slots']-\u0026gt;stop() ?\u0026gt;\n\n\u003c/pre\u003e\n\u003cp dir=\"auto\"\u003eThe template is included using the \u003ccode\u003e{{ include() }}\u003c/code\u003e function. Notice that the\ntemplate name follows the same typical convention. The \u003ccode\u003earticle_details.html.twig\u003c/code\u003e\ntemplate uses an \u003ccode\u003earticle\u003c/code\u003e variable, which we pass to it. In this case,\nyou could avoid doing this entirely, as all of the variables available in\n\u003ccode\u003elist.html.twig\u003c/code\u003e are also available in \u003ccode\u003earticle_details.html.twig\u003c/code\u003e (unless\nyou set \u003ca href=\"http://twig.sensiolabs.org/doc/functions/include.html\" rel=\"nofollow\"\u003ewith_context\u003c/a\u003e to false).\u003c/p\u003e\n\u003cdiv dir=\"auto\"\u003e\n\u003cp dir=\"auto\"\u003eTip\u003c/p\u003e\n\u003cp dir=\"auto\"\u003eThe \u003ccode\u003e{'article': article}\u003c/code\u003e syntax is the standard Twig syntax for hash\nmaps (i.e. an array with named keys). If you needed to pass in multiple\nelements, it would look like this: \u003ccode\u003e{'foo': foo, 'bar': bar}\u003c/code\u003e.\u003c/p\u003e\n\u003c/div\u003e\n\u003cpre\u003e.. index::\n single: Templating; Linking to pages\n\n\u003c/pre\u003e\n\u003ca name=\"user-content-linking-to-pages\"\u003e\u003c/a\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch4 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eLinking to Pages\u003c/h4\u003e\u003ca id=\"user-content-linking-to-pages\" class=\"anchor\" aria-label=\"Permalink: Linking to Pages\" href=\"#linking-to-pages\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eCreating links to other pages in your application is one of the most common\njobs for a template. Instead of hardcoding URLs in templates, use the \u003ccode\u003epath\u003c/code\u003e\nTwig function (or the \u003ccode\u003erouter\u003c/code\u003e helper in PHP) to generate URLs based on\nthe routing configuration. Later, if you want to modify the URL of a particular\npage, all you'll need to do is change the routing configuration; the templates\nwill automatically generate the new URL.\u003c/p\u003e\n\u003cp dir=\"auto\"\u003eFirst, link to the \"_welcome\" page, which is accessible via the following routing\nconfiguration:\u003c/p\u003e\n\u003cpre\u003e.. configuration-block::\n\n .. code-block:: php-annotations\n\n // src/AppBundle/Controller/WelcomeController.php\n\n // ...\n use Sensio\\Bundle\\FrameworkExtraBundle\\Configuration\\Route;\n\n class WelcomeController extends Controller\n {\n /**\n * @Route(\"/\", name=\"_welcome\")\n */\n public function indexAction()\n {\n // ...\n }\n }\n\n .. code-block:: yaml\n\n # app/config/routing.yml\n _welcome:\n path: /\n defaults: { _controller: AppBundle:Welcome:index }\n\n .. code-block:: xml\n\n \u0026lt;!-- app/config/routing.yml --\u0026gt;\n \u0026lt;?xml version=\"1.0\" encoding=\"UTF-8\" ?\u0026gt;\n \u0026lt;routes xmlns=\"http://symfony.com/schema/routing\"\n xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n xsi:schemaLocation=\"http://symfony.com/schema/routing\n http://symfony.com/schema/routing/routing-1.0.xsd\"\u0026gt;\n\n \u0026lt;route id=\"_welcome\" path=\"/\"\u0026gt;\n \u0026lt;default key=\"_controller\"\u0026gt;AppBundle:Welcome:index\u0026lt;/default\u0026gt;\n \u0026lt;/route\u0026gt;\n \u0026lt;/routes\u0026gt;\n\n .. code-block:: php\n\n // app/config/routing.php\n use Symfony\\Component\\Routing\\Route;\n use Symfony\\Component\\Routing\\RouteCollection;\n\n $collection = new RouteCollection();\n $collection-\u0026gt;add('_welcome', new Route('/', array(\n '_controller' =\u0026gt; 'AppBundle:Welcome:index',\n )));\n\n return $collection;\n\n\u003c/pre\u003e\n\u003cp dir=\"auto\"\u003eTo link to the page, just use the \u003ccode\u003epath\u003c/code\u003e Twig function and refer to the route:\u003c/p\u003e\n\u003cpre\u003e.. configuration-block::\n\n .. code-block:: html+twig\n\n \u0026lt;a href=\"{{ path('_welcome') }}\"\u0026gt;Home\u0026lt;/a\u0026gt;\n\n .. code-block:: html+php\n\n \u0026lt;a href=\"\u0026lt;?php echo $view['router']-\u0026gt;path('_welcome') ?\u0026gt;\"\u0026gt;Home\u0026lt;/a\u0026gt;\n\n\u003c/pre\u003e\n\u003cp dir=\"auto\"\u003eAs expected, this will generate the URL \u003ccode\u003e/\u003c/code\u003e. Now, for a more complicated\nroute:\u003c/p\u003e\n\u003cpre\u003e.. configuration-block::\n\n .. code-block:: php-annotations\n\n // src/AppBundle/Controller/ArticleController.php\n\n // ...\n use Sensio\\Bundle\\FrameworkExtraBundle\\Configuration\\Route;\n\n class ArticleController extends Controller\n {\n /**\n * @Route(\"/article/{slug}\", name=\"article_show\")\n */\n public function showAction($slug)\n {\n // ...\n }\n }\n\n .. code-block:: yaml\n\n # app/config/routing.yml\n article_show:\n path: /article/{slug}\n defaults: { _controller: AppBundle:Article:show }\n\n .. code-block:: xml\n\n \u0026lt;!-- app/config/routing.xml --\u0026gt;\n \u00 8A2C 26lt;?xml version=\"1.0\" encoding=\"UTF-8\" ?\u0026gt;\n \u0026lt;routes xmlns=\"http://symfony.com/schema/routing\"\n xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n xsi:schemaLocation=\"http://symfony.com/schema/routing\n http://symfony.com/schema/routing/routing-1.0.xsd\"\u0026gt;\n\n \u0026lt;route id=\"article_show\" path=\"/article/{slug}\"\u0026gt;\n \u0026lt;default key=\"_controller\"\u0026gt;AppBundle:Article:show\u0026lt;/default\u0026gt;\n \u0026lt;/route\u0026gt;\n \u0026lt;/routes\u0026gt;\n\n .. code-block:: php\n\n // app/config/routing.php\n use Symfony\\Component\\Routing\\Route;\n use Symfony\\Component\\Routing\\RouteCollection;\n\n $collection = new RouteCollection();\n $collection-\u0026gt;add('article_show', new Route('/article/{slug}', array(\n '_controller' =\u0026gt; 'AppBundle:Article:show',\n )));\n\n return $collection;\n\n\u003c/pre\u003e\n\u003cp dir=\"auto\"\u003eIn this case, you need to specify both the route name (\u003ccode\u003earticle_show\u003c/code\u003e) and\na value for the \u003ccode\u003e{slug}\u003c/code\u003e parameter. Using this route, revisit the\n\u003ccode\u003erecent_list\u003c/code\u003e template from the previous section and link to the articles\ncorrectly:\u003c/p\u003e\n\u003cpre\u003e.. configuration-block::\n\n .. code-block:: html+twig\n\n {# app/Resources/views/article/recent_list.html.twig #}\n {% for article in articles %}\n \u0026lt;a href=\"{{ path('article_show', {'slug': article.slug}) }}\"\u0026gt;\n {{ article.title }}\n \u0026lt;/a\u0026gt;\n {% endfor %}\n\n .. code-block:: html+php\n\n \u0026lt;!-- app/Resources/views/Article/recent_list.html.php --\u0026gt;\n \u0026lt;?php foreach ($articles in $article): ?\u0026gt;\n \u0026lt;a href=\"\u0026lt;?php echo $view['router']-\u0026gt;path('article_show', array(\n 'slug' =\u0026gt; $article-\u0026gt;getSlug(),\n )) ?\u0026gt;\"\u0026gt;\n \u0026lt;?php echo $article-\u0026gt;getTitle() ?\u0026gt;\n \u0026lt;/a\u0026gt;\n \u0026lt;?php endforeach ?\u0026gt;\n\n\u003c/pre\u003e\n\u003cdiv dir=\"auto\"\u003e\n\u003cp dir=\"auto\"\u003eTip\u003c/p\u003e\n\u003cp dir=\"auto\"\u003eYou can also generate an absolute URL by using the \u003ccode\u003eurl\u003c/code\u003e function:\u003c/p\u003e\n\u003cpre\u003e.. configuration-block::\n\n .. code-block:: html+twig\n\n \u0026lt;a href=\"{{ url('_welcome') }}\"\u0026gt;Home\u0026lt;/a\u0026gt;\n\n .. code-block:: html+php\n\n \u0026lt;a href=\"\u0026lt;?php echo $view['router']-\u0026gt;url(\n '_welcome',\n array()\n ) ?\u0026gt;\"\u0026gt;Home\u0026lt;/a\u0026gt;\n\u003c/pre\u003e\n\u003c/div\u003e\n\u003cpre\u003e.. index::\n single: Templating; Linking to assets\n\n\u003c/pre\u003e\n\u003ca name=\"user-content-linking-to-assets\"\u003e\u003c/a\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch4 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eLinking to Assets\u003c/h4\u003e\u003ca id=\"user-content-linking-to-assets\" class=\"anchor\" aria-label=\"Permalink: Linking to Assets\" href=\"#linking-to-assets\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eTemplates also commonly refer to images, JavaScript, stylesheets and other\nassets. Of course you could hard-code the path to these assets (e.g. \u003ccode\u003e/images/logo.png\u003c/code\u003e),\nbut Symfony provides a more dynamic option via the \u003ccode\u003easset\u003c/code\u003e Twig function:\u003c/p\u003e\n\u003cpre\u003e.. configuration-block::\n\n .. code-block:: html+twig\n\n \u0026lt;img src=\"{{ asset('images/logo.png') }}\" alt=\"Symfony!\" /\u0026gt;\n\n \u0026lt;link href=\"{{ asset('css/blog.css') }}\" rel=\"stylesheet\" /\u0026gt;\n\n .. code-block:: html+php\n\n \u0026lt;img src=\"\u0026lt;?php echo $view['assets']-\u0026gt;getUrl('images/logo.png') ?\u0026gt;\" alt=\"Symfony!\" /\u0026gt;\n\n \u0026lt;link href=\"\u0026lt;?php echo $view['assets']-\u0026gt;getUrl('css/blog.css') ?\u0026gt;\" rel=\"stylesheet\" /\u0026gt;\n\n\u003c/pre\u003e\n\u003cp dir=\"auto\"\u003eThe \u003ccode\u003easset\u003c/code\u003e function's main purpose is to make your application more portable.\nIf your application lives at the root of your host (e.g. \u003ccode\u003ehttp://example.com\u003c/code\u003e),\nthen the rendered paths should be \u003ccode\u003e/images/logo.png\u003c/code\u003e. But if your application\nlives in a subdirectory (e.g. \u003ccode\u003ehttp://example.com/my_app\u003c/code\u003e), each asset path\nshould render with the subdirectory (e.g. \u003ccode\u003e/my_app/images/logo.png\u003c/code\u003e). The\n\u003ccode\u003easset\u003c/code\u003e function takes care of this by determining how your application is\nbeing used and generating the correct paths accordingly.\u003c/p\u003e\n\u003cp dir=\"auto\"\u003eAdditionally, if you use the \u003ccode\u003easset\u003c/code\u003e function, Symfony can automatically\nappend a query string to your asset, in order to guarantee that updated static\nassets won't be loaded from cache after being deployed. For example, \u003ccode\u003e/images/logo.png\u003c/code\u003e might\nlook like \u003ccode\u003e/images/logo.png?v2\u003c/code\u003e. For more information, see the \u003ca href=\"#id23\"\u003e\u003cspan id=\"user-content-id24\"\u003e:ref:`reference-framework-assets-version`\u003c/span\u003e\u003c/a\u003e\nconfiguration option.\u003c/p\u003e\n\u003cp dir=\"auto\"\u003eIf you need absolute URLs for assets, use the \u003ccode\u003eabsolute_url()\u003c/code\u003e Twig function\nas follows:\u003c/p\u003e\n\u003cdiv class=\"highlight highlight-text-html-django notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"\u0026lt;img src=\u0026quot;{{ absolute_url(asset('images/logo.png')) }}\u0026quot; alt=\u0026quot;Symfony!\u0026quot; /\u0026gt;\"\u003e\u003cpre\u003e\u0026lt;\u003cspan class=\"pl-ent\"\u003eimg\u003c/span\u003e \u003cspan class=\"pl-e\"\u003esrc\u003c/span\u003e=\u003cspan class=\"pl-s\"\u003e\u003cspan class=\"pl-pds\"\u003e\"\u003c/span\u003e{{ absolute_url(asset('images/logo.png')) }}\u003cspan class=\"pl-pds\"\u003e\"\u003c/span\u003e\u003c/span\u003e \u003cspan class=\"pl-e\"\u003ealt\u003c/span\u003e=\u003cspan class=\"pl-s\"\u003e\u003cspan class=\"pl-pds\"\u003e\"\u003c/span\u003eSymfony!\u003cspan class=\"pl-pds\"\u003e\"\u003c/span\u003e\u003c/span\u003e /\u0026gt;\u003c/pre\u003e\u003c/div\u003e\n\u003cpre\u003e.. index::\n single: Templating; Including stylesheets and JavaScripts\n single: Stylesheets; Including stylesheets\n single: JavaScript; Including JavaScripts\n\n\u003c/pre\u003e\n\u003ca name=\"user-content-including-stylesheets-and-javascripts-in-twig\"\u003e\u003c/a\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch3 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eIncluding Stylesheets and JavaScripts in Twig\u003c/h3\u003e\u003ca id=\"user-content-including-stylesheets-and-javascripts-in-twig\" class=\"anchor\" aria-label=\"Permalink: Including Stylesheets and JavaScripts in Twig\" href=\"#including-stylesheets-and-javascripts-in-twig\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eNo site would be complete without including JavaScript files and stylesheets.\nIn Symfony, the inclusion of these assets is handled elegantly by taking\nadvantage of Symfony's template inheritance.\u003c/p\u003e\n\u003cdiv dir=\"auto\"\u003e\n\u003cp dir=\"auto\"\u003eTip\u003c/p\u003e\n\u003cp dir=\"auto\"\u003eThis section will teach you the philosophy behind including stylesheet\nand JavaScript assets in Symfony. Symfony is also compatible with another\nlibrary, called Assetic, which follows this philosophy but allows you to do\nmuch more interesting things with those assets. For more information on\nusing Assetic see \u003ca href=\"#id25\"\u003e\u003cspan id=\"user-content-id26\"\u003e:doc:`/assetic/asset_management`\u003c/span\u003e\u003c/a\u003e.\u003c/p\u003e\n\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eStart by adding two blocks to your base template that will hold your assets:\none called \u003ccode\u003estylesheets\u003c/code\u003e inside the \u003ccode\u003ehead\u003c/code\u003e tag and another called \u003ccode\u003ejavascripts\u003c/code\u003e\njust above the closing \u003ccode\u003ebody\u003c/code\u003e tag. These blocks will contain all of the\nstylesheets and JavaScripts that you'll need throughout your site:\u003c/p\u003e\n\u003cpre\u003e.. configuration-block::\n\n .. code-block:: html+twig\n\n {# app/Resources/views/base.html.twig #}\n \u0026lt;html\u0026gt;\n \u0026lt;head\u0026gt;\n {# ... #}\n\n {% block stylesheets %}\n \u0026lt;link href=\"{{ asset('css/main.css') }}\" rel=\"stylesheet\" /\u0026gt;\n {% endblock %}\n \u0026lt;/head\u0026gt;\n \u0026lt;body\u0026gt;\n {# ... #}\n\n {% block javascripts %}\n \u0026lt;script src=\"{{ asset('js/main.js') }}\"\u0026gt;\u0026lt;/script\u0026gt;\n {% endblock %}\n \u0026lt;/body\u0026gt;\n \u0026lt;/html\u0026gt;\n\n .. code-block:: php\n\n // app/Resources/views/base.html.php\n \u0026lt;html\u0026gt;\n \u0026lt;head\u0026gt;\n \u0026lt;?php ... ?\u0026gt;\n\n \u0026lt;?php $view['slots']-\u0026gt;start('stylesheets') ?\u0026gt;\n \u0026lt;link href=\"\u0026lt;?php echo $view['assets']-\u0026gt;getUrl('css/main.css') ?\u0026gt;\" rel=\"stylesheet\" /\u0026gt;\n \u0026lt;?php $view['slots']-\u0026gt;stop() ?\u0026gt;\n \u0026lt;/head\u0026gt;\n \u0026lt;body\u0026gt;\n \u0026lt;?php ... ?\u0026gt;\n\n \u0026lt;?php $view['slots']-\u0026gt;start('javascripts') ?\u0026gt;\n \u0026lt;script src=\"\u0026lt;?php echo $view['assets']-\u0026gt;getUrl('js/main.js') ?\u0026gt;\"\u0026gt;\u0026lt;/script\u0026gt;\n \u0026lt;?php $view['slots']-\u0026gt;stop() ?\u0026gt;\n \u0026lt;/body\u0026gt;\n \u0026lt;/html\u0026gt;\n\n\u003c/pre\u003e\n\u003cp dir=\"auto\"\u003eThat's easy enough! But what if you need to include an extra stylesheet or\nJavaScript from a child template? For example, suppose you have a contact\npage and you need to include a \u003ccode\u003econtact.css\u003c/code\u003e stylesheet \u003cem\u003ejust\u003c/em\u003e on that\npage. From inside that contact page's template, do the following:\u003c/p\u003e\n\u003cpre\u003e.. configuration-block::\n\n .. code-block:: html+twig\n\n {# app/Resources/views/contact/contact.html.twig #}\n {% extends 'base.html.twig' %}\n\n {% block stylesheets %}\n {{ parent() }}\n\n \u0026lt;link href=\"{{ asset('css/contact.css') }}\" rel=\"stylesheet\" /\u0026gt;\n {% endblock %}\n\n {# ... #}\n\n .. code-block:: php\n\n // app/Resources/views/contact/contact.html.twig\n \u0026lt;?php $view-\u0026gt;extend('base.html.php') ?\u0026gt;\n\n \u0026lt;?php $view['slots']-\u0026gt;start('stylesheets') ?\u0026gt;\n \u0026lt;link href=\"\u0026lt;?php echo $view['assets']-\u0026gt;getUrl('css/contact.css') ?\u0026gt;\" rel=\"stylesheet\" /\u0026gt;\n \u0026lt;?php $view['slots']-\u0026gt;stop() ?\u0026gt;\n\n\u003c/pre\u003e\n\u003cp dir=\"auto\"\u003eIn the child template, you simply override the \u003ccode\u003estylesheets\u003c/code\u003e block and\nput your new stylesheet tag inside of that block. Of course, since you want\nto add to the parent block's content (and not actually \u003cem\u003ereplace\u003c/em\u003e it), you\nshould use the \u003ccode\u003eparent()\u003c/code\u003e Twig function to include everything from the \u003ccode\u003estylesheets\u003c/code\u003e\nblock of the base template.\u003c/p\u003e\n\u003cp dir=\"auto\"\u003eYou can also include assets located in your bundles' \u003ccode\u003eResources/public\u003c/code\u003e folder.\nYou will need to run the \u003ccode\u003ephp bin/console assets:install target [--symlink]\u003c/code\u003e\ncommand, which moves (or symlinks) files into the correct location. (target\nis by default \"web\").\u003c/p\u003e\n\u003cpre lang=\"html+twig\"\u003e\u0026lt;link href=\"{{ asset('bundles/acmedemo/css/contact.css') }}\" rel=\"stylesheet\" /\u0026gt;\n\u003c/pre\u003e\n\u003cp dir=\"auto\"\u003eThe end result is a page that includes both the \u003ccode\u003emain.css\u003c/code\u003e and \u003ccode\u003econtact.css\u003c/code\u003e\nstylesheets.\u003c/p\u003e\n\u003ca name=\"user-content-referencing-the-request-user-or-session\"\u003e\u003c/a\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch3 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eReferencing the Request, User or Session\u003c/h3\u003e\u003ca id=\"user-content-referencing-the-request-user-or-session\" class=\"anchor\" aria-label=\"Permalink: Referencing the Request, User or Session\" href=\"#referencing-the-request-user-or-session\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eSymfony also gives you a global \u003ccode\u003eapp\u003c/code\u003e variable in Twig that can be used to access\nthe current user, the Request and more.\u003c/p\u003e\n\u003cp dir=\"auto\"\u003eSee \u003ca href=\"#id27\"\u003e\u003cspan id=\"user-content-id28\"\u003e:doc:`/templating/app_variable`\u003c/span\u003e\u003c/a\u003e for details.\u003c/p\u003e\n\u003ca name=\"user-content-output-escaping\"\u003e\u003c/a\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch3 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eOutput Escaping\u003c/h3\u003e\u003ca id=\"user-content-output-escaping\" class=\"anchor\" aria-label=\"Permalink: Output Escaping\" href=\"#output-escaping\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eTwig performs automatic \"output escaping\" when rendering any content in order to\nprotect you from Cross Site Scripting (XSS) attacks.\u003c/p\u003e\n\u003cp dir=\"auto\"\u003eSuppose \u003ccode\u003edescription\u003c/code\u003e equals \u003ccode\u003eI \u0026lt;3 this product\u003c/code\u003e:\u003c/p\u003e\n\u003cdiv class=\"highlight highlight-text-html-twig notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"\u0026lt;!-- outupt escaping is on automatically --\u0026gt;\n{{ description }} \u0026lt;!-- I \u0026amp;lt3 this product --\u0026gt;\n\n\u0026lt;!-- disable output escaping with the raw filter --\u0026gt;\n{{ description|raw }} \u0026lt;!-- I \u0026lt;3 this product --\u0026gt;\"\u003e\u003cpre\u003e\u003cspan class=\"pl-c\"\u003e\u003cspan class=\"pl-c\"\u003e\u0026lt;!--\u003c/span\u003e outupt escaping is on automatically \u003cspan class=\"pl-c\"\u003e--\u0026gt;\u003c/span\u003e\u003c/span\u003e\n{{ \u003cspan class=\"pl-smi\"\u003edescription\u003c/span\u003e }} \u003cspan class=\"pl-c\"\u003e\u003cspan class=\"pl-c\"\u003e\u0026lt;!--\u003c/span\u003e I \u0026amp;lt3 this product \u003cspan class=\"pl-c\"\u003e--\u0026gt;\u003c/span\u003e\u003c/span\u003e\n\n\u003cspan class=\"pl-c\"\u003e\u003cspan class=\"pl-c\"\u003e\u0026lt;!--\u003c/span\u003e disable output escaping with the raw filter \u003cspan class=\"pl-c\"\u003e--\u0026gt;\u003c/span\u003e\u003c/span\u003e\n{{ \u003cspan class=\"pl-smi\"\u003edescription\u003c/span\u003e\u003cspan class=\"pl-k\"\u003e|\u003c/span\u003e\u003cspan class=\"pl-c1\"\u003eraw\u003c/span\u003e }} \u003cspan class=\"pl-c\"\u003e\u003cspan class=\"pl-c\"\u003e\u0026lt;!--\u003c/span\u003e I \u0026lt;3 this product \u003cspan class=\"pl-c\"\u003e--\u0026gt;\u003c/span\u003e\u003c/span\u003e\u003c/pre\u003e\u003c/div\u003e\n\u003cdiv dir=\"auto\"\u003e\n\u003cp dir=\"auto\"\u003eCaution!\u003c/p\u003e\n\u003cp dir=\"auto\"\u003ePHP templates do not automatically escape content.\u003c/p\u003e\n\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eFor more details, see \u003ca href=\"#id29\"\u003e\u003cspan id=\"user-content-id30\"\u003e:doc:`/templating/escaping`\u003c/span\u003e\u003c/a\u003e.\u003c/p\u003e\n\u003ca name=\"user-content-final-thoughts\"\u003e\u003c/a\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch3 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eFinal Thoughts\u003c/h3\u003e\u003ca id=\"user-content-final-thoughts\" class=\"anchor\" aria-label=\"Permalink: Final Thoughts\" href=\"#final-thoughts\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eThe templating system is just \u003cem\u003eone\u003c/em\u003e of the many tools in Symfony. And its job is\nsimple: allow us to render dynamic \u0026amp; complex HTML output so that this can ultimately\nbe returned to the user, sent in an email or something else.\u003c/p\u003e\n\u003ca name=\"user-content-keep-going\"\u003e\u003c/a\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch3 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eKeep Going!\u003c/h3\u003e\u003ca id=\"user-content-keep-going\" class=\"anchor\" aria-label=\"Permalink: Keep Going!\" href=\"#keep-going\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cp dir=\"auto\"\u003eBefore diving into the rest of Symfony, check out the \u003ca href=\"#id31\"\u003e\u003cspan id=\"user-content-id32\"\u003e:doc:`configuration system \u0026lt;/configuration\u0026gt;`\u003c/span\u003e\u003c/a\u003e.\u003c/p\u003e\n\u003ca name=\"user-content-learn-more\"\u003e\u003c/a\u003e\n\u003cdiv class=\"markdown-heading\" dir=\"auto\"\u003e\u003ch3 tabindex=\"-1\" class=\"heading-element\" dir=\"auto\"\u003eLearn more\u003c/h3\u003e\u003ca id=\"user-content-learn-more\" class=\"anchor\" aria-label=\"Permalink: Learn more\" href=\"#learn-more\"\u003e\u003csvg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"\u003e\u003cpath d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"\u003e\u003c/path\u003e\u003c/svg\u003e\u003c/a\u003e\u003c/div\u003e\n\u003cpre\u003e.. toctree::\n :hidden:\n\n configuration\n\n\u003c/pre\u003e\n\u003cpre\u003e.. toctree::\n :maxdepth: 1\n :glob:\n\n /templating/*\n\n\u003c/pre\u003e\n\n\u003c/article\u003e","renderedFileInfo":null,"shortPath":null,"symbolsEnabled":true,"tabSize":2,"topBannersInfo":{"overridingGlobalFundingFile":false,"globalPreferredFundingPath":"/symfony/.github/blob/6f2ca452c856184a28812bb364b4e34ed50309da/FUNDING.yml","showInvalidCitationWarning":false,"citationHelpUrl":"https://docs.github.com/github/creating-cloning-and-archiving-repositories/creating-a-repository-on-github/about-citation-files","actionsOnboardingTip":null},"truncated":false,"viewable":true,"workflowRedirectUrl":null,"symbols":null},"copilotInfo":null,"copilotAccessAllowed":false,"modelsAccessAllowed":false,"modelsRepoIntegrationEnabled":false,"csrf_tokens":{"/symfony/symfony-docs/branches":{"post":"jAmBdDDu3uRUFqyc5hI__NroKmjm5VYyJYl3iy1esQIOLF2qf9N6TQFuZpYoAIEK3QNpXfcJGRqjPi90ihmjzg"},"/repos/preferences":{"post":"Kw4rCgNnePCoI178e2fKN4YeobaFOQhVvCY9SSMJTFk04q1GbUFtFSzNdtn8c_vS3KX6wjvjoRfcTR2Tj86p3Q"}}},"title":"symfony-docs/templating.rst at 3.0 · symfony/symfony-docs","appPayload":{"helpUrl":"https://docs.github.com","findFileWorkerPath":"/assets-cdn/worker/find-file-worker-263cab1760dd.js","findInFileWorkerPath":"/assets-cdn/worker/find-in-file-worker-b84e9496fc59.js","githubDevUrl":null,"enabled_features":{"code_nav_ui_events":false,"react_blob_overlay":false,"accessible_code_button":true}}}
0