Repository: twigphp/Twig
Branch: 3.x
Commit: 76688da07c9e
Files: 1117
Total size: 1.8 MB
Directory structure:
gitextract_my2eswkz/
├── .editorconfig
├── .gitattributes
├── .github/
│ ├── dependabot.yml
│ └── workflows/
│ ├── ci.yml
│ ├── documentation.yml
│ └── fabbot.yml
├── .gitignore
├── .php-cs-fixer.dist.php
├── CHANGELOG
├── LICENSE
├── README.rst
├── bin/
│ └── generate_operators_precedence.php
├── composer.json
├── doc/
│ ├── .doctor-rst.yaml
│ ├── _build/
│ │ ├── build.php
│ │ └── composer.json
│ ├── advanced.rst
│ ├── api.rst
│ ├── coding_standards.rst
│ ├── deprecated.rst
│ ├── filters/
│ │ ├── abs.rst
│ │ ├── batch.rst
│ │ ├── capitalize.rst
│ │ ├── column.rst
│ │ ├── convert_encoding.rst
│ │ ├── country_name.rst
│ │ ├── currency_name.rst
│ │ ├── currency_symbol.rst
│ │ ├── data_uri.rst
│ │ ├── date.rst
│ │ ├── date_modify.rst
│ │ ├── default.rst
│ │ ├── escape.rst
│ │ ├── filter.rst
│ │ ├── find.rst
│ │ ├── first.rst
│ │ ├── format.rst
│ │ ├── format_currency.rst
│ │ ├── format_date.rst
│ │ ├── format_datetime.rst
│ │ ├── format_number.rst
│ │ ├── format_time.rst
│ │ ├── html_attr_merge.rst
│ │ ├── html_attr_type.rst
│ │ ├── html_to_markdown.rst
│ │ ├── index.rst
│ │ ├── inky_to_html.rst
│ │ ├── inline_css.rst
│ │ ├── invoke.rst
│ │ ├── join.rst
│ │ ├── json_encode.rst
│ │ ├── keys.rst
│ │ ├── language_name.rst
│ │ ├── last.rst
│ │ ├── length.rst
│ │ ├── locale_name.rst
│ │ ├── lower.rst
│ │ ├── map.rst
│ │ ├── markdown_to_html.rst
│ │ ├── merge.rst
│ │ ├── nl2br.rst
│ │ ├── number_format.rst
│ │ ├── plural.rst
│ │ ├── raw.rst
│ │ ├── reduce.rst
│ │ ├── replace.rst
│ │ ├── reverse.rst
│ │ ├── round.rst
│ │ ├── shuffle.rst
│ │ ├── singular.rst
│ │ ├── slice.rst
│ │ ├── slug.rst
│ │ ├── sort.rst
│ │ ├── spaceless.rst
│ │ ├── split.rst
│ │ ├── striptags.rst
│ │ ├── timezone_name.rst
│ │ ├── title.rst
│ │ ├── trim.rst
│ │ ├── u.rst
│ │ ├── upper.rst
│ │ └── url_encode.rst
│ ├── functions/
│ │ ├── attribute.rst
│ │ ├── block.rst
│ │ ├── constant.rst
│ │ ├── country_names.rst
│ │ ├── country_timezones.rst
│ │ ├── currency_names.rst
│ │ ├── cycle.rst
│ │ ├── date.rst
│ │ ├── dump.rst
│ │ ├── enum.rst
│ │ ├── enum_cases.rst
│ │ ├── html_attr.rst
│ │ ├── html_classes.rst
│ │ ├── html_cva.rst
│ │ ├── include.rst
│ │ ├── index.rst
│ │ ├── language_names.rst
│ │ ├── locale_names.rst
│ │ ├── max.rst
│ │ ├── min.rst
│ │ ├── parent.rst
│ │ ├── random.rst
│ │ ├── range.rst
│ │ ├── script_names.rst
│ │ ├── source.rst
│ │ ├── template_from_string.rst
│ │ └── timezone_names.rst
│ ├── index.rst
│ ├── installation.rst
│ ├── internals.rst
│ ├── intro.rst
│ ├── operators_precedence.rst
│ ├── recipes.rst
│ ├── sandbox.rst
│ ├── tags/
│ │ ├── apply.rst
│ │ ├── autoescape.rst
│ │ ├── block.rst
│ │ ├── cache.rst
│ │ ├── deprecated.rst
│ │ ├── do.rst
│ │ ├── embed.rst
│ │ ├── extends.rst
│ │ ├── flush.rst
│ │ ├── for.rst
│ │ ├── from.rst
│ │ ├── guard.rst
│ │ ├── if.rst
│ │ ├── import.rst
│ │ ├── include.rst
│ │ ├── index.rst
│ │ ├── macro.rst
│ │ ├── sandbox.rst
│ │ ├── set.rst
│ │ ├── types.rst
│ │ ├── use.rst
│ │ ├── verbatim.rst
│ │ └── with.rst
│ ├── templates.rst
│ └── tests/
│ ├── constant.rst
│ ├── defined.rst
│ ├── divisibleby.rst
│ ├── empty.rst
│ ├── even.rst
│ ├── index.rst
│ ├── iterable.rst
│ ├── mapping.rst
│ ├── null.rst
│ ├── odd.rst
│ ├── sameas.rst
│ └── sequence.rst
├── extra/
│ ├── cache-extra/
│ │ ├── .gitattributes
│ │ ├── .gitignore
│ │ ├── CacheExtension.php
│ │ ├── CacheRuntime.php
│ │ ├── LICENSE
│ │ ├── Node/
│ │ │ └── CacheNode.php
│ │ ├── README.md
│ │ ├── Tests/
│ │ │ ├── Fixtures/
│ │ │ │ ├── cache.test
│ │ │ │ ├── cache_complex.test
│ │ │ │ ├── cache_with_blocks.test
│ │ │ │ └── macro.test
│ │ │ ├── FunctionalTest.php
│ │ │ └── IntegrationTest.php
│ │ ├── TokenParser/
│ │ │ └── CacheTokenParser.php
│ │ ├── composer.json
│ │ └── phpunit.xml.dist
│ ├── cssinliner-extra/
│ │ ├── .gitattributes
│ │ ├── .gitignore
│ │ ├── CssInlinerExtension.php
│ │ ├── LICENSE
│ │ ├── README.md
│ │ ├── Resources/
│ │ │ └── functions.php
│ │ ├── Tests/
│ │ │ ├── Fixtures/
│ │ │ │ └── inline_css.test
│ │ │ ├── IntegrationTest.php
│ │ │ └── LegacyFunctionsTest.php
│ │ ├── composer.json
│ │ └── phpunit.xml.dist
│ ├── html-extra/
│ │ ├── .gitattributes
│ │ ├── .gitignore
│ │ ├── Cva.php
│ │ ├── HtmlAttr/
│ │ │ ├── AttributeValueInterface.php
│ │ │ ├── InlineStyle.php
│ │ │ ├── MergeableInterface.php
│ │ │ └── SeparatedTokenList.php
│ │ ├── HtmlExtension.php
│ │ ├── LICENSE
│ │ ├── README.md
│ │ ├── Resources/
│ │ │ └── functions.php
│ │ ├── Tests/
│ │ │ ├── CvaTest.php
│ │ │ ├── Fixtures/
│ │ │ │ ├── data_uri.test
│ │ │ │ ├── html_attr.test
│ │ │ │ ├── html_attr_merge.test
│ │ │ │ ├── html_classes.test
│ │ │ │ ├── html_classes_with_unsupported_arg.test
│ │ │ │ ├── html_classes_with_unsupported_key.test
│ │ │ │ ├── html_cva.test
│ │ │ │ └── html_cva_pass_to_template.test
│ │ │ ├── HtmlAttrMergeTest.php
│ │ │ ├── HtmlAttrTest.php
│ │ │ ├── IntegrationTest.php
│ │ │ └── LegacyFunctionsTest.php
│ │ ├── composer.json
│ │ └── phpunit.xml.dist
│ ├── inky-extra/
│ │ ├── .gitattributes
│ │ ├── .gitignore
│ │ ├── InkyExtension.php
│ │ ├── LICENSE
│ │ ├── README.md
│ │ ├── Resources/
│ │ │ └── functions.php
│ │ ├── Tests/
│ │ │ ├── Fixtures/
│ │ │ │ └── inky.test
│ │ │ ├── IntegrationTest.php
│ │ │ └── LegacyFunctionsTest.php
│ │ ├── composer.json
│ │ └── phpunit.xml.dist
│ ├── intl-extra/
│ │ ├── .gitattributes
│ │ ├── .gitignore
│ │ ├── IntlExtension.php
│ │ ├── LICENSE
│ │ ├── README.md
│ │ ├── Tests/
│ │ │ ├── Fixtures/
│ │ │ │ ├── country_name.test
│ │ │ │ ├── country_names.test
│ │ │ │ ├── country_timezones.test
│ │ │ │ ├── currency_name.test
│ │ │ │ ├── currency_names.test
│ │ │ │ ├── currency_symbol.test
│ │ │ │ ├── format_currency.test
│ │ │ │ ├── format_date.test
│ │ │ │ ├── format_date_ICU72.test
│ │ │ │ ├── format_date_php8.test
│ │ │ │ ├── format_date_php8_ICU72.test
│ │ │ │ ├── format_number.test
│ │ │ │ ├── language_name.test
│ │ │ │ ├── language_names.test
│ │ │ │ ├── locale_name.test
│ │ │ │ ├── locale_names.test
│ │ │ │ ├── script_names.test
│ │ │ │ ├── timezone_name.test
│ │ │ │ └── timezone_names.test
│ │ │ ├── IntegrationTest.php
│ │ │ └── IntlExtensionTest.php
│ │ ├── composer.json
│ │ └── phpunit.xml.dist
│ ├── markdown-extra/
│ │ ├── .gitattributes
│ │ ├── .gitignore
│ │ ├── DefaultMarkdown.php
│ │ ├── ErusevMarkdown.php
│ │ ├── LICENSE
│ │ ├── LeagueMarkdown.php
│ │ ├── MarkdownExtension.php
│ │ ├── MarkdownInterface.php
│ │ ├── MarkdownRuntime.php
│ │ ├── MichelfMarkdown.php
│ │ ├── README.md
│ │ ├── Resources/
│ │ │ └── functions.php
│ │ ├── Tests/
│ │ │ ├── Fixtures/
│ │ │ │ └── html_to_markdown.test
│ │ │ ├── FunctionalTest.php
│ │ │ ├── IntegrationTest.php
│ │ │ └── LegacyFunctionsTest.php
│ │ ├── composer.json
│ │ └── phpunit.xml.dist
│ ├── string-extra/
│ │ ├── .gitattributes
│ │ ├── .gitignore
│ │ ├── LICENSE
│ │ ├── README.md
│ │ ├── StringExtension.php
│ │ ├── Tests/
│ │ │ ├── Fixtures/
│ │ │ │ ├── plural-invalid-language.test
│ │ │ │ ├── plural.test
│ │ │ │ ├── plural_es.test
│ │ │ │ ├── singular-invalid-language.test
│ │ │ │ ├── singular.test
│ │ │ │ ├── singular_es.test
│ │ │ │ ├── slug.test
│ │ │ │ └── string.test
│ │ │ └── IntegrationTest.php
│ │ ├── composer.json
│ │ └── phpunit.xml.dist
│ └── twig-extra-bundle/
│ ├── .gitattributes
│ ├── .gitignore
│ ├── DependencyInjection/
│ │ ├── Compiler/
│ │ │ └── MissingExtensionSuggestorPass.php
│ │ ├── Configuration.php
│ │ └── TwigExtraExtension.php
│ ├── Extensions.php
│ ├── LICENSE
│ ├── LeagueCommonMarkConverterFactory.php
│ ├── MissingExtensionSuggestor.php
│ ├── README.md
│ ├── Resources/
│ │ └── config/
│ │ ├── cache.php
│ │ ├── cssinliner.php
│ │ ├── html.php
│ │ ├── inky.php
│ │ ├── intl.php
│ │ ├── markdown.php
│ │ ├── markdown_league.php
│ │ ├── string.php
│ │ └── suggestor.php
│ ├── Tests/
│ │ ├── DependencyInjection/
│ │ │ └── TwigExtraExtensionTest.php
│ │ ├── Fixture/
│ │ │ ├── Kernel.php
│ │ │ └── views/
│ │ │ └── markdown_to_html.html.twig
│ │ └── IntegrationTest.php
│ ├── TwigExtraBundle.php
│ ├── composer.json
│ └── phpunit.xml.dist
├── phpstan-baseline.neon
├── phpstan.neon.dist
├── phpunit.xml.dist
├── splitsh.json
├── src/
│ ├── AbstractTwigCallable.php
│ ├── Attribute/
│ │ ├── AsTwigFilter.php
│ │ ├── AsTwigFunction.php
│ │ ├── AsTwigTest.php
│ │ ├── FirstClassTwigCallableReady.php
│ │ └── YieldReady.php
│ ├── Cache/
│ │ ├── CacheInterface.php
│ │ ├── ChainCache.php
│ │ ├── FilesystemCache.php
│ │ ├── NullCache.php
│ │ ├── ReadOnlyFilesystemCache.php
│ │ └── RemovableCacheInterface.php
│ ├── Compiler.php
│ ├── DeprecatedCallableInfo.php
│ ├── Environment.php
│ ├── Error/
│ │ ├── Error.php
│ │ ├── LoaderError.php
│ │ ├── RuntimeError.php
│ │ └── SyntaxError.php
│ ├── ExpressionParser/
│ │ ├── AbstractExpressionParser.php
│ │ ├── ExpressionParserDescriptionInterface.php
│ │ ├── ExpressionParserInterface.php
│ │ ├── ExpressionParserType.php
│ │ ├── ExpressionParsers.php
│ │ ├── Infix/
│ │ │ ├── ArgumentsTrait.php
│ │ │ ├── ArrowExpressionParser.php
│ │ │ ├── AssignmentExpressionParser.php
│ │ │ ├── BinaryOperatorExpressionParser.php
│ │ │ ├── ConditionalTernaryExpressionParser.php
│ │ │ ├── DotExpressionParser.php
│ │ │ ├── FilterExpressionParser.php
│ │ │ ├── FunctionExpressionParser.php
│ │ │ ├── IsExpressionParser.php
│ │ │ ├── IsNotExpressionParser.php
│ │ │ └── SquareBracketExpressionParser.php
│ │ ├── InfixAssociativity.php
│ │ ├── InfixExpressionParserInterface.php
│ │ ├── PrecedenceChange.php
│ │ ├── Prefix/
│ │ │ ├── GroupingExpressionParser.php
│ │ │ ├── LiteralExpressionParser.php
│ │ │ └── UnaryOperatorExpressionParser.php
│ │ └── PrefixExpressionParserInterface.php
│ ├── ExpressionParser.php
│ ├── Extension/
│ │ ├── AbstractExtension.php
│ │ ├── AttributeExtension.php
│ │ ├── CoreExtension.php
│ │ ├── DebugExtension.php
│ │ ├── EscaperExtension.php
│ │ ├── ExtensionInterface.php
│ │ ├── GlobalsInterface.php
│ │ ├── LastModifiedExtensionInterface.php
│ │ ├── OptimizerExtension.php
│ │ ├── ProfilerExtension.php
│ │ ├── RuntimeExtensionInterface.php
│ │ ├── SandboxExtension.php
│ │ ├── StagingExtension.php
│ │ ├── StringLoaderExtension.php
│ │ └── YieldNotReadyExtension.php
│ ├── ExtensionSet.php
│ ├── FileExtensionEscapingStrategy.php
│ ├── Lexer.php
│ ├── Loader/
│ │ ├── ArrayLoader.php
│ │ ├── ChainLoader.php
│ │ ├── FilesystemLoader.php
│ │ └── LoaderInterface.php
│ ├── Markup.php
│ ├── Node/
│ │ ├── AutoEscapeNode.php
│ │ ├── BlockNode.php
│ │ ├── BlockReferenceNode.php
│ │ ├── BodyNode.php
│ │ ├── CaptureNode.php
│ │ ├── CheckSecurityCallNode.php
│ │ ├── CheckSecurityNode.php
│ │ ├── CheckToStringNode.php
│ │ ├── DeprecatedNode.php
│ │ ├── DoNode.php
│ │ ├── EmbedNode.php
│ │ ├── EmptyNode.php
│ │ ├── Expression/
│ │ │ ├── AbstractExpression.php
│ │ │ ├── ArrayExpression.php
│ │ │ ├── ArrowFunctionExpression.php
│ │ │ ├── AssignNameExpression.php
│ │ │ ├── Binary/
│ │ │ │ ├── AbstractBinary.php
│ │ │ │ ├── AddBinary.php
│ │ │ │ ├── AndBinary.php
│ │ │ │ ├── BinaryInterface.php
│ │ │ │ ├── BitwiseAndBinary.php
│ │ │ │ ├── BitwiseOrBinary.php
│ │ │ │ ├── BitwiseXorBinary.php
│ │ │ │ ├── ConcatBinary.php
│ │ │ │ ├── DivBinary.php
│ │ │ │ ├── ElvisBinary.php
│ │ │ │ ├── EndsWithBinary.php
│ │ │ │ ├── EqualBinary.php
│ │ │ │ ├── FloorDivBinary.php
│ │ │ │ ├── GreaterBinary.php
│ │ │ │ ├── GreaterEqualBinary.php
│ │ │ │ ├── HasEveryBinary.php
│ │ │ │ ├── HasSomeBinary.php
│ │ │ │ ├── InBinary.php
│ │ │ │ ├── LessBinary.php
│ │ │ │ ├── LessEqualBinary.php
│ │ │ │ ├── MatchesBinary.php
│ │ │ │ ├── ModBinary.php
│ │ │ │ ├── MulBinary.php
│ │ │ │ ├── NotEqualBinary.php
│ │ │ │ ├── NotInBinary.php
│ │ │ │ ├── NotSameAsBinary.php
│ │ │ │ ├── NullCoalesceBinary.php
│ │ │ │ ├── ObjectDestructuringSetBinary.php
│ │ │ │ ├── OrBinary.php
│ │ │ │ ├── PowerBinary.php
│ │ │ │ ├── RangeBinary.php
│ │ │ │ ├── SameAsBinary.php
│ │ │ │ ├── SequenceDestructuringSetBinary.php
│ │ │ │ ├── SetBinary.php
│ │ │ │ ├── SpaceshipBinary.php
│ │ │ │ ├── StartsWithBinary.php
│ │ │ │ ├── SubBinary.php
│ │ │ │ └── XorBinary.php
│ │ │ ├── BlockReferenceExpression.php
│ │ │ ├── CallExpression.php
│ │ │ ├── ConditionalExpression.php
│ │ │ ├── ConstantExpression.php
│ │ │ ├── EmptyExpression.php
│ │ │ ├── Filter/
│ │ │ │ ├── DefaultFilter.php
│ │ │ │ └── RawFilter.php
│ │ │ ├── FilterExpression.php
│ │ │ ├── FunctionExpression.php
│ │ │ ├── FunctionNode/
│ │ │ │ ├── EnumCasesFunction.php
│ │ │ │ └── EnumFunction.php
│ │ │ ├── GetAttrExpression.php
│ │ │ ├── InlinePrint.php
│ │ │ ├── ListExpression.php
│ │ │ ├── MacroReferenceExpression.php
│ │ │ ├── MethodCallExpression.php
│ │ │ ├── NameExpression.php
│ │ │ ├── NullCoalesceExpression.php
│ │ │ ├── OperatorEscapeInterface.php
│ │ │ ├── ParentExpression.php
│ │ │ ├── ReturnArrayInterface.php
│ │ │ ├── ReturnBoolInterface.php
│ │ │ ├── ReturnNumberInterface.php
│ │ │ ├── ReturnPrimitiveTypeInterface.php
│ │ │ ├── ReturnStringInterface.php
│ │ │ ├── SupportDefinedTestDeprecationTrait.php
│ │ │ ├── SupportDefinedTestInterface.php
│ │ │ ├── SupportDefinedTestTrait.php
│ │ │ ├── TempNameExpression.php
│ │ │ ├── Ternary/
│ │ │ │ └── ConditionalTernary.php
│ │ │ ├── Test/
│ │ │ │ ├── ConstantTest.php
│ │ │ │ ├── DefinedTest.php
│ │ │ │ ├── DivisiblebyTest.php
│ │ │ │ ├── EvenTest.php
│ │ │ │ ├── NullTest.php
│ │ │ │ ├── OddTest.php
│ │ │ │ ├── SameasTest.php
│ │ │ │ └── TrueTest.php
│ │ │ ├── TestExpression.php
│ │ │ ├── Unary/
│ │ │ │ ├── AbstractUnary.php
│ │ │ │ ├── NegUnary.php
│ │ │ │ ├── NotUnary.php
│ │ │ │ ├── PosUnary.php
│ │ │ │ ├── SpreadUnary.php
│ │ │ │ ├── StringCastUnary.php
│ │ │ │ └── UnaryInterface.php
│ │ │ ├── Variable/
│ │ │ │ ├── AssignContextVariable.php
│ │ │ │ ├── AssignTemplateVariable.php
│ │ │ │ ├── ContextVariable.php
│ │ │ │ ├── LocalVariable.php
│ │ │ │ └── TemplateVariable.php
│ │ │ └── VariadicExpression.php
│ │ ├── FlushNode.php
│ │ ├── ForElseNode.php
│ │ ├── ForLoopNode.php
│ │ ├── ForNode.php
│ │ ├── IfNode.php
│ │ ├── ImportNode.php
│ │ ├── IncludeNode.php
│ │ ├── MacroNode.php
│ │ ├── ModuleNode.php
│ │ ├── NameDeprecation.php
│ │ ├── Node.php
│ │ ├── NodeCaptureInterface.php
│ │ ├── NodeOutputInterface.php
│ │ ├── Nodes.php
│ │ ├── PrintNode.php
│ │ ├── SandboxNode.php
│ │ ├── SetNode.php
│ │ ├── TextNode.php
│ │ ├── TypesNode.php
│ │ └── WithNode.php
│ ├── NodeTraverser.php
│ ├── NodeVisitor/
│ │ ├── AbstractNodeVisitor.php
│ │ ├── EscaperNodeVisitor.php
│ │ ├── NodeVisitorInterface.php
│ │ ├── OptimizerNodeVisitor.php
│ │ ├── SafeAnalysisNodeVisitor.php
│ │ ├── SandboxNodeVisitor.php
│ │ └── YieldNotReadyNodeVisitor.php
│ ├── OperatorPrecedenceChange.php
│ ├── Parser.php
│ ├── Profiler/
│ │ ├── Dumper/
│ │ │ ├── BaseDumper.php
│ │ │ ├── BlackfireDumper.php
│ │ │ ├── HtmlDumper.php
│ │ │ └── TextDumper.php
│ │ ├── Node/
│ │ │ ├── EnterProfileNode.php
│ │ │ └── LeaveProfileNode.php
│ │ ├── NodeVisitor/
│ │ │ └── ProfilerNodeVisitor.php
│ │ └── Profile.php
│ ├── Resources/
│ │ ├── core.php
│ │ ├── debug.php
│ │ ├── escaper.php
│ │ └── string_loader.php
│ ├── Runtime/
│ │ └── EscaperRuntime.php
│ ├── RuntimeLoader/
│ │ ├── ContainerRuntimeLoader.php
│ │ ├── FactoryRuntimeLoader.php
│ │ └── RuntimeLoaderInterface.php
│ ├── Sandbox/
│ │ ├── SecurityError.php
│ │ ├── SecurityNotAllowedFilterError.php
│ │ ├── SecurityNotAllowedFunctionError.php
│ │ ├── SecurityNotAllowedMethodError.php
│ │ ├── SecurityNotAllowedPropertyError.php
│ │ ├── SecurityNotAllowedTagError.php
│ │ ├── SecurityPolicy.php
│ │ ├── SecurityPolicyInterface.php
│ │ └── SourcePolicyInterface.php
│ ├── Source.php
│ ├── Template.php
│ ├── TemplateWrapper.php
│ ├── Test/
│ │ ├── IntegrationTestCase.php
│ │ └── NodeTestCase.php
│ ├── Token.php
│ ├── TokenParser/
│ │ ├── AbstractTokenParser.php
│ │ ├── ApplyTokenParser.php
│ │ ├── AutoEscapeTokenParser.php
│ │ ├── BlockTokenParser.php
│ │ ├── DeprecatedTokenParser.php
│ │ ├── DoTokenParser.php
│ │ ├── EmbedTokenParser.php
│ │ ├── ExtendsTokenParser.php
│ │ ├── FlushTokenParser.php
│ │ ├── ForTokenParser.php
│ │ ├── FromTokenParser.php
│ │ ├── GuardTokenParser.php
│ │ ├── IfTokenParser.php
│ │ ├── ImportTokenParser.php
│ │ ├── IncludeTokenParser.php
│ │ ├── MacroTokenParser.php
│ │ ├── SandboxTokenParser.php
│ │ ├── SetTokenParser.php
│ │ ├── TokenParserInterface.php
│ │ ├── TypesTokenParser.php
│ │ ├── UseTokenParser.php
│ │ └── WithTokenParser.php
│ ├── TokenStream.php
│ ├── TwigCallableInterface.php
│ ├── TwigFilter.php
│ ├── TwigFunction.php
│ ├── TwigTest.php
│ └── Util/
│ ├── CallableArgumentsExtractor.php
│ ├── DeprecationCollector.php
│ ├── ReflectionCallable.php
│ └── TemplateDirIterator.php
└── tests/
├── Cache/
│ ├── ChainTest.php
│ ├── FilesystemTest.php
│ └── ReadOnlyFilesystemTest.php
├── CompilerTest.php
├── ContainerRuntimeLoaderTest.php
├── CustomExtensionTest.php
├── DeprecatedCallableInfoTest.php
├── DummyBackedEnum.php
├── DummyUnitEnum.php
├── EnvironmentTest.php
├── ErrorTest.php
├── ExpressionParserTest.php
├── Extension/
│ ├── AttributeExtensionTest.php
│ ├── CoreTest.php
│ ├── EscaperTest.php
│ ├── Fixtures/
│ │ ├── ExtensionWithAttributes.php
│ │ ├── FilterWithoutValue.php
│ │ └── TestWithoutValue.php
│ ├── LegacyDebugFunctionsTest.php
│ ├── LegacyStringLoaderFunctionsTest.php
│ ├── SandboxTest.php
│ └── StringLoaderExtensionTest.php
├── FactoryRuntimeLoaderTest.php
├── FileExtensionEscapingStrategyTest.php
├── FilesystemHelper.php
├── Fixtures/
│ ├── autoescape/
│ │ ├── block.test
│ │ └── name.test
│ ├── errors/
│ │ ├── base.html
│ │ ├── extends/
│ │ │ ├── include.twig
│ │ │ └── index.twig
│ │ ├── index.html
│ │ ├── no_line_and_context_exception.twig
│ │ ├── no_line_and_context_exception_include_line_1.twig
│ │ └── no_line_and_context_exception_include_line_5.twig
│ ├── exceptions/
│ │ ├── child_contents_outside_blocks.test
│ │ ├── exception_in_extension_extends.test
│ │ ├── exception_in_extension_include.test
│ │ ├── multiline_array_with_undefined_variable.test
│ │ ├── multiline_array_with_undefined_variable_again.test
│ │ ├── multiline_function_with_undefined_variable.test
│ │ ├── multiline_function_with_unknown_argument.test
│ │ ├── multiline_tag_with_undefined_variable.test
│ │ ├── syntax_error_in_reused_template.test
│ │ ├── unclosed_tag.test
│ │ ├── undefined_parent.test
│ │ ├── undefined_template_in_child_template.test
│ │ └── undefined_trait.test
│ ├── expressions/
│ │ ├── _self.test
│ │ ├── array.test
│ │ ├── array_call.test
│ │ ├── attributes.test
│ │ ├── binary.test
│ │ ├── bitwise.test
│ │ ├── call_argument_defined_twice.test
│ │ ├── call_argument_unpacking.test
│ │ ├── call_argument_unpacking_before_normal.test
│ │ ├── call_positional_arg_after_named_arg.test
│ │ ├── comparison.test
│ │ ├── comparison_precedence.test
│ │ ├── const.test
│ │ ├── divisibleby.test
│ │ ├── dot_as_concatenation.test
│ │ ├── dotdot.test
│ │ ├── dynamic_attribute.test
│ │ ├── ends_with.test
│ │ ├── exponential_numbers.test
│ │ ├── floats.test
│ │ ├── grouping.test
│ │ ├── has_every.test
│ │ ├── has_some.test
│ │ ├── literals.test
│ │ ├── magic_call.test
│ │ ├── matches.test
│ │ ├── matches_error_compilation.test
│ │ ├── matches_error_runtime.test
│ │ ├── method_call.test
│ │ ├── negative_numbers.test
│ │ ├── not.test
│ │ ├── not_arrow_fn.test
│ │ ├── operators_as_variables.test
│ │ ├── postfix.test
│ │ ├── power.test
│ │ ├── sameas.test
│ │ ├── set.test
│ │ ├── spread_array_operator.test
│ │ ├── spread_mapping_operator.test
│ │ ├── spread_ternary_precedence.test
│ │ ├── starts_with.test
│ │ ├── string_operator_as_var_assignment.test
│ │ ├── strings.test
│ │ ├── ternary_operator.test
│ │ ├── ternary_operator_noelse.test
│ │ ├── ternary_operator_nothen.test
│ │ ├── two_word_operators_as_variables.test
│ │ ├── unary.test
│ │ ├── unary_macro_arguments.test
│ │ ├── unary_precedence.test
│ │ ├── underscored_numbers.test
│ │ └── underscored_numbers_error.test
│ ├── extensions/
│ │ └── anonymous_functions.test
│ ├── filters/
│ │ ├── abs.test
│ │ ├── arrow_reserved_names.test
│ │ ├── batch.test
│ │ ├── batch_float.test
│ │ ├── batch_with_empty_fill.test
│ │ ├── batch_with_exact_elements.test
│ │ ├── batch_with_fill.test
│ │ ├── batch_with_keys.test
│ │ ├── batch_with_more_elements.test
│ │ ├── batch_with_zero_elements.test
│ │ ├── capitalize.test
│ │ ├── column.test
│ │ ├── convert_encoding.test
│ │ ├── date.test
│ │ ├── date_default_format.test
│ │ ├── date_default_format_interval.test
│ │ ├── date_immutable.test
│ │ ├── date_interval.test
│ │ ├── date_modify.test
│ │ ├── date_namedargs.test
│ │ ├── date_time_zone_conversion.test
│ │ ├── default.test
│ │ ├── dynamic_filter.test
│ │ ├── escape.test
│ │ ├── escape_html_attr.test
│ │ ├── escape_html_attr_relaxed.test
│ │ ├── escape_javascript.test
│ │ ├── escape_non_supported_charset.test
│ │ ├── filter.test
│ │ ├── find.test
│ │ ├── first.test
│ │ ├── force_escape.test
│ │ ├── format.test
│ │ ├── invoke.test
│ │ ├── join.test
│ │ ├── json_encode.test
│ │ ├── last.test
│ │ ├── length.test
│ │ ├── length_utf8.test
│ │ ├── lower.test
│ │ ├── map.test
│ │ ├── merge.test
│ │ ├── nl2br.test
│ │ ├── number_format.test
│ │ ├── number_format_default.test
│ │ ├── raw.test
│ │ ├── reduce.test
│ │ ├── reduce_key.test
│ │ ├── replace.test
│ │ ├── replace_invalid_arg.test
│ │ ├── reverse.test
│ │ ├── round.test
│ │ ├── shuffle.test
│ │ ├── slice.test
│ │ ├── sort.test
│ │ ├── sort_with_arrow.test
│ │ ├── spaceless.legacy.test
│ │ ├── special_chars.test
│ │ ├── split.test
│ │ ├── split_utf8.test
│ │ ├── static_calls.test
│ │ ├── striptags.test
│ │ ├── title.test
│ │ ├── trailing_commas.test
│ │ ├── trim.test
│ │ ├── upper.test
│ │ └── urlencode.test
│ ├── functions/
│ │ ├── attribute.legacy.test
│ │ ├── attribute_with_wrong_args.legacy.test
│ │ ├── block.test
│ │ ├── block_with_template.test
│ │ ├── block_without_name.test
│ │ ├── block_without_parent.test
│ │ ├── constant.test
│ │ ├── cycle.test
│ │ ├── cycle_empty_mapping.test
│ │ ├── cycle_empty_sequence.test
│ │ ├── cycle_without_enough_args.test
│ │ ├── date.test
│ │ ├── date_namedargs.test
│ │ ├── deprecated.test
│ │ ├── dump.test
│ │ ├── dump_array.test
│ │ ├── dynamic_function.test
│ │ ├── enum/
│ │ │ ├── invalid_dynamic_enum.test
│ │ │ ├── invalid_enum.test
│ │ │ ├── invalid_enum_escaping.test
│ │ │ ├── invalid_literal_type.test
│ │ │ └── valid.test
│ │ ├── enum_cases/
│ │ │ ├── invalid_dynamic_enum.test
│ │ │ ├── invalid_enum.test
│ │ │ ├── invalid_enum_escaping.test
│ │ │ ├── invalid_literal_type.test
│ │ │ └── valid.test
│ │ ├── include/
│ │ │ ├── assignment.test
│ │ │ ├── autoescaping.test
│ │ │ ├── basic.test
│ │ │ ├── expression.test
│ │ │ ├── ignore_missing.test
│ │ │ ├── ignore_missing_exists.test
│ │ │ ├── include_missing_extends.test
│ │ │ ├── missing.test
│ │ │ ├── missing_nested.test
│ │ │ ├── sandbox.test
│ │ │ ├── sandbox_disabling.test
│ │ │ ├── sandbox_disabling_ignore_missing.test
│ │ │ ├── template_instance.test
│ │ │ ├── templates_as_array.test
│ │ │ ├── with_context.test
│ │ │ └── with_variables.test
│ │ ├── include_template_from_string.test
│ │ ├── magic_call.test
│ │ ├── magic_static_call.test
│ │ ├── max.test
│ │ ├── max_without_args.test
│ │ ├── min.test
│ │ ├── parent_in_condition.test
│ │ ├── parent_outside_of_a_block.test
│ │ ├── range.test
│ │ ├── recursive_block_with_inheritance.test
│ │ ├── source.test
│ │ ├── special_chars.test
│ │ ├── static_calls.test
│ │ ├── template_from_string.test
│ │ ├── template_from_string_error.test
│ │ ├── template_from_string_error_php80.test
│ │ ├── trailing_commas.test
│ │ ├── undefined_block.test
│ │ └── undefined_block_deep.test
│ ├── macros/
│ │ ├── arrow_as_arg.test
│ │ ├── default_values.test
│ │ ├── macro_with_capture.test
│ │ ├── nested_calls.test
│ │ ├── reserved_variables.test
│ │ ├── simple.test
│ │ ├── trailing_commas.test
│ │ ├── unknown_macro.test
│ │ ├── unknown_macro_different_template.test
│ │ ├── varargs.test
│ │ ├── varargs_argument.test
│ │ └── with_filters.test
│ ├── operators/
│ │ ├── concat_vs_add_sub.test
│ │ ├── contat_vs_add_sub.legacy.test
│ │ ├── minus_vs_pipe.legacy.test
│ │ ├── not_precedence.legacy.test
│ │ └── not_precedence.test
│ ├── regression/
│ │ ├── 4029-iterator_to_array.test
│ │ ├── 4033-missing-unwrap.test
│ │ ├── 4701-block-inheritance-issue.test
│ │ ├── block_names_unicity.test
│ │ ├── combined_debug_info.test
│ │ ├── empty_token.test
│ │ ├── markup_test.test
│ │ ├── multi_word_tests.test
│ │ ├── simple_xml_element.test
│ │ └── strings_like_numbers.test
│ ├── tags/
│ │ ├── apply/
│ │ │ ├── basic.test
│ │ │ ├── json_encode.test
│ │ │ ├── multiple.test
│ │ │ ├── nested.test
│ │ │ ├── scope.test
│ │ │ ├── with_for_tag.test
│ │ │ └── with_if_tag.test
│ │ ├── autoescape/
│ │ │ ├── basic.test
│ │ │ ├── blocks.test
│ │ │ ├── double_escaping.test
│ │ │ ├── functions.test
│ │ │ ├── literal.test
│ │ │ ├── nested.test
│ │ │ ├── objects.test
│ │ │ ├── raw.test
│ │ │ ├── strategy.test
│ │ │ ├── type.test
│ │ │ ├── with_filters.test
│ │ │ ├── with_filters_arguments.test
│ │ │ ├── with_pre_escape_filters.test
│ │ │ └── with_preserves_safety_filters.test
│ │ ├── block/
│ │ │ ├── basic.test
│ │ │ ├── block_unique_name.test
│ │ │ ├── conditional_block.test
│ │ │ └── special_chars.test
│ │ ├── deprecated/
│ │ │ ├── block.legacy.test
│ │ │ ├── macro.legacy.test
│ │ │ ├── template.legacy.test
│ │ │ ├── with_package.legacy.test
│ │ │ └── with_package_version.legacy.test
│ │ ├── embed/
│ │ │ ├── basic.test
│ │ │ ├── complex_dynamic_parent.test
│ │ │ ├── dynamic_parent.test
│ │ │ ├── embed_ignore_missing.test
│ │ │ ├── error_line.test
│ │ │ ├── multiple.test
│ │ │ ├── nested.test
│ │ │ └── with_extends.test
│ │ ├── for/
│ │ │ ├── context.test
│ │ │ ├── else.test
│ │ │ ├── for_on_strings.test
│ │ │ ├── inner_variables.test
│ │ │ ├── keys.test
│ │ │ ├── keys_and_values.test
│ │ │ ├── loop_context.test
│ │ │ ├── loop_context_local.test
│ │ │ ├── nested_else.test
│ │ │ ├── objects.test
│ │ │ ├── objects_countable.test
│ │ │ ├── recursive.test
│ │ │ ├── reserved_names.test
│ │ │ └── values.test
│ │ ├── from.test
│ │ ├── guard/
│ │ │ ├── basic.test
│ │ │ ├── exception.test
│ │ │ ├── nested.test
│ │ │ └── throwing_handler.test
│ │ ├── if/
│ │ │ ├── basic.test
│ │ │ ├── empty_body.test
│ │ │ └── expression.test
│ │ ├── include/
│ │ │ ├── basic.test
│ │ │ ├── expression.test
│ │ │ ├── ignore_missing.test
│ │ │ ├── ignore_missing_exists.test
│ │ │ ├── include_missing_extends.test
│ │ │ ├── missing.test
│ │ │ ├── missing_nested.test
│ │ │ ├── only.test
│ │ │ ├── template_instance.test
│ │ │ ├── templates_as_array.test
│ │ │ └── with_variables.test
│ │ ├── inheritance/
│ │ │ ├── basic.test
│ │ │ ├── block_expr.test
│ │ │ ├── block_expr2.test
│ │ │ ├── capturing_block.test
│ │ │ ├── conditional.test
│ │ │ ├── conditional_block.test
│ │ │ ├── conditional_block_nested.test
│ │ │ ├── dynamic.test
│ │ │ ├── dynamic_parent_from_include.test
│ │ │ ├── empty.test
│ │ │ ├── extends_as_array.test
│ │ │ ├── extends_as_array_with_empty_name.test
│ │ │ ├── extends_as_array_with_nested_blocks.test
│ │ │ ├── extends_in_block.test
│ │ │ ├── extends_in_macro.test
│ │ │ ├── extends_with_nested_blocks.test
│ │ │ ├── multiple.test
│ │ │ ├── multiple_dynamic.test
│ │ │ ├── nested_blocks.test
│ │ │ ├── nested_blocks_parent_only.test
│ │ │ ├── nested_inheritance.test
│ │ │ ├── parent.test
│ │ │ ├── parent_as_template_wrapper.test
│ │ │ ├── parent_change.test
│ │ │ ├── parent_isolation.test
│ │ │ ├── parent_nested.test
│ │ │ ├── parent_without_extends.test
│ │ │ ├── parent_without_extends_but_traits.test
│ │ │ ├── template_instance.test
│ │ │ └── use.test
│ │ ├── macro/
│ │ │ ├── argument_reserved_names.test
│ │ │ ├── auto_import.test
│ │ │ ├── auto_import_blocks.test
│ │ │ ├── auto_import_without_blocks.test
│ │ │ ├── basic.test
│ │ │ ├── colon_not_supported_as_default_separator.test
│ │ │ ├── endmacro_name.test
│ │ │ ├── external.test
│ │ │ ├── from.test
│ │ │ ├── from_embed_with_global_macro.test
│ │ │ ├── from_in_block_is_local.test
│ │ │ ├── from_local_override.test
│ │ │ ├── from_macro_in_a_macro.test
│ │ │ ├── from_macros_in_parent.test
│ │ │ ├── from_nested_blocks.test
│ │ │ ├── from_nested_blocks_with_global_macro.test
│ │ │ ├── from_recursive.test
│ │ │ ├── from_reserved_names.test
│ │ │ ├── from_self_parent.test
│ │ │ ├── from_syntax_error.test
│ │ │ ├── global.test
│ │ │ ├── import_and_blocks.test
│ │ │ ├── import_embed_with_global_macro.test
│ │ │ ├── import_from_string_template.test
│ │ │ ├── import_in_block_is_local.test
│ │ │ ├── import_local_override.test
│ │ │ ├── import_macro_in_a_macro.test
│ │ │ ├── import_macros_in_parent.test
│ │ │ ├── import_nested_blocks.test
│ │ │ ├── import_nested_blocks_with_global_macro.test
│ │ │ ├── import_reserved_names.test
│ │ │ ├── import_same_parent_and_child.test
│ │ │ ├── import_self_parent.test
│ │ │ ├── import_syntax_error.test
│ │ │ ├── named_arguments.test
│ │ │ ├── self_import.test
│ │ │ ├── special_chars.test
│ │ │ └── super_globals.test
│ │ ├── sandbox/
│ │ │ ├── array.legacy.test
│ │ │ ├── not_valid1.legacy.test
│ │ │ ├── not_valid2.legacy.test
│ │ │ └── simple.legacy.test
│ │ ├── set/
│ │ │ ├── basic.test
│ │ │ ├── capture-empty.test
│ │ │ ├── capture.test
│ │ │ ├── capture_scope.test
│ │ │ ├── expression.test
│ │ │ ├── inheritance.test
│ │ │ ├── inheritance_overriding.test
│ │ │ ├── mutating.test
│ │ │ └── reserved_names.test
│ │ ├── special_chars.test
│ │ ├── use/
│ │ │ ├── aliases.test
│ │ │ ├── basic.test
│ │ │ ├── deep.test
│ │ │ ├── deep_empty.test
│ │ │ ├── inheritance.test
│ │ │ ├── inheritance2.test
│ │ │ ├── multiple.test
│ │ │ ├── multiple_aliases.test
│ │ │ ├── parent_block.test
│ │ │ ├── parent_block2.test
│ │ │ ├── parent_block3.test
│ │ │ ├── use_aliased_block_overridden.test
│ │ │ └── use_with_parent.test
│ │ ├── verbatim/
│ │ │ ├── basic.test
│ │ │ └── whitespace_control.test
│ │ └── with/
│ │ ├── basic.test
│ │ ├── expression.test
│ │ ├── globals.test
│ │ ├── iterable.test
│ │ ├── nested.test
│ │ ├── with_no_mapping.test
│ │ └── with_only.test
│ ├── tests/
│ │ ├── array.test
│ │ ├── constant.test
│ │ ├── defined.test
│ │ ├── defined_for_attribute.legacy.test
│ │ ├── defined_for_attribute.test
│ │ ├── defined_for_blocks.test
│ │ ├── defined_for_blocks_with_template.test
│ │ ├── defined_for_constants.test
│ │ ├── defined_for_macros.test
│ │ ├── defined_on_complex_expr.test
│ │ ├── dynamic_test.test
│ │ ├── empty.test
│ │ ├── even.test
│ │ ├── in.test
│ │ ├── in_with_iterator.test
│ │ ├── in_with_objects.test
│ │ ├── iterable.test
│ │ ├── mapping.test
│ │ ├── null_coalesce.legacy.test
│ │ ├── null_coalesce.test
│ │ ├── null_coalesce_block.test
│ │ ├── odd.test
│ │ └── sequence.test
│ └── whitespace/
│ ├── trim_block.test
│ ├── trim_delimiter_as_strings.test
│ ├── trim_left.test
│ ├── trim_line_left.test
│ ├── trim_line_right.test
│ └── trim_right.test
├── IntegrationTest.php
├── LexerTest.php
├── Loader/
│ ├── ArrayTest.php
│ ├── ChainTest.php
│ ├── FilesystemTest.php
│ └── Fixtures/
│ ├── inheritance/
│ │ ├── array_inheritance_empty_parent.html.twig
│ │ ├── array_inheritance_nonexistent_parent.html.twig
│ │ ├── array_inheritance_valid_parent.html.twig
│ │ ├── parent.html.twig
│ │ └── spare_parent.html.twig
│ ├── named/
│ │ └── index.html
│ ├── named_bis/
│ │ └── index.html
│ ├── named_final/
│ │ └── index.html
│ ├── named_quater/
│ │ └── named_absolute.html
│ ├── named_ter/
│ │ └── index.html
│ ├── normal/
│ │ └── index.html
│ ├── normal_bis/
│ │ └── index.html
│ ├── normal_final/
│ │ └── index.html
│ ├── normal_ter/
│ │ └── index.html
│ ├── phar/
│ │ └── phar-sample.phar
│ └── themes/
│ ├── theme1/
│ │ └── blocks.html.twig
│ └── theme2/
│ └── blocks.html.twig
├── Node/
│ ├── AutoEscapeTest.php
│ ├── BlockReferenceTest.php
│ ├── BlockTest.php
│ ├── DeprecatedTest.php
│ ├── DoTest.php
│ ├── EmbedTest.php
│ ├── Expression/
│ │ ├── ArrayTest.php
│ │ ├── Binary/
│ │ │ ├── AddTest.php
│ │ │ ├── AndTest.php
│ │ │ ├── ConcatTest.php
│ │ │ ├── DivTest.php
│ │ │ ├── FloorDivTest.php
│ │ │ ├── ModTest.php
│ │ │ ├── MulTest.php
│ │ │ ├── NullCoalesceTest.php
│ │ │ ├── OrTest.php
│ │ │ └── SubTest.php
│ │ ├── CallTest.php
│ │ ├── ConditionalTest.php
│ │ ├── ConstantTest.php
│ │ ├── Filter/
│ │ │ └── RawTest.php
│ │ ├── FilterTest.php
│ │ ├── FilterTestExtension.php
│ │ ├── FunctionTest.php
│ │ ├── GetAttrTest.php
│ │ ├── NullCoalesceTest.php
│ │ ├── ParentTest.php
│ │ ├── Ternary/
│ │ │ └── ConditionalTernaryTest.php
│ │ ├── TestTest.php
│ │ ├── Unary/
│ │ │ ├── NegTest.php
│ │ │ ├── NotTest.php
│ │ │ └── PosTest.php
│ │ └── Variable/
│ │ ├── AssignContextVariableTest.php
│ │ └── ContextVariableTest.php
│ ├── ForTest.php
│ ├── IfTest.php
│ ├── ImportTest.php
│ ├── IncludeTest.php
│ ├── MacroTest.php
│ ├── ModuleTest.php
│ ├── NodeTest.php
│ ├── PrintTest.php
│ ├── SandboxTest.php
│ ├── SetTest.php
│ ├── TextTest.php
│ └── TypesTest.php
├── NodeVisitor/
│ ├── OptimizerTest.php
│ └── SandboxTest.php
├── ParserTest.php
├── Profiler/
│ ├── Dumper/
│ │ ├── BlackfireTest.php
│ │ ├── HtmlTest.php
│ │ ├── ProfilerTestCase.php
│ │ └── TextTest.php
│ └── ProfileTest.php
├── Runtime/
│ └── EscaperRuntimeTest.php
├── TemplateTest.php
├── TemplateWrapperTest.php
├── TokenParser/
│ ├── GuardTokenParserTest.php
│ └── TypesTokenParserTest.php
├── TokenStreamTest.php
├── Util/
│ ├── CallableArgumentsExtractorTest.php
│ └── DeprecationCollectorTest.php
└── drupal_test.sh
================================================
FILE CONTENTS
================================================
================================================
FILE: .editorconfig
================================================
; top-most EditorConfig file
root = true
; Unix-style newlines
[*]
end_of_line = LF
[*.php]
indent_style = space
indent_size = 4
[*.test]
indent_style = space
indent_size = 4
[*.rst]
indent_style = space
indent_size = 4
================================================
FILE: .gitattributes
================================================
/bin/ export-ignore
/doc/ export-ignore
/extra/ export-ignore
/tests/ export-ignore
/.editorconfig export-ignore
/.git* export-ignore
/.php-cs-fixer.dist.php export-ignore
/phpunit.xml.dist export-ignore
/phpstan.neon.dist export-ignore
/phpstan-baseline.neon export-ignore
/splitsh.json export-ignore
================================================
FILE: .github/dependabot.yml
================================================
version: 2
updates:
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "weekly"
================================================
FILE: .github/workflows/ci.yml
================================================
name: "CI"
on:
pull_request:
push:
env:
SYMFONY_PHPUNIT_DISABLE_RESULT_CACHE: 1
permissions:
contents: read
jobs:
tests:
name: "PHP ${{ matrix.php-version }}"
runs-on: 'ubuntu-latest'
strategy:
matrix:
php-version:
- '8.1'
- '8.2'
- '8.3'
- '8.4'
steps:
- name: "Checkout code"
uses: actions/checkout@v4
- name: "Install PHP with extensions"
uses: shivammathur/setup-php@v2
with:
coverage: "none"
php-version: ${{ matrix.php-version }}
ini-values: memory_limit=-1
- name: "Add PHPUnit matcher"
run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"
- run: composer install
- name: "Switch use_yield to true on PHP ${{ matrix.php-version }}"
if: "matrix.php-version == '8.2'"
run: |
sed -i -e "s/'use_yield' => false/'use_yield' => true/" src/Environment.php
- name: "Install PHPUnit"
run: vendor/bin/simple-phpunit install
- name: "PHPUnit version"
run: vendor/bin/simple-phpunit --version
- name: "Run tests"
run: vendor/bin/simple-phpunit
extension-tests:
needs:
- 'tests'
name: "${{ matrix.extension }} PHP ${{ matrix.php-version }}"
runs-on: 'ubuntu-latest'
continue-on-error: true
strategy:
matrix:
php-version:
- '8.1'
- '8.2'
- '8.3'
- '8.4'
extension:
- 'cache-extra'
- 'cssinliner-extra'
- 'html-extra'
- 'inky-extra'
- 'intl-extra'
- 'markdown-extra'
- 'string-extra'
- 'twig-extra-bundle'
steps:
- name: "Checkout code"
uses: actions/checkout@v4
- name: "Install PHP with extensions"
uses: shivammathur/setup-php@v2
with:
coverage: "none"
php-version: ${{ matrix.php-version }}
ini-values: memory_limit=-1
- name: "Add PHPUnit matcher"
run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"
- name: "Composer install Twig"
run: composer install
- name: "Install PHPUnit"
run: vendor/bin/simple-phpunit install
- name: "PHPUnit version"
run: vendor/bin/simple-phpunit --version
- name: "Prevent installing symfony/translation-contracts 3.0"
if: "matrix.extension == 'twig-extra-bundle'"
working-directory: extra/${{ matrix.extension }}
run: "composer require --no-update 'symfony/translation-contracts:^1.1|^2.0'"
- name: "Composer install ${{ matrix.extension }}"
working-directory: extra/${{ matrix.extension }}
run: composer install
- name: "Switch use_yield to true"
if: "matrix.php-version == '8.2'"
run: |
sed -i -e "s/'use_yield' => false/'use_yield' => true/" extra/${{ matrix.extension }}/vendor/twig/twig/src/Environment.php
- name: "Run tests for ${{ matrix.extension }}"
working-directory: extra/${{ matrix.extension }}
run: ../../vendor/bin/simple-phpunit
integration-tests:
needs:
- 'tests'
name: "Integration tests with PHP ${{ matrix.php-version }}"
runs-on: 'ubuntu-latest'
continue-on-error: true
strategy:
matrix:
php-version:
- '8.2'
steps:
- name: "Checkout code"
uses: actions/checkout@v4
- name: "Install PHP with extensions"
uses: shivammathur/setup-php@v2
with:
coverage: "none"
extensions: "gd, pdo_sqlite, uuid"
php-version: ${{ matrix.php-version }}
ini-values: memory_limit=-1
tools: composer:v2
- run: bash ./tests/drupal_test.sh
shell: "bash"
phpstan:
name: "PHPStan"
runs-on: 'ubuntu-latest'
strategy:
matrix:
php-version:
- '8.4'
steps:
- name: "Checkout code"
uses: actions/checkout@v4
- name: "Install PHP with extensions"
uses: shivammathur/setup-php@v2
with:
coverage: "none"
php-version: ${{ matrix.php-version }}
ini-values: memory_limit=-1
- run: composer install
- name: "Run tests"
run: vendor/bin/phpstan
================================================
FILE: .github/workflows/documentation.yml
================================================
name: "Documentation"
on:
pull_request:
push:
permissions:
contents: read
jobs:
build:
name: "Build"
runs-on: ubuntu-latest
steps:
- name: "Checkout code"
uses: actions/checkout@v4
- name: "Set-up PHP"
uses: shivammathur/setup-php@v2
with:
php-version: 8.2
coverage: none
tools: "composer:v2"
- name: Get composer cache directory
id: composercache
working-directory: doc/_build
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT
- name: Cache dependencies
uses: actions/cache@v4
with:
path: ${{ steps.composercache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: ${{ runner.os }}-composer-
- name: "Install dependencies"
working-directory: doc/_build
run: composer install --prefer-dist --no-progress
- name: "Build the docs"
working-directory: doc/_build
run: php build.php --disable-cache
doctor-rst:
name: "DOCtor-RST"
runs-on: ubuntu-latest
steps:
- name: "Checkout code"
uses: actions/checkout@v4
- name: "Run DOCtor-RST"
uses: docker://oskarstark/doctor-rst
with:
args: --short
env:
DOCS_DIR: 'doc/'
================================================
FILE: .github/workflows/fabbot.yml
================================================
name: CS
on:
pull_request:
permissions:
contents: read
jobs:
call-fabbot:
name: Fabbot
uses: symfony-tools/fabbot/.github/workflows/fabbot.yml@main
with:
package: Twig
================================================
FILE: .gitignore
================================================
/doc/_build/vendor
/doc/_build/output
/composer.lock
/phpunit.xml
/vendor
.phpunit.result.cache
================================================
FILE: .php-cs-fixer.dist.php
================================================
setRules([
'@Symfony' => true,
'@Symfony:risky' => true,
'@PHPUnit75Migration:risky' => true,
'php_unit_dedicate_assert' => ['target' => '5.6'],
'array_syntax' => ['syntax' => 'short'],
'php_unit_fqcn_annotation' => true,
'no_unreachable_default_argument_value' => false,
'braces' => ['allow_single_line_closure' => true],
'heredoc_to_nowdoc' => false,
'single_line_throw' => false,
'ordered_imports' => true,
'phpdoc_types_order' => ['null_adjustment' => 'always_last', 'sort_algorithm' => 'none'],
'no_superfluous_phpdoc_tags' => ['allow_mixed' => true],
])
->setRiskyAllowed(true)
->setParallelConfig(ParallelConfigFactory::detect())
->setFinder((new Finder())->in(__DIR__))
;
================================================
FILE: CHANGELOG
================================================
# 3.24.1 (2026-XX-XX)
* n/a
# 3.24.0 (2026-03-17)
* Deprecate not implementing the `getOperatorTokens()` method in `ExpressionParserInterface` implementations
* Deprecate passing a non-`AbstractExpression` node to `Twig\Node\Expression\Binary\MatchesBinary` constructor
* Deprecate passing a non-`AbstractExpression` node to `Parser::setParent()`
* Add support for renaming variables in object destructuring (`{name: userName} = user`)
* Add `html_attr_relaxed` escaping strategy that preserves :, @, [, and ] for front-end framework attribute names
* Add support for short-circuiting in null-safe operator chains
* Add the `html_attr` function and `html_attr_merge` as well as `html_attr_type` filters
# 3.23.0 (2026-01-23)
* Add `=` assignment operator (allows to set variables in expression or to replace the short-form of the set tag)
* Add sequence, mapping, and object destructuring
* Add `?.` null-safe operator
* Add `===` and `!==` operators (equivalent to the `same as` and `not same as` tests)
* Fix opcache preload warning for unlinked anonymous class
* Fix spread operator behavior
# 3.22.2 (2025-12-14)
* Fix "cycle" with non-countable ArrayAccess + Traversable objects
* Use "getShareDir" as an indicator of Symfony version in Symfony bundle
* Fix escaper compatibility with PHP 8.5
# 3.22.1 (2025-11-16)
* Add support for Symfony 8
# 3.22.0 (2025-10-29)
* Add support for two words test in guard tag
* Add `Environment::registerUndefinedTestCallback()`
* Fix compatibility with Symfony 8
* Fix accessing arrays with stringable objects as key
* Avoid errors when failing to guess the template info for an error
* Fix expression parser compatibility layer
* Fix compiling 'index' with repr (not string) in EmbedNode
* Update configuration keys + allow extra keys for CommonMark extensions
* Allow usage of other Markdown converters than CommonMark in LeagueMarkdown
# 3.21.1 (2025-05-03)
* Fix ExtensionSet usage of BinaryOperatorExpressionParser
# 3.21.0 (2025-05-02)
* Fix wrong array index
* Deprecate `Template::loadTemplate()`
* Fix testing and expression when it evaluates to an instance of `Markup`
* Add `ReturnPrimitiveTypeInterface` (and sub-interfaces for number, boolean, string, and array)
* Add `SupportDefinedTestInterface` for expression nodes supporting the `defined` test
* Deprecate using the `|` operator in an expression with `+` or `-` without using parentheses to clarify precedence
* Deprecate operator precedence outside of the [0, 512] range
* Introduce expression parser classes to describe operators and operands provided by extensions
instead of arrays (it comes with many deprecations that are documented in
the ``deprecated`` documentation chapter)
* Deprecate the `Twig\ExpressionParser`, and `Twig\OperatorPrecedenceChange` classes
* Add attributes `AsTwigFilter`, `AsTwigFunction`, and `AsTwigTest` to ease extension development
# 3.20.0 (2025-02-13)
* Fix support for ignoring syntax errors in an undefined handler in guard
* Add configuration for Commonmark
* Fix wrong array index
* Bump minimum PHP version to 8.1
* Add support for registering callbacks for undefined functions, filters or token parsers in the IntegrationTestCase
* Use correct line number for `ForElseNode`
* Fix timezone conversion on strings
# 3.19.0 (2025-01-28)
* Fix a security issue where escaping was missing when using `??`
* Deprecate `Token::getType()`, use `Token::test()` instead
* Add `Token::toEnglish()`
* Add `ForElseNode`
* Deprecate `Twig\ExpressionParser::parseOnlyArguments()` and
`Twig\ExpressionParser::parseArguments()` (use
`Twig\ExpressionParser::parseNamedArguments()` instead)
* Fix `constant()` behavior when used with `??`
* Add the `invoke` filter
* Make `{}` optional for the `types` tag
* Add `LastModifiedExtensionInterface` and implementation in `AbstractExtension` to track modification of runtime classes
* Ignore static properties when using the dot operator
# 3.18.0 (2024-12-29)
* Support for invoking closures
* Fix unary operator precedence change
* Ignore `SyntaxError` exceptions from undefined handlers when using the `guard` tag
* Add a way to stream template rendering (`TemplateWrapper::stream()` and `TemplateWrapper::streamBlock()`)
# 3.17.1 (2024-12-12)
* Fix the null coalescing operator when the test returns null
* Fix the Elvis operator when used as '? :' instead of '?:'
# 3.17.0 (2024-12-10)
* Fix ArrayAccess with objects as keys
* Support underscores in number literals
* Deprecate `ConditionalExpression` and `NullCoalesceExpression` (use `ConditionalTernary` and `NullCoalesceBinary` instead)
# 3.16.0 (2024-11-29)
* Deprecate `InlinePrint`
* Fix having macro variables starting with an underscore
* Deprecate not passing a `Source` instance to `TokenStream`
* Deprecate returning `null` from `TwigFilter::getSafe()` and `TwigFunction::getSafe()`, return `[]` instead
# 3.15.0 (2024-11-17)
* [BC BREAK] Add support for accessing class constants with the dot operator;
this can be a BC break if you don't use UPPERCASE constant names
* Add Spanish inflector support for the `plural` and `singular` filters in the String extension
* Deprecate `TempNameExpression` in favor of `LocalVariable`
* Deprecate `NameExpression` in favor of `ContextVariable`
* Deprecate `AssignNameExpression` in favor of `AssignContextVariable`
* Remove `MacroAutoImportNodeVisitor`
* Deprecate `MethodCallExpression` in favor of `MacroReferenceExpression`
* Fix support for the "is defined" test on `_self.xxx` (auto-imported) macros
* Fix support for the "is defined" test on inherited macros
* Add named arguments support for the dot operator arguments (`foo.bar(some: arg)`)
* Add named arguments support for macros
* Add a new `guard` tag that allows to test if some Twig callables are available at compilation time
* Allow arrow functions everywhere
* Deprecate passing a string or an array to Twig callable arguments accepting arrow functions (pass a `\Closure`)
* Add support for triggering deprecations for future operator precedence changes
* Deprecate using the `not` unary operator in an expression with ``*``, ``/``, ``//``, or ``%`` without using explicit parentheses to clarify precedence
* Deprecate using the `??` binary operator without explicit parentheses
* Deprecate using the `~` binary operator in an expression with `+` or `-` without using parentheses to clarify precedence
* Deprecate not passing `AbstractExpression` args to most constructor arguments for classes extending `AbstractExpression`
* Fix `power` expressions with a negative number in parenthesis (`(-1) ** 2`)
* Deprecate instantiating `Node` directly. Use `EmptyNode` or `Nodes` instead.
* Add support for inline comments
* Add `Profile::getStartTime()` and `Profile::getEndTime()`
* Fix "ignore missing" when used on an "embed" tag
* Fix the possibility to override an aliased block (via use)
* Add template cache hot reload
* Allow Twig callable argument names to be free-form (snake-case or camelCase) independently of the PHP callable signature
They were automatically converted to snake-cased before
* Deprecate the `attribute` function; use the `.` notation and wrap the name with parenthesis instead
* Add support for argument unpackaging
* Add JSON support for the file extension escaping strategy
* Support Markup instances (and any other \Stringable) as dynamic mapping keys
* Deprecate the `sandbox` tag
* Improve the way one can deprecate a Twig callable (use `deprecation_info` instead of the other callable options)
* Add the `enum` function
* Add support for logical `xor` operator
# 3.14.2 (2024-11-07)
* Fix an infinite recursion in the sandbox code
# 3.14.1 (2024-11-06)
* [BC BREAK] Fix a security issue in the sandbox mode allowing an attacker to call attributes on Array-like objects
They are now checked via the property policy
* Fix a security issue in the sandbox mode allowing an attacker to be able to call `toString()`
under some circumstances on an object even if the `__toString()` method is not allowed by the security policy
# 3.14.0 (2024-09-09)
* Fix a security issue when an included sandboxed template has been loaded before without the sandbox context
* Add the possibility to reset globals via `Environment::resetGlobals()`
* Deprecate `Environment::mergeGlobals()`
# 3.13.0 (2024-09-07)
* Add the `types` tag (experimental)
* Deprecate the `Twig\Test\NodeTestCase::getTests()` data provider, override `provideTests()` instead.
* Mark `Twig\Test\NodeTestCase::getEnvironment()` as final, override `createEnvironment()` instead.
* Deprecate `Twig\Test\NodeTestCase::getVariableGetter()`, call `createVariableGetter()` instead.
* Deprecate `Twig\Test\NodeTestCase::getAttributeGetter()`, call `createAttributeGetter()` instead.
* Deprecate not overriding `Twig\Test\IntegrationTestCase::getFixturesDirectory()`, this method will be abstract in 4.0
* Marked `Twig\Test\IntegrationTestCase::getTests()` and `getLegacyTests()` as final
# 3.12.0 (2024-08-29)
* Deprecate the fact that the `extends` and `use` tags are always allowed in a sandboxed template.
This behavior will change in 4.0 where these tags will need to be explicitly allowed like any other tag.
* Deprecate the "tag" constructor argument of the "Twig\Node\Node" class as the tag is now automatically set by the Parser when needed
* Fix precedence of two-word tests when the first word is a valid test
* Deprecate the `spaceless` filter
* Deprecate some internal methods from `Parser`: `getBlockStack()`, `hasBlock()`, `getBlock()`, `hasMacro()`, `hasTraits()`, `getParent()`
* Deprecate passing `null` to `Twig\Parser::setParent()`
* Update `Node::__toString()` to include the node tag if set
* Add support for integers in methods of `Twig\Node\Node` that take a Node name
* Deprecate not passing a `BodyNode` instance as the body of a `ModuleNode` or `MacroNode` constructor
* Deprecate returning "null" from "TokenParserInterface::parse()".
* Deprecate `OptimizerNodeVisitor::OPTIMIZE_TEXT_NODES`
* Fix performance regression when `use_yield` is `false` (which is the default)
* Improve compatibility when `use_yield` is `false` (as extensions still using `echo` will work as is)
* Accept colons (`:`) in addition to equals (`=`) to separate argument names and values in named arguments
* Add the `html_cva` function (in the HTML extra package)
* Add support for named arguments to the `block` and `attribute` functions
* Throw a SyntaxError exception at compile time when a Twig callable has not the minimum number of required arguments
* Add a `CallableArgumentsExtractor` class
* Deprecate passing a name to `FunctionExpression`, `FilterExpression`, and `TestExpression`;
pass a `TwigFunction`, `TwigFilter`, or `TestFilter` instead
* Deprecate all Twig callable attributes on `FunctionExpression`, `FilterExpression`, and `TestExpression`
* Deprecate the `filter` node of `FilterExpression`
* Add the notion of Twig callables (functions, filters, and tests)
* Bump minimum PHP version to 8.0
* Fix integration tests when a test has more than one data/expect section and deprecations
* Add the `enum_cases` function
# 3.11.2 (2024-11-06)
* [BC BREAK] Fix a security issue in the sandbox mode allowing an attacker to call attributes on Array-like objects
They are now checked via the property policy
* Fix a security issue in the sandbox mode allowing an attacker to be able to call `toString()`
under some circumstances on an object even if the `__toString()` method is not allowed by the security policy
# 3.11.1 (2024-09-10)
* Fix a security issue when an included sandboxed template has been loaded before without the sandbox context
# 3.11.0 (2024-08-08)
* Deprecate `OptimizerNodeVisitor::OPTIMIZE_RAW_FILTER`
* Add `Twig\Cache\ChainCache` and `Twig\Cache\ReadOnlyFilesystemCache`
* Add the possibility to deprecate attributes and nodes on `Node`
* Add the possibility to add a package and a version to the `deprecated` tag
* Add the possibility to add a package for filter/function/test deprecations
* Mark `ConstantExpression` as being `@final`
* Add the `find` filter
* Fix optimizer mode validation in `OptimizerNodeVisitor`
* Add the possibility to yield from a generator in `PrintNode`
* Add the `shuffle` filter
* Add the `singular` and `plural` filters in `StringExtension`
* Deprecate the second argument of `Twig\Node\Expression\CallExpression::compileArguments()`
* Deprecate `Twig\ExpressionParser\parseHashExpression()` in favor of
`Twig\ExpressionParser::parseMappingExpression()`
* Deprecate `Twig\ExpressionParser\parseArrayExpression()` in favor of
`Twig\ExpressionParser::parseSequenceExpression()`
* Add `sequence` and `mapping` tests
* Deprecate `Twig\Node\Expression\NameExpression::isSimple()` and
`Twig\Node\Expression\NameExpression::isSpecial()`
# 3.10.3 (2024-05-16)
* Fix missing ; in generated code
# 3.10.2 (2024-05-14)
* Fix support for the deprecated escaper signature
# 3.10.1 (2024-05-12)
* Fix BC break on escaper extension
* Fix constant return type
# 3.10.0 (2024-05-11)
* Make `CoreExtension::formatDate`, `CoreExtension::convertDate`, and
`CoreExtension::formatNumber` part of the public API
* Add `needs_charset` option for filters and functions
* Extract the escaping logic from the `EscaperExtension` class to a new
`EscaperRuntime` class.
The following methods from ``Twig\\Extension\\EscaperExtension`` are
deprecated: ``setEscaper()``, ``getEscapers()``, ``setSafeClasses``,
``addSafeClasses()``. Use the same methods on the
``Twig\\Runtime\\EscaperRuntime`` class instead.
* Fix capturing output from extensions that still use echo
* Fix a PHP warning in the Lexer on malformed templates
* Fix blocks not available under some circumstances
* Synchronize source context in templates when setting a Node on a Node
# 3.9.3 (2024-04-18)
* Add missing `twig_escape_filter_is_safe` deprecated function
* Fix yield usage with CaptureNode
* Add missing unwrap call when using a TemplateWrapper instance internally
* Ensure Lexer is initialized early on
# 3.9.2 (2024-04-17)
* Fix usage of display_end hook
# 3.9.1 (2024-04-17)
* Fix missing `$blocks` variable in `CaptureNode`
# 3.9.0 (2024-04-16)
* Add support for PHP 8.4
* Deprecate AbstractNodeVisitor
* Deprecate passing Template to Environment::resolveTemplate(), Environment::load(), and Template::loadTemplate()
* Add a new "yield" mode for output generation;
Node implementations that use "echo" or "print" should use "yield" instead;
all Node implementations should be flagged with `#[YieldReady]` once they've been made ready for "yield";
the "use_yield" Environment option can be turned on when all nodes have been made `#[YieldReady]`;
"yield" will be the only strategy supported in the next major version
* Add return type for Symfony 7 compatibility
* Fix premature loop exit in Security Policy lookup of allowed methods/properties
* Deprecate all internal extension functions in favor of methods on the extension classes
* Mark all extension functions as @internal
* Add SourcePolicyInterface to selectively enable the Sandbox based on a template's Source
* Throw a proper Twig exception when using cycle on an empty array
# 3.8.0 (2023-11-21)
* Catch errors thrown during template rendering
* Fix IntlExtension::formatDateTime use of date formatter prototype
* Fix premature loop exit in Security Policy lookup of allowed methods/properties
* Remove NumberFormatter::TYPE_CURRENCY (deprecated in PHP 8.3)
* Restore return type annotations
* Allow Symfony 7 packages to be installed
* Deprecate `twig_test_iterable` function. Use the native `is_iterable` instead.
# 3.7.1 (2023-08-28)
* Fix some phpdocs
# 3.7.0 (2023-07-26)
* Add support for the ...spread operator on arrays and hashes
# 3.6.1 (2023-06-08)
* Suppress some native return type deprecation messages
# 3.6.0 (2023-05-03)
* Allow psr/container 2.0
* Add the new PHP 8.0 IntlDateFormatter::RELATIVE_* constants for date formatting
* Make the Lexer initialize itself lazily
# 3.5.1 (2023-02-08)
* Arrow functions passed to the "reduce" filter now accept the current key as a third argument
* Restores the leniency of the matches twig comparison
* Fix error messages in sandboxed mode for "has some" and "has every"
# 3.5.0 (2022-12-27)
* Make Twig\ExpressionParser non-internal
* Add "has some" and "has every" operators
* Add Compile::reset()
* Throw a better runtime error when the "matches" regexp is not valid
* Add "twig *_names" intl functions
* Fix optimizing closures callbacks
* Add a better exception when getting an undefined constant via `constant`
* Fix `if` nodes when outside of a block and with an empty body
# 3.4.3 (2022-09-28)
* Fix a security issue on filesystem loader (possibility to load a template outside a configured directory)
# 3.4.2 (2022-08-12)
* Allow inherited magic method to still run with calling class
* Fix CallExpression::reflectCallable() throwing TypeError
* Fix typo in naming (currency_code)
# 3.4.1 (2022-05-17)
* Fix optimizing non-public named closures
# 3.4.0 (2022-05-22)
* Add support for named closures
# 3.3.10 (2022-04-06)
* Enable bytecode invalidation when auto_reload is enabled
# 3.3.9 (2022-03-25)
* Fix custom escapers when using multiple Twig environments
* Add support for "constant('class', object)"
* Do not reuse internally generated variable names during parsing
# 3.3.8 (2022-02-04)
* Fix a security issue when in a sandbox: the `sort` filter must require a Closure for the `arrow` parameter
* Fix deprecation notice on `round`
* Fix call to deprecated `convertToHtml` method
# 3.3.7 (2022-01-03)
* Allow more null support when Twig expects a string (for better 8.1 support)
* Only use Commonmark extensions if markdown enabled
# 3.3.6 (2022-01-03)
* Only use Commonmark extensions if markdown enabled
# 3.3.5 (2022-01-03)
* Allow CommonMark extensions to easily be added
* Allow null when Twig expects a string (for better 8.1 support)
* Make some performance optimizations
* Allow Symfony translation contract v3+
# 3.3.4 (2021-11-25)
* Bump minimum supported Symfony component versions
* Fix a deprecated message
# 3.3.3 (2021-09-17)
* Allow Symfony 6
* Improve compatibility with PHP 8.1
* Explicitly specify the encoding for mb_ord in JS escaper
# 3.3.2 (2021-05-16)
* Revert "Throw a proper exception when a template name is an absolute path (as it has never been supported)"
# 3.3.1 (2021-05-12)
* Fix PHP 8.1 compatibility
* Throw a proper exception when a template name is an absolute path (as it has never been supported)
# 3.3.0 (2021-02-08)
* Fix macro calls in a "cache" tag
* Add the slug filter
* Allow extra bundle to be compatible with Twig 2
# 3.2.1 (2021-01-05)
* Fix extra bundle compat with older versions of Symfony
# 3.2.0 (2021-01-05)
* Add the Cache extension in the "extra" repositories: "cache" tag
* Add "registerUndefinedTokenParserCallback"
* Mark built-in node visitors as @internal
* Fix "odd" not working for negative numbers
# 3.1.1 (2020-10-27)
* Fix "include(template_from_string())"
# 3.1.0 (2020-10-21)
* Fix sandbox support when using "include(template_from_string())"
* Make round brackets optional for one argument tests like "same as" or "divisible by"
* Add support for ES2015 style object initialisation shortcut { a } is the same as { 'a': a }
# 3.0.5 (2020-08-05)
* Fix twig_compare w.r.t. whitespace trimming
* Fix sandbox not disabled if syntax error occurs within {% sandbox %} tag
* Fix a regression when not using a space before an operator
* Restrict callables to closures in filters
* Allow trailing commas in argument lists (in calls as well as definitions)
# 3.0.4 (2020-07-05)
* Fix comparison operators
* Fix options not taken into account when using "Michelf\MarkdownExtra"
* Fix "Twig\Extra\Intl\IntlExtension::getCountryName()" to accept "null" as a first argument
* Throw exception in case non-Traversable data is passed to "filter"
* Fix context optimization on PHP 7.4
* Fix PHP 8 compatibility
* Fix ambiguous syntax parsing
# 3.0.3 (2020-02-11)
* Add a check to ensure that iconv() is defined
# 3.0.2 (2020-02-11)
* Avoid exceptions when an intl resource is not found
* Fix implementation of case-insensitivity for method names
# 3.0.1 (2019-12-28)
* fixed Symfony 5.0 support for the HTML extra extension
# 3.0.0 (2019-11-15)
* fixed number formatter in Intl extra extension when using a formatter prototype
# 3.0.0-BETA1 (2019-11-11)
* removed the "if" condition support on the "for" tag
* made the in, <, >, <=, >=, ==, and != operators more strict when comparing strings and integers/floats
* removed the "filter" tag
* added type hints everywhere
* changed Environment::resolveTemplate() to always return a TemplateWrapper instance
* removed Template::__toString()
* removed Parser::isReservedMacroName()
* removed SanboxedPrintNode
* removed Node::setTemplateName()
* made classes marked as "@final" final
* removed InitRuntimeInterface, ExistsLoaderInterface, and SourceContextLoaderInterface
* removed the "spaceless" tag
* removed Twig\Environment::getBaseTemplateClass() and Twig\Environment::setBaseTemplateClass()
* removed the "base_template_class" option on Twig\Environment
* bumped minimum PHP version to 7.2
* removed PSR-0 classes
================================================
FILE: LICENSE
================================================
Copyright (c) 2009-present by the Twig Team.
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of Twig nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
================================================
FILE: README.rst
================================================
Twig, the flexible, fast, and secure template language for PHP
==============================================================
Twig is a template language for PHP.
Twig uses a syntax similar to the Django and Jinja template languages which
inspired the Twig runtime environment.
Sponsors
--------
.. raw:: html
More Information
----------------
Read the `documentation`_ for more information.
.. _documentation: https://twig.symfony.com/documentation
================================================
FILE: bin/generate_operators_precedence.php
================================================
getExpressionParsers() as $expressionParser) {
if (!$seen->offsetExists($expressionParser)) {
$expressionParsers[] = $expressionParser;
$seen->offsetSet($expressionParser, true);
$descriptionLength = max($descriptionLength, $expressionParser instanceof ExpressionParserDescriptionInterface ? strlen($expressionParser->getDescription()) : '');
}
}
fwrite($output, "\n+------------+------------------+---------+---------------+".str_repeat('-', $descriptionLength + 2)."+\n");
fwrite($output, '| Precedence | Operator | Type | Associativity | Description'.str_repeat(' ', $descriptionLength - 11)." |\n");
fwrite($output, '+============+==================+=========+===============+'.str_repeat('=', $descriptionLength + 2).'+');
usort($expressionParsers, static fn ($a, $b) => $b->getPrecedence() <=> $a->getPrecedence());
$previous = null;
foreach ($expressionParsers as $expressionParser) {
if (null !== $previous) {
fwrite($output, "\n+------------+------------------+---------+---------------+".str_repeat('-', $descriptionLength + 2).'+');
}
$precedence = $expressionParser->getPrecedence();
$previousPrecedence = $previous ? $previous->getPrecedence() : \PHP_INT_MAX;
$associativity = $expressionParser instanceof InfixExpressionParserInterface ? (InfixAssociativity::Left === $expressionParser->getAssociativity() ? 'Left' : 'Right') : 'n/a';
$previousAssociativity = $previous ? ($previous instanceof InfixExpressionParserInterface ? (InfixAssociativity::Left === $previous->getAssociativity() ? 'Left' : 'Right') : 'n/a') : 'n/a';
if ($previousPrecedence !== $precedence) {
$previous = null;
}
$operatorName = '``'.$expressionParser->getName().'``';
if ($expressionParser->getAliases()) {
$operatorName .= ', ``'.implode('``, ``', $expressionParser->getAliases()).'``';
}
fwrite($output, rtrim(sprintf("\n| %-10s | %-16s | %-7s | %-13s | %-{$descriptionLength}s |\n",
(!$previous || $previousPrecedence !== $precedence ? $precedence : '').($expressionParser->getPrecedenceChange() ? ' => '.$expressionParser->getPrecedenceChange()->getNewPrecedence() : ''),
$operatorName,
!$previous || ExpressionParserType::getType($previous) !== ExpressionParserType::getType($expressionParser) ? ExpressionParserType::getType($expressionParser)->value : '',
!$previous || $previousAssociativity !== $associativity ? $associativity : '',
$expressionParser instanceof ExpressionParserDescriptionInterface ? $expressionParser->getDescription() : '',
)));
$previous = $expressionParser;
}
fwrite($output, "\n+------------+------------------+---------+---------------+".str_repeat('-', $descriptionLength + 2)."+\n");
fwrite($output, "\nWhen a precedence will change in 4.0, the new precedence is indicated by the arrow ``=>``.\n");
fwrite($output, "\nHere is the same table for Twig 4.0 with adjusted precedences:\n");
fwrite($output, "\n+------------+------------------+---------+---------------+".str_repeat('-', $descriptionLength + 2)."+\n");
fwrite($output, '| Precedence | Operator | Type | Associativity | Description'.str_repeat(' ', $descriptionLength - 11)." |\n");
fwrite($output, '+============+==================+=========+===============+'.str_repeat('=', $descriptionLength + 2).'+');
usort($expressionParsers, static function ($a, $b) {
$aPrecedence = $a->getPrecedenceChange() ? $a->getPrecedenceChange()->getNewPrecedence() : $a->getPrecedence();
$bPrecedence = $b->getPrecedenceChange() ? $b->getPrecedenceChange()->getNewPrecedence() : $b->getPrecedence();
return $bPrecedence - $aPrecedence;
});
$previous = null;
foreach ($expressionParsers as $expressionParser) {
if (null !== $previous) {
fwrite($output, "\n+------------+------------------+---------+---------------+".str_repeat('-', $descriptionLength + 2).'+');
}
$precedence = $expressionParser->getPrecedenceChange() ? $expressionParser->getPrecedenceChange()->getNewPrecedence() : $expressionParser->getPrecedence();
$previousPrecedence = $previous ? ($previous->getPrecedenceChange() ? $previous->getPrecedenceChange()->getNewPrecedence() : $previous->getPrecedence()) : \PHP_INT_MAX;
$associativity = $expressionParser instanceof InfixExpressionParserInterface ? (InfixAssociativity::Left === $expressionParser->getAssociativity() ? 'Left' : 'Right') : 'n/a';
$previousAssociativity = $previous ? ($previous instanceof InfixExpressionParserInterface ? (InfixAssociativity::Left === $previous->getAssociativity() ? 'Left' : 'Right') : 'n/a') : 'n/a';
if ($previousPrecedence !== $precedence) {
$previous = null;
}
$operatorName = '``'.$expressionParser->getName().'``';
if ($expressionParser->getAliases()) {
$operatorName .= ', ``'.implode('``, ``', $expressionParser->getAliases()).'``';
}
fwrite($output, rtrim(sprintf("\n| %-10s | %-16s | %-7s | %-13s | %-{$descriptionLength}s |\n",
!$previous || $previousPrecedence !== $precedence ? $precedence : '',
$operatorName,
!$previous || ExpressionParserType::getType($previous) !== ExpressionParserType::getType($expressionParser) ? ExpressionParserType::getType($expressionParser)->value : '',
!$previous || $previousAssociativity !== $associativity ? $associativity : '',
$expressionParser instanceof ExpressionParserDescriptionInterface ? $expressionParser->getDescription() : '',
)));
$previous = $expressionParser;
}
fwrite($output, "\n+------------+------------------+---------+---------------+".str_repeat('-', $descriptionLength + 2)."+\n");
fclose($output);
================================================
FILE: composer.json
================================================
{
"name": "twig/twig",
"type": "library",
"description": "Twig, the flexible, fast, and secure template language for PHP",
"keywords": ["templating"],
"homepage": "https://twig.symfony.com",
"license": "BSD-3-Clause",
"minimum-stability": "dev",
"authors": [
{
"name": "Fabien Potencier",
"email": "fabien@symfony.com",
"homepage": "http://fabien.potencier.org",
"role": "Lead Developer"
},
{
"name": "Twig Team",
"role": "Contributors"
},
{
"name": "Armin Ronacher",
"email": "armin.ronacher@active-4.com",
"role": "Project Founder"
}
],
"require": {
"php": ">=8.1.0",
"symfony/deprecation-contracts": "^2.5|^3",
"symfony/polyfill-mbstring": "^1.3",
"symfony/polyfill-ctype": "^1.8"
},
"require-dev": {
"symfony/phpunit-bridge": "^5.4.9|^6.4|^7.0",
"psr/container": "^1.0|^2.0",
"phpstan/phpstan": "^2.0@stable",
"php-cs-fixer/shim": "^3.0@stable"
},
"autoload": {
"files": [
"src/Resources/core.php",
"src/Resources/debug.php",
"src/Resources/escaper.php",
"src/Resources/string_loader.php"
],
"psr-4" : {
"Twig\\" : "src/"
}
},
"autoload-dev": {
"psr-4" : {
"Twig\\Tests\\" : "tests/"
}
}
}
================================================
FILE: doc/.doctor-rst.yaml
================================================
rules:
american_english: ~
avoid_repetetive_words: ~
blank_line_after_directive: ~
blank_line_before_directive: ~
composer_dev_option_not_at_the_end: ~
correct_code_block_directive_based_on_the_content: ~
deprecated_directive_should_have_version: ~
ensure_order_of_code_blocks_in_configuration_block: ~
extension_xlf_instead_of_xliff: ~
indention: ~
lowercase_as_in_use_statements: ~
max_blank_lines:
max: 2
no_blank_line_after_filepath_in_php_code_block: ~
no_blank_line_after_filepath_in_twig_code_block: ~
no_blank_line_after_filepath_in_xml_code_block: ~
no_blank_line_after_filepath_in_yaml_code_block: ~
no_composer_req: ~
no_explicit_use_of_code_block_php: ~
no_inheritdoc: ~
no_namespace_after_use_statements: ~
no_php_open_tag_in_code_block_php_directive: ~
no_space_before_self_xml_closing_tag: ~
ordered_use_statements: ~
php_prefix_before_bin_console: ~
replace_code_block_types: ~
replacement: ~
short_array_syntax: ~
typo: ~
unused_links: ~
use_deprecated_directive_instead_of_versionadded: ~
use_https_xsd_urls: ~
valid_inline_highlighted_namespaces: ~
valid_use_statements: ~
versionadded_directive_should_have_version: ~
yaml_instead_of_yml_suffix: ~
yarn_dev_option_at_the_end: ~
versionadded_directive_major_version:
major_version: 3
versionadded_directive_min_version:
min_version: '3.0'
deprecated_directive_major_version:
major_version: 3
deprecated_directive_min_version:
min_version: '3.0'
whitelist:
lines:
- 'I like Twig. '
================================================
FILE: doc/_build/build.php
================================================
#!/usr/bin/env php
register('build-docs')
->addOption('disable-cache', null, InputOption::VALUE_NONE, 'Use this option to force a full regeneration of all doc contents')
->setCode(static function (InputInterface $input, OutputInterface $output) {
$io = new SymfonyStyle($input, $output);
$io->text('Building all Twig docs...');
$outputDir = __DIR__.'/output';
$buildConfig = (new BuildConfig())
->setContentDir(__DIR__.'/..')
->setOutputDir($outputDir)
->setImagesDir(__DIR__.'/output/_images')
->setImagesPublicPrefix('_images')
->setTheme('rtd')
;
$buildConfig->setExcludedPaths(['vendor/']);
$buildConfig->disableJsonFileGeneration();
$buildConfig->disableBuildCache();
$result = (new DocBuilder())->build($buildConfig);
if ($result->isSuccessful()) {
// fix assets URLs to make them absolute (otherwise, they don't work in subdirectories)
foreach (glob($outputDir.'/**/*.html') as $htmlFilePath) {
$htmlContents = file_get_contents($htmlFilePath);
file_put_contents($htmlFilePath, str_replace('href="assets/', 'href="/assets/', $htmlContents));
}
$io->success(sprintf('The Twig docs were successfully built at %s', realpath($outputDir)));
} else {
$io->error(sprintf("There were some errors while building the docs:\n\n%s\n", $result->getErrorTrace()));
$io->newLine();
$io->comment('Tip: you can add the -v, -vv or -vvv flags to this command to get debug information.');
return 1;
}
return 0;
})
->getApplication()
->setDefaultCommand('build-docs', true)
->run();
================================================
FILE: doc/_build/composer.json
================================================
{
"minimum-stability": "dev",
"prefer-stable": true,
"config": {
"platform": {
"php": "8.1.0"
},
"preferred-install": {
"*": "dist"
},
"sort-packages": true
},
"require": {
"php": ">=8.1",
"symfony/console": "^5.4",
"symfony/process": "^5.4",
"symfony-tools/docs-builder": "^0.18"
}
}
================================================
FILE: doc/advanced.rst
================================================
Extending Twig
==============
Twig can be extended in many ways; you can add extra tags, filters, tests,
operators, global variables, and functions. You can even extend the parser
itself with node visitors.
.. note::
The first section of this chapter describes how to extend Twig. If you want
to reuse your changes in different projects or if you want to share them
with others, you should then create an extension as described in the
following section.
.. caution::
When extending Twig without creating an extension, Twig won't be able to
recompile your templates when the PHP code is updated. To see your changes
in real-time, either disable template caching or package your code into an
extension (see the next section of this chapter).
Before extending Twig, you must understand the differences between all the
different possible extension points and when to use them.
First, remember that Twig has two main language constructs:
* ``{{ }}``: used to print the result of an expression evaluation;
* ``{% %}``: used to execute statements.
To understand why Twig exposes so many extension points, let's see how to
implement a *Lorem ipsum* generator (it needs to know the number of words to
generate).
You can use a ``lipsum`` *tag*:
.. code-block:: twig
{% lipsum 40 %}
That works, but using a tag for ``lipsum`` is not a good idea for at least
three main reasons:
* ``lipsum`` is not a language construct;
* The tag outputs something;
* The tag is not flexible as you cannot use it in an expression:
.. code-block:: twig
{{ 'some text' ~ {% lipsum 40 %} ~ 'some more text' }}
In fact, you rarely need to create tags; and that's good news because tags are
the most complex extension point.
Now, let's use a ``lipsum`` *filter*:
.. code-block:: twig
{{ 40|lipsum }}
Again, it works. But a filter should transform the passed value to something
else. Here, we use the value to indicate the number of words to generate (so,
``40`` is an argument of the filter, not the value we want to transform).
Next, let's use a ``lipsum`` *function*:
.. code-block:: twig
{{ lipsum(40) }}
Here we go. For this specific example, the creation of a function is the
extension point to use. And you can use it anywhere an expression is accepted:
.. code-block:: twig
{{ 'some text' ~ lipsum(40) ~ 'some more text' }}
{% set lipsum = lipsum(40) %}
Lastly, you can also use a *global* object with a method able to generate lorem
ipsum text:
.. code-block:: twig
{{ text.lipsum(40) }}
As a rule of thumb, use functions for frequently used features and global
objects for everything else.
Keep in mind the following when you want to extend Twig:
========== ========================== ========== =========================
What? Implementation difficulty? How often? When?
========== ========================== ========== =========================
*macro* simple frequent Content generation
*global* simple frequent Helper object
*function* simple frequent Content generation
*filter* simple frequent Value transformation
*tag* complex rare DSL language construct
*test* simple rare Boolean decision
*operator* simple rare Values transformation
========== ========================== ========== =========================
Globals
-------
Global variables are available in all templates and macros. Use ``addGlobal()``
to add a global variable to a Twig environment::
$twig = new \Twig\Environment($loader);
$twig->addGlobal('text', new Text());
You can then use the ``text`` variable anywhere in a template:
.. code-block:: twig
{{ text.lipsum(40) }}
Filters
-------
Creating a filter consists of associating a name with a PHP callable::
// an anonymous function
$filter = new \Twig\TwigFilter('rot13', function ($string) {
return str_rot13($string);
});
// or a simple PHP function
$filter = new \Twig\TwigFilter('rot13', 'str_rot13');
// or a class static method
$filter = new \Twig\TwigFilter('rot13', ['SomeClass', 'rot13Filter']);
$filter = new \Twig\TwigFilter('rot13', 'SomeClass::rot13Filter');
// or a class method
$filter = new \Twig\TwigFilter('rot13', [$this, 'rot13Filter']);
// the one below needs a runtime implementation (see below for more information)
$filter = new \Twig\TwigFilter('rot13', ['SomeClass', 'rot13Filter']);
The first argument passed to the ``\Twig\TwigFilter`` constructor is the name of the
filter you will use in templates and the second one is the PHP callable to
associate with it.
Then, add the filter to the Twig environment::
$twig = new \Twig\Environment($loader);
$twig->addFilter($filter);
And here is how to use it in a template:
.. code-block:: twig
{{ 'Twig'|rot13 }}
{# will output Gjvt #}
When called by Twig, the PHP callable receives the left side of the filter
(before the pipe ``|``) as the first argument and the extra arguments passed
to the filter (within parentheses ``()``) as extra arguments.
For instance, the following code:
.. code-block:: twig
{{ 'TWIG'|lower }}
{{ now|date('d/m/Y') }}
is compiled to something like the following::
The ``\Twig\TwigFilter`` class takes an array of options as its last argument::
$filter = new \Twig\TwigFilter('rot13', 'str_rot13', $options);
Charset-aware Filters
~~~~~~~~~~~~~~~~~~~~~
If you want to access the default charset in your filter, set the
``needs_charset`` option to ``true``; Twig will pass the default charset as the
first argument to the filter call::
$filter = new \Twig\TwigFilter('rot13', function (string $charset, $string) {
return str_rot13($string);
}, ['needs_charset' => true]);
Environment-aware Filters
~~~~~~~~~~~~~~~~~~~~~~~~~
If you want to access the current environment instance in your filter, set the
``needs_environment`` option to ``true``; Twig will pass the current
environment as the first argument to the filter call::
$filter = new \Twig\TwigFilter('rot13', function (\Twig\Environment $env, $string) {
// get the current charset for instance
$charset = $env->getCharset();
return str_rot13($string);
}, ['needs_environment' => true]);
Context-aware Filters
~~~~~~~~~~~~~~~~~~~~~
If you want to access the current context in your filter, set the
``needs_context`` option to ``true``; Twig will pass the current context as
the first argument to the filter call (or the second one if
``needs_environment`` is also set to ``true``)::
$filter = new \Twig\TwigFilter('rot13', function ($context, $string) {
// ...
}, ['needs_context' => true]);
$filter = new \Twig\TwigFilter('rot13', function (\Twig\Environment $env, $context, $string) {
// ...
}, ['needs_context' => true, 'needs_environment' => true]);
Automatic Escaping
~~~~~~~~~~~~~~~~~~
If automatic escaping is enabled, the output of the filter may be escaped
before printing. If your filter acts as an escaper (or explicitly outputs HTML
or JavaScript code), you will want the raw output to be printed. In such a
case, set the ``is_safe`` option::
$filter = new \Twig\TwigFilter('nl2br', 'nl2br', ['is_safe' => ['html']]);
Some filters may need to work on input that is already escaped or safe, for
example when adding (safe) HTML tags to originally unsafe output. In such a
case, set the ``pre_escape`` option to escape the input data before it is run
through your filter::
$filter = new \Twig\TwigFilter('somefilter', 'somefilter', ['pre_escape' => 'html', 'is_safe' => ['html']]);
Variadic Filters
~~~~~~~~~~~~~~~~
When a filter should accept an arbitrary number of arguments, set the
``is_variadic`` option to ``true``; Twig will pass the extra arguments as the
last argument to the filter call as an array::
$filter = new \Twig\TwigFilter('thumbnail', function ($file, array $options = []) {
// ...
}, ['is_variadic' => true]);
Be warned that :ref:`named arguments ` passed to a variadic
filter cannot be checked for validity as they will automatically end up in the
option array.
Dynamic Filters
~~~~~~~~~~~~~~~
A filter name containing the special ``*`` character is a dynamic filter and
the ``*`` part will match any string::
$filter = new \Twig\TwigFilter('*_path', function ($name, $arguments) {
// ...
});
The following filters are matched by the above defined dynamic filter:
* ``product_path``
* ``category_path``
A dynamic filter can define more than one dynamic parts::
$filter = new \Twig\TwigFilter('*_path_*', function ($name, $suffix, $arguments) {
// ...
});
The filter receives all dynamic part values before the normal filter arguments,
but after the environment and the context. For instance, a call to
``'Paris'|a_path_b()`` will result in the following arguments to be passed to the
filter: ``('a', 'b', 'Paris')``.
Deprecated Filters
~~~~~~~~~~~~~~~~~~
.. versionadded:: 3.15
The ``deprecation_info`` option was added in Twig 3.15.
You can mark a filter as being deprecated by setting the ``deprecation_info``
option::
$filter = new \Twig\TwigFilter('obsolete', function () {
// ...
}, ['deprecation_info' => new DeprecatedCallableInfo('twig/twig', '3.11', 'new_one')]);
The ``DeprecatedCallableInfo`` constructor takes the following parameters:
* The Composer package name that defines the filter;
* The version when the filter was deprecated.
Optionally, you can also provide the following parameters about an alternative:
* The package name that contains the alternative filter;
* The alternative filter name that replaces the deprecated one;
* The package version that added the alternative filter.
When a filter is deprecated, Twig emits a deprecation notice when compiling a
template using it. See :ref:`deprecation-notices` for more information.
.. note::
Before Twig 3.15, you can mark a filter as being deprecated by setting the
``deprecated`` option to ``true``. You can also give an alternative filter
that replaces the deprecated one when that makes sense::
$filter = new \Twig\TwigFilter('obsolete', function () {
// ...
}, ['deprecated' => true, 'alternative' => 'new_one']);
.. versionadded:: 3.11
The ``deprecating_package`` option was added in Twig 3.11.
You can also set the ``deprecating_package`` option to specify the package
that is deprecating the filter, and ``deprecated`` can be set to the
package version when the filter was deprecated::
$filter = new \Twig\TwigFilter('obsolete', function () {
// ...
}, ['deprecated' => '1.1', 'deprecating_package' => 'twig/some-package']);
Functions
---------
Functions are defined in the exact same way as filters, but you need to create
an instance of ``\Twig\TwigFunction``::
$twig = new \Twig\Environment($loader);
$function = new \Twig\TwigFunction('function_name', function () {
// ...
});
$twig->addFunction($function);
Functions support the same features as filters, except for the ``pre_escape``
and ``preserves_safety`` options.
Tests
-----
Tests are defined in the exact same way as filters and functions, but you need
to create an instance of ``\Twig\TwigTest``::
$twig = new \Twig\Environment($loader);
$test = new \Twig\TwigTest('test_name', function () {
// ...
});
$twig->addTest($test);
Tests allow you to create custom application specific logic for evaluating
boolean conditions. As a simple example, let's create a Twig test that checks if
objects are 'red'::
$twig = new \Twig\Environment($loader);
$test = new \Twig\TwigTest('red', function ($value) {
if (isset($value->color) && $value->color == 'red') {
return true;
}
if (isset($value->paint) && $value->paint == 'red') {
return true;
}
return false;
});
$twig->addTest($test);
Test functions must always return ``true``/``false``.
When creating tests you can use the ``node_class`` option to provide custom test
compilation. This is useful if your test can be compiled into PHP primitives.
This is used by many of the tests built into Twig::
namespace App;
use Twig\Environment;
use Twig\Node\Expression\TestExpression;
use Twig\TwigTest;
$twig = new Environment($loader);
$test = new TwigTest(
'odd',
null,
['node_class' => OddTestExpression::class]);
$twig->addTest($test);
class OddTestExpression extends TestExpression
{
public function compile(\Twig\Compiler $compiler)
{
$compiler
->raw('(')
->subcompile($this->getNode('node'))
->raw(' % 2 != 0')
->raw(')')
;
}
}
The above example shows how you can create tests that use a node class. The node
class has access to one sub-node called ``node``. This sub-node contains the
value that is being tested. When the ``odd`` filter is used in code such as:
.. code-block:: twig
{% if my_value is odd %}
The ``node`` sub-node will contain an expression of ``my_value``. Node-based
tests also have access to the ``arguments`` node. This node will contain the
various other arguments that have been provided to your test.
If you want to pass a variable number of positional or named arguments to the
test, set the ``is_variadic`` option to ``true``. Tests support dynamic
names (see dynamic filters for the syntax).
Tags
----
One of the most exciting features of a template engine like Twig is the
possibility to define new **language constructs**. This is also the most complex
feature as you need to understand how Twig's internals work.
Most of the time though, a tag is not needed:
* If your tag generates some output, use a **function** instead.
* If your tag modifies some content and returns it, use a **filter** instead.
For instance, if you want to create a tag that converts a Markdown formatted
text to HTML, create a ``markdown`` filter instead:
.. code-block:: twig
{{ '**markdown** text'|markdown }}
If you want use this filter on large amounts of text, wrap it with the
:doc:`apply ` tag:
.. code-block:: twig
{% apply markdown %}
Title
=====
Much better than creating a tag as you can **compose** filters.
{% endapply %}
* If your tag does not output anything, but only exists because of a side
effect, create a **function** that returns nothing and call it via the
:doc:`do ` tag.
For instance, if you want to create a tag that logs text, create a ``log``
function instead and call it via the :doc:`do ` tag:
.. code-block:: twig
{% do log('Log some things') %}
If you still want to create a tag for a new language construct, great!
Let's create a ``set`` tag that allows the definition of simple variables from
within a template. The tag can be used like follows:
.. code-block:: twig
{% set name = "value" %}
{{ name }}
{# should output value #}
.. note::
The ``set`` tag is part of the Core extension and as such is always
available. The built-in version is slightly more powerful and supports
multiple assignments by default.
Three steps are needed to define a new tag:
* Defining a Token Parser class (responsible for parsing the template code);
* Defining a Node class (responsible for converting the parsed code to PHP);
* Registering the tag.
Registering a new tag
~~~~~~~~~~~~~~~~~~~~~
Add a tag by calling the ``addTokenParser`` method on the ``\Twig\Environment``
instance::
$twig = new \Twig\Environment($loader);
$twig->addTokenParser(new CustomSetTokenParser());
Defining a Token Parser
~~~~~~~~~~~~~~~~~~~~~~~
Now, let's see the actual code of this class::
class CustomSetTokenParser extends \Twig\TokenParser\AbstractTokenParser
{
public function parse(\Twig\Token $token)
{
$parser = $this->parser;
$lineno = $token->getLine();
$stream = $parser->getStream();
$name = $stream->expect(\Twig\Token::NAME_TYPE)->getValue();
$stream->expect(\Twig\Token::OPERATOR_TYPE, '=');
$value = $parser->getExpressionParser()->parseExpression();
$stream->expect(\Twig\Token::BLOCK_END_TYPE);
return new CustomSetNode($name, $value, $lineno);
}
public function getTag()
{
return 'set';
}
}
The ``getTag()`` method must return the tag we want to parse, here ``set``.
The ``parse()`` method is invoked whenever the parser encounters a ``set``
tag. It should return a ``\Twig\Node\Node`` instance that represents the node (the
``CustomSetNode`` calls creating is explained in the next section).
The parsing process is simplified thanks to a bunch of methods you can call
from the token stream (``$this->parser->getStream()``):
* ``getCurrent()``: Gets the current token in the stream.
* ``next()``: Moves to the next token in the stream, *but returns the old one*.
* ``test($type)``, ``test($value)`` or ``test($type, $value)``: Determines whether
the current token is of a particular type or value (or both). The value may be an
array of several possible values.
* ``expect($type[, $value[, $message]])``: If the current token isn't of the given
type/value a syntax error is thrown. Otherwise, if the type and value are correct,
the token is returned and the stream moves to the next token.
* ``look()``: Looks at the next token without consuming it.
Parsing expressions is done by calling the ``parseExpression()`` like we did for
the ``set`` tag.
When encountering a syntax error during parsing, throw an exception::
throw new SyntaxError('Some error message.', $stream->getCurrent()->getLine(), $stream->getSourceContext());
For better error reporting to the user, follow these recommendations:
* Use ``\Twig\Error\SyntaxError``;
* **Always** pass the line number of the node and the source context;
* End the exception message with a dot.
.. tip::
Reading the existing ``TokenParser`` classes is the best way to learn all
the nitty-gritty details of the parsing process.
Defining a Node
~~~~~~~~~~~~~~~
The ``CustomSetNode`` class itself is quite short::
class CustomSetNode extends \Twig\Node\Node
{
public function __construct($name, \Twig\Node\Expression\AbstractExpression $value, $line)
{
parent::__construct(['value' => $value], ['name' => $name], $line);
}
public function compile(\Twig\Compiler $compiler)
{
$compiler
->addDebugInfo($this)
->write('$context[\''.$this->getAttribute('name').'\'] = ')
->subcompile($this->getNode('value'))
->raw(";\n")
;
}
}
The compiler implements a fluid interface and provides methods that help the
developer generate beautiful and readable PHP code:
* ``subcompile()``: Compiles a node.
* ``raw()``: Writes the given string as is.
* ``write()``: Writes the given string by adding indentation at the beginning
of each line.
* ``string()``: Writes a quoted string.
* ``repr()``: Writes a PHP representation of a given value (see
``\Twig\Node\ForNode`` for a usage example).
* ``addDebugInfo()``: Adds the line of the original template file related to
the current node as a comment. It's highly recommended to call this method
when implementing custom nodes.
* ``indent()``: Indents the generated code (see ``\Twig\Node\BlockNode`` for a
usage example).
* ``outdent()``: Outdents the generated code (see ``\Twig\Node\BlockNode`` for a
usage example).
For structural nodes, always call ``addDebugInfo()`` early on in the
compilation process to improve error reporting to the user in case the code
would throw an exception.
.. _creating_extensions:
Creating an Extension
---------------------
The main motivation for writing an extension is to move often used code into a
reusable class like adding support for internationalization. An extension can
define tags, filters, tests, operators, functions, and node visitors.
Most of the time, it is useful to create a single extension for your project,
to host all the specific tags and filters you want to add to Twig.
.. tip::
When packaging your code into an extension, Twig is smart enough to
recompile your templates whenever you make a change to it (when
``auto_reload`` is enabled).
An extension is a class that implements the following interface::
interface \Twig\Extension\ExtensionInterface
{
/**
* Returns the token parser instances to add to the existing list.
*
* @return \Twig\TokenParser\TokenParserInterface[]
*/
public function getTokenParsers();
/**
* Returns the node visitor instances to add to the existing list.
*
* @return \Twig\NodeVisitor\NodeVisitorInterface[]
*/
public function getNodeVisitors();
/**
* Returns a list of filters to add to the existing list.
*
* @return \Twig\TwigFilter[]
*/
public function getFilters();
/**
* Returns a list of tests to add to the existing list.
*
* @return \Twig\TwigTest[]
*/
public function getTests();
/**
* Returns a list of functions to add to the existing list.
*
* @return \Twig\TwigFunction[]
*/
public function getFunctions();
/**
* Returns a list of expression parsers to add to the existing list.
*
* @return \Twig\ExpressionParser\ExpressionParserInterface[]
*/
public function getExpressionParsers();
}
To keep your extension class clean and lean, inherit from the built-in
``\Twig\Extension\AbstractExtension`` class instead of implementing the interface as it provides
empty implementations for all methods::
class CustomTwigExtension extends \Twig\Extension\AbstractExtension
{
}
This extension does nothing for now. We will customize it in the next sections.
You can save your extension anywhere on the filesystem, as all extensions must
be registered explicitly to be available in your templates.
You can register an extension by using the ``addExtension()`` method on your
main ``Environment`` object::
$twig = new \Twig\Environment($loader);
$twig->addExtension(new CustomTwigExtension());
.. tip::
The Twig core extensions are great examples of how extensions work.
Globals
~~~~~~~
Global variables can be registered in an extension via the ``getGlobals()``
method::
class CustomTwigExtension extends \Twig\Extension\AbstractExtension implements \Twig\Extension\GlobalsInterface
{
public function getGlobals(): array
{
return [
'text' => new Text(),
];
}
// ...
}
.. caution::
Globals are fetched once from extensions and then cached for the lifetime
of the Twig environment. It means that globals should not be used to store
values that can change during the lifetime of the Twig environment. For
instance, if you're using an application server like RoadRunner or
FrankenPHP, you should not store values related to the current context (like
the HTTP request). If you do so, don't forget to reset the cache between
requests by calling ``Environment::resetGlobals()``.
Functions
~~~~~~~~~
Functions can be registered in an extension via the ``getFunctions()``
method::
class CustomTwigExtension extends \Twig\Extension\AbstractExtension
{
public function getFunctions()
{
return [
new \Twig\TwigFunction('lipsum', 'generate_lipsum'),
];
}
// ...
}
Filters
~~~~~~~
To add a filter to an extension, you need to override the ``getFilters()``
method. This method must return an array of filters to add to the Twig
environment::
class CustomTwigExtension extends \Twig\Extension\AbstractExtension
{
public function getFilters()
{
return [
new \Twig\TwigFilter('rot13', 'str_rot13'),
];
}
// ...
}
Tags
~~~~
Adding a tag in an extension can be done by overriding the
``getTokenParsers()`` method. This method must return an array of tags to add
to the Twig environment::
class CustomTwigExtension extends \Twig\Extension\AbstractExtension
{
public function getTokenParsers()
{
return [new CustomSetTokenParser()];
}
// ...
}
In the above code, we have added a single new tag, defined by the
``CustomSetTokenParser`` class. The ``CustomSetTokenParser`` class is
responsible for parsing the tag and compiling it to PHP.
Operators
~~~~~~~~~
.. versionadded:: 3.21
The ``getExpressionParsers()`` method was added in Twig 3.21.
.. deprecated:: 3.21
The ``getExpressionParsers()`` method replaces the now deprecated
``getOperators()`` method. See the :doc:`deprecated ` page for
details on how to upgrade from ``getOperators()`` to
``getExpressionParsers()``.
The ``getExpressionParsers()`` method lets you add new operators. To implement
a new one, have a look at the default operators provided by
``Twig\Extension\CoreExtension``.
Tests
~~~~~
The ``getTests()`` method lets you add new test functions::
class CustomTwigExtension extends \Twig\Extension\AbstractExtension
{
public function getTests()
{
return [
new \Twig\TwigTest('even', 'twig_test_even'),
];
}
// ...
}
Using PHP Attributes to define Extensions
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. versionadded:: 3.21
The attribute classes were added in Twig 3.21.
You can add the ``#[AsTwigFilter]``, ``#[AsTwigFunction]``, and ``#[AsTwigTest]``
attributes to public methods of any class to define filters, functions, and tests.
Create a class using these attributes::
use Twig\Attribute\AsTwigFilter;
use Twig\Attribute\AsTwigFunction;
use Twig\Attribute\AsTwigTest;
class ProjectExtension
{
#[AsTwigFilter('rot13')]
public static function rot13(string $string): string
{
// ...
}
#[AsTwigFunction('lipsum')]
public static function lipsum(int $count): string
{
// ...
}
#[AsTwigTest('even')]
public static function isEven(int $number): bool
{
// ...
}
}
Then register the ``Twig\Extension\AttributeExtension`` with the class name::
$twig = new \Twig\Environment($loader);
$twig->addExtension(new \Twig\Extension\AttributeExtension(ProjectExtension::class));
If all the methods are static, you are done. The ``ProjectExtension`` class will
never be instantiated and the class attributes will be scanned only when a template
is compiled.
Otherwise, if some methods are not static, you need to register the class as
a runtime extension using one of the runtime loaders::
use Twig\Attribute\AsTwigFunction;
class ProjectExtension
{
// Inject hypothetical dependencies
public function __construct(private LipsumProvider $lipsumProvider) {}
#[AsTwigFunction('lipsum')]
public function lipsum(int $count): string
{
return $this->lipsumProvider->lipsum($count);
}
}
$twig = new \Twig\Environment($loader);
$twig->addExtension(new \Twig\Extension\AttributeExtension(ProjectExtension::class);
$twig->addRuntimeLoader(new \Twig\RuntimeLoader\FactoryLoader([
ProjectExtension::class => function () use ($lipsumProvider) {
return new ProjectExtension($lipsumProvider);
},
]));
If you want to access the current environment instance in your filter or function,
add the ``Twig\Environment`` type to the first argument of the method::
class ProjectExtension
{
#[AsTwigFunction('lipsum')]
public function lipsum(\Twig\Environment $env, int $count): string
{
// ...
}
}
``#[AsTwigFilter]`` and ``#[AsTwigFunction]`` support variadic arguments
automatically when applied to variadic methods::
class ProjectExtension
{
#[AsTwigFilter('thumbnail')]
public function thumbnail(string $file, mixed ...$options): string
{
// ...
}
}
The attributes support other options used to configure the Twig Callables:
* ``AsTwigFilter``: ``needsCharset``, ``needsEnvironment``, ``needsContext``, ``isSafe``, ``isSafeCallback``, ``preEscape``, ``preservesSafety``, ``deprecationInfo``
* ``AsTwigFunction``: ``needsCharset``, ``needsEnvironment``, ``needsContext``, ``isSafe``, ``isSafeCallback``, ``deprecationInfo``
* ``AsTwigTest``: ``needsCharset``, ``needsEnvironment``, ``needsContext``, ``deprecationInfo``
Definition vs Runtime
~~~~~~~~~~~~~~~~~~~~~
Twig filters, functions, and tests runtime implementations can be defined as
any valid PHP callable:
* **functions/static methods**: Simple to implement and fast (used by all Twig
core extensions); but it is hard for the runtime to depend on external
objects;
* **closures**: Simple to implement;
* **object methods**: More flexible and required if your runtime code depends
on external objects.
The simplest way to use methods is to define them on the extension itself::
class CustomTwigExtension extends \Twig\Extension\AbstractExtension
{
private $rot13Provider;
public function __construct($rot13Provider)
{
$this->rot13Provider = $rot13Provider;
}
public function getFunctions()
{
return [
new \Twig\TwigFunction('rot13', [$this, 'rot13']),
];
}
public function rot13($value)
{
return $this->rot13Provider->rot13($value);
}
}
This is very convenient but not recommended as it makes template compilation
depend on runtime dependencies even if they are not needed (think for instance
as a dependency that connects to a database engine).
You can decouple the extension definitions from their runtime implementations by
registering a ``\Twig\RuntimeLoader\RuntimeLoaderInterface`` instance on the
environment that knows how to instantiate such runtime classes (runtime classes
must be autoload-able)::
class RuntimeLoader implements \Twig\RuntimeLoader\RuntimeLoaderInterface
{
public function load($class)
{
// implement the logic to create an instance of $class
// and inject its dependencies
// most of the time, it means using your dependency injection container
if ('CustomTwigRuntime' === $class) {
return new $class(new Rot13Provider());
} else {
// ...
}
}
}
$twig->addRuntimeLoader(new RuntimeLoader());
.. note::
Twig comes with a PSR-11 compatible runtime loader
(``\Twig\RuntimeLoader\ContainerRuntimeLoader``).
It is now possible to move the runtime logic to a new
``CustomTwigRuntime`` class and use it directly in the extension::
class CustomTwigRuntime
{
private $rot13Provider;
public function __construct($rot13Provider)
{
$this->rot13Provider = $rot13Provider;
}
public function rot13($value)
{
return $this->rot13Provider->rot13($value);
}
}
class CustomTwigExtension extends \Twig\Extension\AbstractExtension
{
public function getFunctions()
{
return [
new \Twig\TwigFunction('rot13', ['CustomTwigRuntime', 'rot13']),
// or
new \Twig\TwigFunction('rot13', 'CustomTwigRuntime::rot13'),
];
}
}
.. note::
The extension class should implement the ``Twig\Extension\LastModifiedExtensionInterface``
interface to invalidate the template cache when the runtime class is modified.
The ``AbstractExtension`` class implements this interface and tracks the
runtime class if its name is the same as the extension class but ends with
``Runtime`` instead of ``Extension``.
Testing an Extension
--------------------
Functional Tests
~~~~~~~~~~~~~~~~
You can create functional tests for extensions by creating the following file
structure in your test directory::
Fixtures/
filters/
lower.test
upper.test
functions/
date.test
format.test
tags/
for.test
if.test
IntegrationTest.php
The ``IntegrationTest.php`` file should look like this::
namespace Project\Tests;
use Twig\Test\IntegrationTestCase;
class IntegrationTest extends IntegrationTestCase
{
public function getExtensions()
{
return [
new CustomTwigExtension1(),
new CustomTwigExtension2(),
];
}
public function getFixturesDir()
{
return __DIR__.'/Fixtures/';
}
}
Fixtures examples can be found within the Twig repository
`tests/Twig/Fixtures`_ directory.
Node Tests
~~~~~~~~~~
Testing the node visitors can be complex, so extend your test cases from
``\Twig\Test\NodeTestCase``. Examples can be found in the Twig repository
`tests/Twig/Node`_ directory.
.. _`tests/Twig/Fixtures`: https://github.com/twigphp/Twig/tree/3.x/tests/Fixtures
.. _`tests/Twig/Node`: https://github.com/twigphp/Twig/tree/3.x/tests/Node
================================================
FILE: doc/api.rst
================================================
Twig for Developers
===================
This chapter describes the API to Twig and not the template language. It will
be most useful as reference to those implementing the template interface to
the application and not those who are creating Twig templates.
Basics
------
Twig uses a central object called the **environment** (of class
``\Twig\Environment``). Instances of this class are used to store the
configuration and extensions, and are used to load templates.
Most applications create one ``\Twig\Environment`` object on application
initialization and use that to load templates. In some cases, it might be useful
to have multiple environments side by side, with different configurations.
The typical way to configure Twig to load templates for an application looks
roughly like this::
require_once '/path/to/vendor/autoload.php';
$loader = new \Twig\Loader\FilesystemLoader('/path/to/templates');
$twig = new \Twig\Environment($loader, [
'cache' => '/path/to/compilation_cache',
]);
This creates a template environment with a default configuration and a loader
that looks up templates in the ``/path/to/templates/`` directory. Different
loaders are available and you can also write your own if you want to load
templates from a database or other resources.
.. note::
Notice that the second argument of the environment is an array of options.
The ``cache`` option is a compilation cache directory, where Twig caches
the compiled templates to avoid the parsing phase for subsequent
requests. It is very different from the cache you might want to add for
the evaluated templates. For such a need, you can use any available PHP
cache library.
Loading Templates
-----------------
To load a template, call the ``load()`` method on a Twig environment which
returns a ``\Twig\TemplateWrapper`` instance::
$template = $twig->load('index.html.twig');
Rendering Templates
-------------------
To render a template with some variables, call the ``render()`` method::
echo $template->render(['the' => 'variables', 'go' => 'here']);
.. note::
The ``display()`` method is a shortcut to output the rendered template.
You can also load and render the template directly via the Environment::
echo $twig->render('index.html.twig', ['the' => 'variables', 'go' => 'here']);
If a template defines blocks, they can be rendered individually via the
``renderBlock()`` call::
echo $template->renderBlock('block_name', ['the' => 'variables', 'go' => 'here']);
Streaming Templates
-------------------
.. versionadded:: 3.18
To stream a template, call the ``stream()`` method::
$template->stream(['the' => 'variables', 'go' => 'here']);
To stream a specific template block, call the ``streamBlock()`` method::
$template->streamBlock('block_name', ['the' => 'variables', 'go' => 'here']);
.. note::
The ``stream()`` and ``streamBlock()`` methods return an iterable.
.. _environment_options:
Environment Options
-------------------
When creating a new ``\Twig\Environment`` instance, you can pass an array of
options as the constructor second argument::
$twig = new \Twig\Environment($loader, ['debug' => true]);
The following options are available:
* ``debug`` *boolean*
When set to ``true``, the generated templates have a
``__toString()`` method that you can use to display the generated nodes
(default to ``false``).
* ``charset`` *string* (defaults to ``utf-8``)
The charset used by the templates.
* ``cache`` *string* or ``false``
An absolute path where to store the compiled templates, or
``false`` to disable caching (which is the default).
* ``auto_reload`` *boolean*
When developing with Twig, it's useful to recompile the
template whenever the source code changes. If you don't provide a value for
the ``auto_reload`` option, it will be determined automatically based on the
``debug`` value.
.. _environment_options_strict_variables:
* ``strict_variables`` *boolean*
If set to ``false``, Twig will silently ignore invalid
variables (variables and or attributes/methods that do not exist) and
replace them with a ``null`` value. When set to ``true``, Twig throws an
exception instead (default to ``false``).
* ``autoescape`` *string*
Sets the default auto-escaping strategy (``name``, ``html``, ``js``, ``css``, ``url``,
``html_attr``, ``html_attr_relaxed``, or a PHP callback that takes the template "filename"
and returns the escaping strategy to use -- the callback cannot be a function
name to avoid collision with built-in escaping strategies); set it to
``false`` to disable auto-escaping. The ``name`` escaping strategy determines
the escaping strategy to use for a template based on the template filename
extension (this strategy does not incur any overhead at runtime as
auto-escaping is done at compilation time.)
* ``optimizations`` *integer*
A flag that indicates which optimizations to apply
(default to ``-1`` -- all optimizations are enabled; set it to ``0`` to
disable).
* ``use_yield`` *boolean*
``true``: forces templates to exclusively use ``yield`` instead of ``echo``
(all extensions must be yield ready)
``false`` (default): allows templates to use a mix of ``yield`` and ``echo``
calls to allow for a progressive migration.
Switch to ``true`` when possible as this will be the only supported mode in
Twig 4.0.
Loaders
-------
Loaders are responsible for loading templates from a resource such as the file
system.
Compilation Cache
~~~~~~~~~~~~~~~~~
All template loaders can cache the compiled templates on the filesystem for
future reuse. It speeds up Twig a lot as templates are only compiled once.
Built-in Loaders
~~~~~~~~~~~~~~~~
Here is a list of the built-in loaders:
``\Twig\Loader\FilesystemLoader``
.................................
``\Twig\Loader\FilesystemLoader`` loads templates from the file system. This loader
can find templates in folders on the file system and is the preferred way to
load them::
$loader = new \Twig\Loader\FilesystemLoader($templateDir);
It can also look for templates in an array of directories::
$loader = new \Twig\Loader\FilesystemLoader([$templateDir1, $templateDir2]);
With such a configuration, Twig will first look for templates in
``$templateDir1`` and if they do not exist, it will fallback to look for them
in the ``$templateDir2``.
You can add or prepend paths via the ``addPath()`` and ``prependPath()``
methods::
$loader->addPath($templateDir3);
$loader->prependPath($templateDir4);
The filesystem loader also supports namespaced templates. This allows you to group
your templates under different namespaces which have their own template paths.
When using the ``setPaths()``, ``addPath()``, and ``prependPath()`` methods,
specify the namespace as the second argument (when not specified, these
methods act on the "main" namespace)::
$loader->addPath($templateDir, 'admin');
Namespaced templates can be accessed via the special
``@namespace_name/template_path`` notation::
$twig->render('@admin/index.html.twig', []);
``\Twig\Loader\FilesystemLoader`` supports absolute and relative paths. Using relative
paths is preferred as it makes the cache keys independent of the project root
directory (for instance, it allows warming the cache from a build server where
the directory might be different from the one used on production servers)::
$loader = new \Twig\Loader\FilesystemLoader('templates', getcwd().'/..');
.. note::
When not passing the root path as a second argument, Twig uses ``getcwd()``
for relative paths.
``\Twig\Loader\ArrayLoader``
............................
``\Twig\Loader\ArrayLoader`` loads a template from a PHP array. It is passed an
array of strings bound to template names::
$loader = new \Twig\Loader\ArrayLoader([
'index.html.twig' => 'Hello {{ name }}!',
]);
$twig = new \Twig\Environment($loader);
echo $twig->render('index.html.twig', ['name' => 'Fabien']);
This loader is very useful for unit testing. It can also be used for small
projects where storing all templates in a single PHP file might make sense.
.. tip::
When using the ``Array`` loader with a cache mechanism, you should know that
a new cache key is generated each time a template content "changes" (the
cache key being the source code of the template). If you don't want to see
your cache grows out of control, you need to take care of clearing the old
cache file by yourself.
``\Twig\Loader\ChainLoader``
............................
``\Twig\Loader\ChainLoader`` delegates the loading of templates to other loaders::
$loader1 = new \Twig\Loader\ArrayLoader([
'base.html.twig' => '{% block content %}{% endblock %}',
]);
$loader2 = new \Twig\Loader\ArrayLoader([
'index.html.twig' => '{% extends "base.html.twig" %}{% block content %}Hello {{ name }}{% endblock %}',
'base.html.twig' => 'Will never be loaded',
]);
$loader = new \Twig\Loader\ChainLoader([$loader1, $loader2]);
$twig = new \Twig\Environment($loader);
When looking for a template, Twig tries each loader in turn and returns as soon
as the template is found. When rendering the ``index.html.twig`` template from the
above example, Twig will load it with ``$loader2`` but the ``base.html.twig``
template will be loaded from ``$loader1``.
.. note::
You can also add loaders via the ``addLoader()`` method.
Create your own Loader
~~~~~~~~~~~~~~~~~~~~~~
All loaders implement the ``\Twig\Loader\LoaderInterface``::
interface \Twig\Loader\LoaderInterface
{
/**
* Returns the source context for a given template logical name.
*
* @param string $name The template logical name
*
* @return \Twig\Source
*
* @throws \Twig\Error\LoaderError When $name is not found
*/
public function getSourceContext($name);
/**
* Gets the cache key to use for the cache for a given template name.
*
* @param string $name The name of the template to load
*
* @return string The cache key
*
* @throws \Twig\Error\LoaderError When $name is not found
*/
public function getCacheKey($name);
/**
* Returns true if the template is still fresh.
*
* @param string $name The template name
* @param timestamp $time The last modification time of the cached template
*
* @return bool true if the template is fresh, false otherwise
*
* @throws \Twig\Error\LoaderError When $name is not found
*/
public function isFresh($name, $time);
/**
* Check if we have the source code of a template, given its name.
*
* @param string $name The name of the template to check if we can load
*
* @return bool If the template source code is handled by this loader or not
*/
public function exists($name);
}
The ``isFresh()`` method must return ``true`` if the current cached template
is still fresh, given the last modification time, or ``false`` otherwise.
The ``getSourceContext()`` method must return an instance of ``\Twig\Source``.
Using Extensions
----------------
Twig extensions are packages that add new features to Twig. Register an
extension via the ``addExtension()`` method::
$twig->addExtension(new \Twig\Extension\SandboxExtension());
Twig comes bundled with the following extensions:
* ``\Twig\Extension\CoreExtension``: Defines all the core features of Twig.
* ``\Twig\Extension\DebugExtension``: Defines the ``dump`` function to help debug
template variables.
* ``\Twig\Extension\EscaperExtension``: Adds automatic output-escaping and the
possibility to escape/unescape blocks of code.
* ``\Twig\Extension\SandboxExtension``: Adds a sandbox mode to the default Twig
environment, making it safe to evaluate untrusted code.
* ``\Twig\Extension\ProfilerExtension``: Enables the built-in Twig profiler.
* ``\Twig\Extension\OptimizerExtension``: Optimizes the node tree before
compilation.
* ``\Twig\Extension\StringLoaderExtension``: Defines the ``template_from_string``
function to allow loading templates from string in a template.
The Core, Escaper, and Optimizer extensions are registered by default.
Built-in Extensions
-------------------
This section describes the features added by the built-in extensions.
.. tip::
Read the chapter about :doc:`extending Twig ` to learn how to
create your own extensions.
Core Extension
~~~~~~~~~~~~~~
The ``core`` extension defines all the core features of Twig:
* :doc:`Tags `;
* :doc:`Filters `;
* :doc:`Functions `;
* :doc:`Tests `.
Escaper Extension
~~~~~~~~~~~~~~~~~
The ``escaper`` extension adds automatic output escaping to Twig. It defines a
tag, ``autoescape``, and a filter, ``raw``.
When creating the escaper extension, you can switch on or off the global
output escaping strategy::
$escaper = new \Twig\Extension\EscaperExtension('html');
$twig->addExtension($escaper);
If set to ``html``, all variables in templates are escaped (using the ``html``
escaping strategy), except those using the ``raw`` filter:
.. code-block:: twig
{{ article.to_html|raw }}
You can also change the escaping mode locally by using the ``autoescape`` tag:
.. code-block:: twig
{% autoescape 'html' %}
{{ var }}
{{ var|raw }} {# var won't be escaped #}
{{ var|escape }} {# var won't be double-escaped #}
{% endautoescape %}
.. warning::
The ``autoescape`` tag has no effect on included files.
The escaping rules are implemented as follows:
* Literals (integers, booleans, arrays, ...) used in the template directly as
variables or filter arguments are never automatically escaped:
.. code-block:: html+twig
{{ "Twig " }} {# won't be escaped #}
{% set text = "Twig " %}
{{ text }} {# will be escaped #}
* Expressions which the result is a literal or a variable marked safe
are never automatically escaped:
.. code-block:: html+twig
{{ any_value ? "Twig " : " Twig" }} {# won't be escaped #}
{% set text = "Twig " %}
{{ true ? text : " Twig" }} {# will be escaped #}
{{ false ? text : " Twig" }} {# won't be escaped #}
{% set text = "Twig " %}
{{ any_value ? text|raw : " Twig" }} {# won't be escaped #}
* Objects with a ``__toString`` method are converted to strings and
escaped. You can mark some classes and/or interfaces as being safe for some
strategies via ``EscaperExtension::addSafeClass()``:
.. code-block:: twig
// mark objects of class "HtmlGenerator" as safe for the HTML strategy
$escaper->addSafeClass('HtmlGenerator', ['html']);
// mark objects of interface "HtmlGeneratorInterface" as safe for the HTML strategy
$escaper->addSafeClass('HtmlGeneratorInterface', ['html']);
// mark objects of class "HtmlGenerator" as safe for the HTML and JS strategies
$escaper->addSafeClass('HtmlGenerator', ['html', 'js']);
// mark objects of class "HtmlGenerator" as safe for all strategies
$escaper->addSafeClass('HtmlGenerator', ['all']);
* Escaping is applied before printing, after any other filter is applied:
.. code-block:: twig
{{ var|upper }} {# is equivalent to {{ var|upper|escape }} #}
* The ``raw`` filter should only be used at the end of the filter chain:
.. code-block:: twig
{{ var|raw|upper }} {# will be escaped #}
{{ var|upper|raw }} {# won't be escaped #}
* Automatic escaping is not applied if the last filter in the chain is marked
safe for the current context (e.g. ``html`` or ``js``). ``escape`` and
``escape('html')`` are marked safe for HTML, ``escape('js')`` is marked
safe for JavaScript, ``raw`` is marked safe for everything.
.. code-block:: twig
{% autoescape 'js' %}
{{ var|escape('html') }} {# will be escaped for HTML and JavaScript #}
{{ var }} {# will be escaped for JavaScript #}
{{ var|escape('js') }} {# won't be double-escaped #}
{% endautoescape %}
.. note::
Note that autoescaping has some limitations as escaping is applied on
expressions after evaluation. For instance, when working with
concatenation, ``{{ value|raw ~ other }}`` won't give the expected result
as escaping is applied on the result of the concatenation, not on the
individual variables (so, the ``raw`` filter won't have any effect here).
Sandbox Extension
~~~~~~~~~~~~~~~~~
The ``sandbox`` extension can be used to evaluate untrusted code. Read more
about it in the :doc:`sandbox` chapter.
Profiler Extension
~~~~~~~~~~~~~~~~~~
The ``profiler`` extension enables a profiler for Twig templates; it should
only be used on your development machines as it adds some overhead::
$profile = new \Twig\Profiler\Profile();
$twig->addExtension(new \Twig\Extension\ProfilerExtension($profile));
$dumper = new \Twig\Profiler\Dumper\TextDumper();
echo $dumper->dump($profile);
A profile contains information about time and memory consumption for template,
block, and macro executions.
You can also dump the data in a `Blackfire.io `_
compatible format::
$dumper = new \Twig\Profiler\Dumper\BlackfireDumper();
file_put_contents('/path/to/profile.prof', $dumper->dump($profile));
Upload the profile to visualize it (create a `free account
`_
first):
.. code-block:: sh
blackfire --slot=7 upload /path/to/profile.prof
Optimizer Extension
~~~~~~~~~~~~~~~~~~~
The ``optimizer`` extension optimizes the node tree before compilation::
$twig->addExtension(new \Twig\Extension\OptimizerExtension());
By default, all optimizations are turned on. You can select the ones you want
to enable by passing them to the constructor::
$optimizer = new \Twig\Extension\OptimizerExtension(\Twig\NodeVisitor\OptimizerNodeVisitor::OPTIMIZE_FOR);
$twig->addExtension($optimizer);
Twig supports the following optimizations:
* ``\Twig\NodeVisitor\OptimizerNodeVisitor::OPTIMIZE_ALL``, enables all optimizations
(this is the default value).
* ``\Twig\NodeVisitor\OptimizerNodeVisitor::OPTIMIZE_NONE``, disables all optimizations.
This reduces the compilation time, but it can increase the execution time
and the consumed memory.
* ``\Twig\NodeVisitor\OptimizerNodeVisitor::OPTIMIZE_FOR``, optimizes the ``for`` tag by
removing the ``loop`` variable creation whenever possible.
Exceptions
----------
Twig can throw exceptions:
* ``\Twig\Error\Error``: The base exception for all errors.
* ``\Twig\Error\SyntaxError``: Thrown to tell the user that there is a problem with
the template syntax.
* ``\Twig\Error\RuntimeError``: Thrown when an error occurs at runtime (when a filter
does not exist for instance).
* ``\Twig\Error\LoaderError``: Thrown when an error occurs during template loading.
* ``\Twig\Sandbox\SecurityError``: Thrown when an unallowed tag, filter, or
method is called in a sandboxed template.
================================================
FILE: doc/coding_standards.rst
================================================
Coding Standards
================
.. note::
The `Twig CS fixer tool `_
uses the coding standards described in this document to automatically fix
your templates.
When writing Twig templates, we recommend you to follow these official coding
standards:
* Put exactly one space after the start of a delimiter (``{{``, ``{%``,
and ``{#``) and before the end of a delimiter (``}}``, ``%}``, and ``#}``)
if the content is non empty:
.. code-block:: twig
{{ user }}
{# comment #} {##}
{% if user %}{% endif %}
When using the whitespace control character, do not put any spaces between
it and the delimiter:
.. code-block:: twig
{{- user -}}
{#- comment -#} {#--#}
{%- if user -%}{%- endif -%}
* Put exactly one space before and after the following operators:
comparison operators (``==``, ``!=``, ``<``, ``>``, ``>=``, ``<=``), math
operators (``+``, ``-``, ``/``, ``*``, ``%``, ``//``, ``**``), logic
operators (``not``, ``and``, ``or``), ``~``, ``is``, ``in``, and the ternary
operator (``?:``):
.. code-block:: twig
{{ 1 + 2 }}
{{ first_name ~ ' ' ~ last_name }}
{{ is_correct ? true : false }}
* Put exactly one space after the ``:`` sign in mappings and ``,`` in sequences
and mappings:
.. code-block:: twig
[1, 2, 3]
{'name': 'Fabien'}
* Do not put any spaces after an opening parenthesis and before a closing
parenthesis in expressions:
.. code-block:: twig
{{ 1 + (2 * 3) }}
* Do not put any spaces before and after string delimiters:
.. code-block:: twig
{{ 'Twig' }}
{{ "Twig" }}
* Do not put any spaces before and after the following operators: ``|``,
``.``, ``..``, ``[]``:
.. code-block:: twig
{{ name|upper|lower }}
{{ user.name }}
{{ user[name] }}
{% for i in 1..12 %}{% endfor %}
* Do not put any spaces before and after the parenthesis used for filter and
function calls:
.. code-block:: twig
{{ name|default('Fabien') }}
{{ range(1..10) }}
* Do not put any spaces before and after the opening and the closing of
sequences and mappings:
.. code-block:: twig
[1, 2, 3]
{'name': 'Fabien'}
* Put exactly one space before and after ``=`` in macro argument declarations:
.. code-block:: twig
{% macro html_input(class = "input") %}
* Put exactly one space after the ``:`` sign when using named arguments:
.. code-block:: twig
{{ html_input(class: "input") }}
* Use snake case for all variable names (provided by the application and
created in templates), function/filter/test names, argument names and named
arguments:
.. code-block:: twig
{% set name = 'Fabien' %}
{% set first_name = 'Fabien' %}
{{ 'Fabien Potencier'|to_lower_case }}
{{ generate_random_number() }}
{% macro html_input(class_name) %}
{{ html_input(class_name: 'pwd') }}
* Indent your code inside tags (use the same indentation as the one used for
the target language of the rendered template):
.. code-block:: twig
{% block content %}
{% if true %}
true
{% endif %}
{% endblock %}
* Use ``:`` instead of ``=`` to separate argument names and values:
.. code-block:: twig
{{ data|convert_encoding(from: 'iso-2022-jp', to: 'UTF-8') }}
================================================
FILE: doc/deprecated.rst
================================================
Deprecated Features
===================
This document lists deprecated features in Twig 3.x. Deprecated features are
kept for backward compatibility and removed in the next major release (a
feature that was deprecated in Twig 3.x is removed in Twig 4.0).
Functions
---------
* The ``twig_test_iterable`` function is deprecated; use the native PHP
``is_iterable`` function instead.
* The ``attribute`` function is deprecated as of Twig 3.15. Use the ``.``
operator instead and wrap the name with parenthesis:
.. code-block:: twig
{# before #}
{{ attribute(object, method) }}
{{ attribute(object, method, arguments) }}
{{ attribute(array, item) }}
{# after #}
{{ object.(method) }}
{{ object.(method)(arguments) }}
{{ array[item] }}
Note that it won't be removed in 4.0 to allow a smoother upgrade path.
Extensions
----------
* All functions defined in Twig extensions are marked as internal as of Twig
3.9.0, and will be removed in Twig 4.0. They have been replaced by internal
methods on their respective extension classes.
If you were using the ``twig_escape_filter()`` function in your code, use
``$env->getRuntime(EscaperRuntime::class)->escape()`` instead.
* The following methods from ``Twig\Extension\EscaperExtension`` are
deprecated: ``setEscaper()``, ``getEscapers()``, ``setSafeClasses``,
``addSafeClasses()``. Use the same methods on the
``Twig\Runtime\EscaperRuntime`` class instead:
Before:
``$twig->getExtension(EscaperExtension::class)->METHOD();``
After:
``$twig->getRuntime(EscaperRuntime::class)->METHOD();``
Nodes
-----
* The "tag" constructor parameter of the ``Twig\Node\Node`` class is deprecated
as of Twig 3.12 as the tag is now automatically set by the Parser when
needed.
* The following ``Twig\Node\Node`` methods will take a string or an integer
(instead of just a string) in Twig 4.0 for their "name" argument:
``getNode()``, ``hasNode()``, ``setNode()``, ``removeNode()``, and
``deprecateNode()``.
* Not passing a ``BodyNode`` instance as the body of a ``ModuleNode`` or
``MacroNode`` constructor is deprecated as of Twig 3.12.
* Returning ``null`` from ``TokenParserInterface::parse()`` is deprecated as of
Twig 3.12 (as forbidden by the interface).
* The second argument of the
``Twig\Node\Expression\CallExpression::compileArguments()`` method is
deprecated.
* The ``Twig\Node\Expression\NameExpression::isSimple()`` and
``Twig\Node\Expression\NameExpression::isSpecial()`` methods are deprecated as
of Twig 3.11 and will be removed in Twig 4.0.
* The ``filter`` node of ``Twig\Node\Expression\FilterExpression`` is
deprecated as of Twig 3.12 and will be removed in 4.0. Use the ``filter``
attribute instead to get the filter:
Before:
``$node->getNode('filter')->getAttribute('value')``
After:
``$node->getAttribute('twig_callable')->getName()``
* Passing a name to ``Twig\Node\Expression\FunctionExpression``,
``Twig\Node\Expression\FilterExpression``, and
``Twig\Node\Expression\TestExpression`` is deprecated as of Twig 3.12.
As of Twig 4.0, you need to pass a ``TwigFunction``, ``TwigFilter``, or
``TestFilter`` instead.
Let's take a ``FunctionExpression`` as an example.
If you have a node that extends ``FunctionExpression`` and if you don't
override the constructor, you don't need to do anything. But if you override
the constructor, then you need to change the type hint of the name and mark
the constructor with the ``Twig\Attribute\FirstClassTwigCallableReady`` attribute.
Before::
class NotReadyFunctionExpression extends FunctionExpression
{
public function __construct(string $function, Node $arguments, int $lineno)
{
parent::__construct($function, $arguments, $lineno);
}
}
class NotReadyFilterExpression extends FilterExpression
{
public function __construct(Node $node, ConstantExpression $filter, Node $arguments, int $lineno)
{
parent::__construct($node, $filter, $arguments, $lineno);
}
}
class NotReadyTestExpression extends TestExpression
{
public function __construct(Node $node, string $test, ?Node $arguments, int $lineno)
{
parent::__construct($node, $test, $arguments, $lineno);
}
}
After::
class ReadyFunctionExpression extends FunctionExpression
{
#[FirstClassTwigCallableReady]
public function __construct(TwigFunction|string $function, Node $arguments, int $lineno)
{
parent::__construct($function, $arguments, $lineno);
}
}
class ReadyFilterExpression extends FilterExpression
{
#[FirstClassTwigCallableReady]
public function __construct(Node $node, TwigFilter|ConstantExpression $filter, Node $arguments, int $lineno)
{
parent::__construct($node, $filter, $arguments, $lineno);
}
}
class ReadyTestExpression extends TestExpression
{
#[FirstClassTwigCallableReady]
public function __construct(Node $node, TwigTest|string $test, ?Node $arguments, int $lineno)
{
parent::__construct($node, $test, $arguments, $lineno);
}
}
* The following ``Twig\Node\Expression\FunctionExpression`` attributes are
deprecated as of Twig 3.12: ``needs_charset``, ``needs_environment``,
``needs_context``, ``arguments``, ``callable``, ``is_variadic``,
and ``dynamic_name``.
* The following ``Twig\Node\Expression\FilterExpression`` attributes are
deprecated as of Twig 3.12: ``needs_charset``, ``needs_environment``,
``needs_context``, ``arguments``, ``callable``, ``is_variadic``,
and ``dynamic_name``.
* The following ``Twig\Node\Expression\TestExpression`` attributes are
deprecated as of Twig 3.12: ``arguments``, ``callable``, ``is_variadic``,
and ``dynamic_name``.
* The ``MethodCallExpression`` class is deprecated as of Twig 3.15, use
``MacroReferenceExpression`` instead.
* The ``Twig\Node\Expression\TempNameExpression`` class is deprecated as of
Twig 3.15; use ``Twig\Node\Expression\Variable\LocalVariable`` instead.
* The ``Twig\Node\Expression\NameExpression`` class is deprecated as of Twig
3.15; use ``Twig\Node\Expression\Variable\ContextVariable`` instead.
* The ``Twig\Node\Expression\AssignNameExpression`` class is deprecated as of
Twig 3.15; use ``Twig\Node\Expression\Variable\AssignContextVariable``
instead.
* Node implementations that use ``echo`` or ``print`` should use ``yield``
instead; all Node implementations should use the
``#[\Twig\Attribute\YieldReady]`` attribute on their class once they've been
made ready for ``yield``; the ``use_yield`` Environment option can be turned
on when all nodes use the ``#[\Twig\Attribute\YieldReady]`` attribute.
* The ``Twig\Node\InlinePrint`` class is deprecated as of Twig 3.16 with no
replacement.
* The ``Twig\Node\Expression\NullCoalesceExpression`` class is deprecated as
of Twig 3.17, use ``Twig\Node\Expression\Binary\NullCoalesceBinary``
instead.
* The ``Twig\Node\Expression\ConditionalExpression`` class is deprecated as of
Twig 3.17, use ``Twig\Node\Expression\Ternary\ConditionalTernary`` instead.
* The ``is_defined_test`` attribute is deprecated as of Twig 3.21, use
``Twig\Node\Expression\SupportDefinedTestInterface`` instead.
* Instantiating ``Twig\Node\Node`` directly is deprecated as of Twig 3.15. Use
``EmptyNode`` or ``Nodes`` instead depending on the use case. The
``Twig\Node\Node`` class will be abstract in Twig 4.0.
* Not passing ``AbstractExpression`` arguments to the following ``Node`` class
constructors is deprecated as of Twig 3.15:
* ``AbstractBinary``
* ``AbstractUnary``
* ``BlockReferenceExpression``
* ``TestExpression``
* ``DefinedTest``
* ``FilterExpression``
* ``RawFilter``
* ``DefaultFilter``
* ``InlinePrint``
* ``NullCoalesceExpression``
Node Visitors
-------------
* The ``Twig\NodeVisitor\AbstractNodeVisitor`` class is deprecated, implement the
``Twig\NodeVisitor\NodeVisitorInterface`` interface instead.
* The ``Twig\NodeVisitor\OptimizerNodeVisitor::OPTIMIZE_RAW_FILTER`` and the
``Twig\NodeVisitor\OptimizerNodeVisitor::OPTIMIZE_TEXT_NODES`` options are
deprecated as of Twig 3.12 and will be removed in Twig 4.0; they don't do
anything anymore.
Parser
------
* The following methods from ``Twig\Parser`` are deprecated as of Twig 3.12:
``getBlockStack()``, ``hasBlock()``, ``getBlock()``, ``hasMacro()``,
``hasTraits()``, ``getParent()``.
* Passing ``null`` to ``Twig\Parser::setParent()`` is deprecated as of Twig
3.12.
* Passing a non-``AbstractExpression`` node to ``Twig\Parser::setParent()`` is
deprecated as of Twig 3.24; the method will require an ``AbstractExpression``
instance in Twig 4.0.
* Passing non-``AbstractExpression`` nodes to
``Twig\Node\Expression\Binary\MatchesBinary`` constructor is deprecated as of
Twig 3.24; the constructor will require an ``AbstractExpression`` instance in Twig
4.0.
* The ``Twig\Parser::getExpressionParser()`` method is deprecated as of Twig
3.21, use ``Twig\Parser::parseExpression()`` instead.
* The ``Twig\ExpressionParser`` class is deprecated as of Twig 3.21:
* ``parseExpression()``, use ``Parser::parseExpression()``
* ``parsePrimaryExpression()``, use ``Parser::parseExpression()``
* ``parseStringExpression()``, use ``Parser::parseExpression()``
* ``parseHashExpression()``, use ``Parser::parseExpression()``
* ``parseMappingExpression()``, use ``Parser::parseExpression()``
* ``parseArrayExpression()``, use ``Parser::parseExpression()``
* ``parseSequenceExpression()``, use ``Parser::parseExpression()``
* ``parsePostfixExpression``
* ``parseSubscriptExpression``
* ``parseFilterExpression``
* ``parseFilterExpressionRaw``
* ``parseArguments()``, use ``Twig\ExpressionParser\Infix\ArgumentsTrait::parseNamedArguments()``
* ``parseAssignmentExpression``, use ``AbstractTokenParser::parseAssignmentExpression``
* ``parseMultitargetExpression``
* ``parseOnlyArguments()``, use ``Twig\ExpressionParser\Infix\ArgumentsTrait::parseNamedArguments()``
Token
-----
* Not passing a ``Source`` instance to ``Twig\TokenStream`` constructor is
deprecated as of Twig 3.16.
* The ``Token::getType()`` method is deprecated as of Twig 3.19, use
``Token::test()`` instead.
* The ``Token::ARROW_TYPE`` constant is deprecated as of Twig 3.21, the arrow
``=>`` is now an operator (``Token::OPERATOR_TYPE``).
* The ``Token::PUNCTUATION_TYPE`` with values ``(``, ``[``, ``|``, ``.``,
``?``, or ``?:`` are now of the ``Token::OPERATOR_TYPE`` type.
Templates
---------
* The method ``Template::loadTemplate()`` is deprecated.
* Passing ``Twig\Template`` instances to Twig public API is deprecated (like
in ``Environment::resolveTemplate()`` and ``Environment::load()``); pass
instances of ``Twig\TemplateWrapper`` instead.
Filters
-------
* The ``spaceless`` filter is deprecated as of Twig 3.12 and will be removed in
Twig 4.0.
Sandbox
-------
* Having the ``extends`` and ``use`` tags allowed by default in a sandbox is
deprecated as of Twig 3.12. You will need to explicitly allow them if needed
in 4.0.
* Deprecate the ``sandbox`` tag, use the ``sandboxed`` option of the
``include`` function instead:
Before::
{% sandbox %}
{% include 'user_defined.html.twig' %}
{% endsandbox %}
After::
{{ include('user_defined.html.twig', sandboxed: true) }}
Testing Utilities
-----------------
* Implementing the data provider method ``Twig\Test\NodeTestCase::getTests()``
is deprecated as of Twig 3.13. Instead, implement the static data provider
``provideTests()``.
* In order to make their functionality available for static data providers, the
helper methods ``getVariableGetter()`` and ``getAttributeGetter()`` on
``Twig\Test\NodeTestCase`` have been deprecated. Call the new methods
``createVariableGetter()`` and ``createAttributeGetter()`` instead.
* The method ``Twig\Test\NodeTestCase::getEnvironment()`` is considered final
as of Twig 3.13. If you want to override how the Twig environment is
constructed, override ``createEnvironment()`` instead.
* The method ``getFixturesDir()`` on ``Twig\Test\IntegrationTestCase`` is
deprecated, implement the new static method ``getFixturesDirectory()``
instead, which will be abstract in 4.0.
* The data providers ``getTests()`` and ``getLegacyTests()`` on
``Twig\Test\IntegrationTestCase`` are considered final as of Twig 3.13.
Environment
-----------
* The ``Twig\Environment::mergeGlobals()`` method is deprecated as of Twig 3.14
and will be removed in Twig 4.0:
Before::
$context = $twig->mergeGlobals($context);
After::
$context += $twig->getGlobals();
Functions/Filters/Tests
-----------------------
* The ``deprecated``, ``deprecating_package``, ``alternative`` options on Twig
functions/filters/Tests are deprecated as of Twig 3.15, and will be removed
in Twig 4.0. Use the ``deprecation_info`` option instead:
Before::
$twig->addFunction(new TwigFunction('upper', 'upper', [
'deprecated' => '3.12', 'deprecating_package' => 'twig/twig',
]));
After::
$twig->addFunction(new TwigFunction('upper', 'upper', [
'deprecation_info' => new DeprecatedCallableInfo('twig/twig', '3.12'),
]));
* For variadic arguments, use snake-case for the argument name to ease the
transition to 4.0.
* Passing a ``string`` or an ``array`` to Twig callable arguments accepting
arrow functions is deprecated as of Twig 3.15; these arguments will have a
``\Closure`` type hint in 4.0.
* Returning ``null`` from ``TwigFilter::getSafe()`` and
``TwigFunction::getSafe()`` is deprecated as of Twig 3.16; return ``[]``
instead.
Operators
---------
* An operator precedence must be part of the [0, 512] range as of Twig 3.21.
* The ``.`` operator allows accessing class constants as of Twig 3.15.
This can be a BC break if you don't use UPPERCASE constant names.
* Using ``~`` in an expression with the ``+`` or ``-`` operators without using
parentheses to clarify precedence triggers a deprecation as of Twig 3.15 (in
Twig 4.0, ``+`` / ``-`` will have a higher precedence than ``~``).
For example, the following expression will trigger a deprecation in Twig 3.15::
{{ '42' ~ 1 + 41 }}
To avoid the deprecation, wrap the concatenation in parentheses to clarify
the precedence::
{{ ('42' ~ 1) + 41 }} {# this is equivalent to what Twig 3.x does without the parentheses #}
{# or #}
{{ '42' ~ (1 + 41) }} {# this is equivalent to what Twig 4.x will do without the parentheses #}
* Using ``??`` without explicit parentheses to clarify precedence triggers a
deprecation as of Twig 3.15 (in Twig 4.0, ``??`` will have the lowest
precedence).
For example, the following expression will trigger a deprecation in Twig 3.15::
{{ 'notnull' ?? 'foo' ~ '_bar' }}
To avoid the deprecation, wrap the ``??`` expression in parentheses to clarify
the precedence::
{{ ('notnull' ?? 'foo') ~ '_bar' }} {# this is equivalent to what Twig 3.x does without the parentheses #}
{# or #}
{{ 'notnull' ?? ('foo' ~ '_bar') }} {# this is equivalent to what Twig 4.x will do without the parentheses #}
* Using the ``not`` unary operator in an expression with ``*``, ``/``, ``//``,
or ``%`` operators without explicit parentheses to clarify precedence
triggers a deprecation as of Twig 3.15 (in Twig 4.0, ``not`` will have a
higher precedence than ``*``, ``/``, ``//``, and ``%``).
For example, the following expression will trigger a deprecation in Twig 3.15::
{{ not 1 * 2 }}
To avoid the deprecation, wrap the concatenation in parentheses to clarify
the precedence::
{{ (not 1 * 2) }} {# this is equivalent to what Twig 3.x does without the parentheses #}
{# or #}
{{ (not 1) * 2 }} {# this is equivalent to what Twig 4.x will do without the parentheses #}
* Using the ``|`` operator in an expression with ``+`` or ``-`` without explicit
parentheses to clarify precedence triggers a deprecation as of Twig 3.21 (in
Twig 4.0, ``|`` will have a higher precedence than ``+`` and ``-``).
For example, the following expression will trigger a deprecation in Twig 3.21::
{{ -1|abs }}
To avoid the deprecation, add parentheses to clarify the precedence::
{{ -(1|abs) }} {# this is equivalent to what Twig 3.x does without the parentheses #}
{# or #}
{{ (-1)|abs }} {# this is equivalent to what Twig 4.x will do without the parentheses #}
* The ``Twig\Extension\ExtensionInterface::getOperators()`` method is deprecated
as of Twig 3.21, use ``Twig\Extension\ExtensionInterface::getExpressionParsers()``
instead:
Before::
public function getOperators(): array {
return [
'not' => [
'precedence' => 10,
'class' => NotUnary::class,
],
];
}
After::
public function getExpressionParsers(): array {
return [
new UnaryOperatorExpressionParser(NotUnary::class, 'not', 10),
];
}
* The ``Twig\OperatorPrecedenceChange`` class is deprecated as of Twig 3.21,
use ``Twig\ExpressionParser\PrecedenceChange`` instead.
* Not implementing the ``getOperatorTokens()`` method in
``Twig\ExpressionParser\ExpressionParserInterface`` implementations is
deprecated as of Twig 3.24. This method will be added to the interface in
Twig 4.0. It returns the operator token strings that the expression parser
handles (used by the Lexer and the parser registry). If your custom
expression parser extends ``Twig\ExpressionParser\AbstractExpressionParser``,
the default implementation returns ``[$this->getName(), ...$this->getAliases()]``.
Override it if your parser doesn't handle operator tokens (return ``[]``) or if
the operator tokens differ from the parser name.
================================================
FILE: doc/filters/abs.rst
================================================
``abs``
=======
The ``abs`` filter returns the absolute value.
.. code-block:: twig
{# number = -5 #}
{{ number|abs }}
{# outputs 5 #}
.. note::
Internally, Twig uses the PHP `abs`_ function.
.. _`abs`: https://www.php.net/abs
================================================
FILE: doc/filters/batch.rst
================================================
``batch``
=========
The ``batch`` filter "batches" items by returning a list of lists with the
given number of items. A second parameter can be provided and used to fill in
missing items:
.. code-block:: html+twig
{% set items = ['a', 'b', 'c', 'd'] %}
{% for row in items|batch(3, 'No item') %}
{% for index, column in row %}
{{ index }} - {{ column }}
{% endfor %}
{% endfor %}
The above example will be rendered as:
.. code-block:: html+twig
0 - a
1 - b
2 - c
3 - d
4 - No item
5 - No item
If you choose to set the third parameter ``preserve_keys`` to ``false``, the keys will be reset in each loop.
.. code-block:: html+twig
{% set items = ['a', 'b', 'c', 'd'] %}
{% for row in items|batch(3, 'No item', false) %}
{% for index, column in row %}
{{ index }} - {{ column }}
{% endfor %}
{% endfor %}
The above example will be rendered as:
.. code-block:: html+twig
0 - a
1 - b
2 - c
0 - d
1 - No item
2 - No item
Arguments
---------
* ``size``: The size of the batch; fractional numbers will be rounded up
* ``fill``: Used to fill in missing items
* ``preserve_keys``: Whether to preserve keys or not (defaults to ``true``)
================================================
FILE: doc/filters/capitalize.rst
================================================
``capitalize``
==============
The ``capitalize`` filter capitalizes a value. The first character will be
uppercase, all others lowercase:
.. code-block:: twig
{{ 'my first car'|capitalize }}
{# outputs 'My first car' #}
================================================
FILE: doc/filters/column.rst
================================================
``column``
==========
The ``column`` filter returns the values from a single column in the input
array.
.. code-block:: twig
{% set items = [{ 'fruit' : 'apple'}, {'fruit' : 'orange' }] %}
{% set fruits = items|column('fruit') %}
{# fruits now contains ['apple', 'orange'] #}
.. note::
Internally, Twig uses the PHP `array_column`_ function.
Arguments
---------
* ``name``: The column name to extract
.. _`array_column`: https://www.php.net/array_column
================================================
FILE: doc/filters/convert_encoding.rst
================================================
``convert_encoding``
====================
The ``convert_encoding`` filter converts a string from one encoding to
another. The first argument is the expected output charset and the second one
is the input charset:
.. code-block:: twig
{{ data|convert_encoding('UTF-8', 'iso-2022-jp') }}
.. note::
This filter relies on the `iconv`_ extension.
Arguments
---------
* ``to``: The output charset
* ``from``: The input charset
.. _`iconv`: https://www.php.net/iconv
================================================
FILE: doc/filters/country_name.rst
================================================
``country_name``
================
The ``country_name`` filter returns the country name given its ISO-3166 code:
.. code-block:: twig
{# France #}
{{ 'FR'|country_name }}
By default, the filter uses the current locale. You can pass it explicitly:
.. code-block:: twig
{# États-Unis #}
{{ 'US'|country_name('fr') }}
{# 美國 #}
{{ 'US'|country_name('zh_Hant_HK') }}
.. note::
The ``country_name`` filter is part of the ``IntlExtension`` which is not
installed by default. Install it first:
.. code-block:: bash
$ composer require twig/intl-extra
Then, on Symfony projects, install the ``twig/extra-bundle``:
.. code-block:: bash
$ composer require twig/extra-bundle
Otherwise, add the extension explicitly on the Twig environment::
use Twig\Extra\Intl\IntlExtension;
$twig = new \Twig\Environment(...);
$twig->addExtension(new IntlExtension());
Arguments
---------
* ``locale``: The locale code as defined in `RFC 5646`_
.. _RFC 5646: https://www.rfc-editor.org/info/rfc5646
================================================
FILE: doc/filters/currency_name.rst
================================================
``currency_name``
=================
The ``currency_name`` filter returns the currency name given its ISO 4217 code:
.. code-block:: twig
{# Euro #}
{{ 'EUR'|currency_name }}
{# Japanese Yen #}
{{ 'JPY'|currency_name }}
By default, the filter uses the current locale. You can pass it explicitly:
.. code-block:: twig
{# yen japonais #}
{{ 'JPY'|currency_name('fr_FR') }}
.. note::
The ``currency_name`` filter is part of the ``IntlExtension`` which is not
installed by default. Install it first:
.. code-block:: bash
$ composer require twig/intl-extra
Then, on Symfony projects, install the ``twig/extra-bundle``:
.. code-block:: bash
$ composer require twig/extra-bundle
Otherwise, add the extension explicitly on the Twig environment::
use Twig\Extra\Intl\IntlExtension;
$twig = new \Twig\Environment(...);
$twig->addExtension(new IntlExtension());
Arguments
---------
* ``locale``: The locale code as defined in `RFC 5646`_
.. _RFC 5646: https://www.rfc-editor.org/info/rfc5646
================================================
FILE: doc/filters/currency_symbol.rst
================================================
``currency_symbol``
===================
The ``currency_symbol`` filter returns the currency symbol given its ISO 4217
code:
.. code-block:: twig
{# € #}
{{ 'EUR'|currency_symbol }}
{# ¥ #}
{{ 'JPY'|currency_symbol }}
By default, the filter uses the current locale. You can pass it explicitly:
.. code-block:: twig
{# ¥ #}
{{ 'JPY'|currency_symbol('fr') }}
.. note::
The ``currency_symbol`` filter is part of the ``IntlExtension`` which is not
installed by default. Install it first:
.. code-block:: bash
$ composer require twig/intl-extra
Then, on Symfony projects, install the ``twig/extra-bundle``:
.. code-block:: bash
$ composer require twig/extra-bundle
Otherwise, add the extension explicitly on the Twig environment::
use Twig\Extra\Intl\IntlExtension;
$twig = new \Twig\Environment(...);
$twig->addExtension(new IntlExtension());
Arguments
---------
* ``locale``: The locale code as defined in `RFC 5646`_
.. _RFC 5646: https://www.rfc-editor.org/info/rfc5646
================================================
FILE: doc/filters/data_uri.rst
================================================
``data_uri``
============
The ``data_uri`` filter generates a URL using the data scheme as defined in
`RFC 2397`_:
.. code-block:: html+twig
{{ image_data|data_uri }}
{{ source('path_to_image')|data_uri }}
{# force the mime type, disable the guessing of the mime type #}
{{ image_data|data_uri(mime: "image/svg") }}
{# also works with plain text #}
{{ 'foobar'|data_uri(mime: "text/html") }}
{# add some extra parameters #}
{{ 'foobar'|data_uri(mime: "text/html", parameters: {charset: "ascii"}) }}
.. note::
The ``data_uri`` filter is part of the ``HtmlExtension`` which is not
installed by default. Install it first:
.. code-block:: bash
$ composer require twig/html-extra
Then, on Symfony projects, install the ``twig/extra-bundle``:
.. code-block:: bash
$ composer require twig/extra-bundle
Otherwise, add the extension explicitly on the Twig environment::
use Twig\Extra\Html\HtmlExtension;
$twig = new \Twig\Environment(...);
$twig->addExtension(new HtmlExtension());
.. note::
The filter does not perform any length validation on purpose (limit depends
on the usage context), validation should be done before calling this filter.
Arguments
---------
* ``mime``: The mime type
* ``parameters``: A mapping of parameters
.. _RFC 2397: https://tools.ietf.org/html/rfc2397
================================================
FILE: doc/filters/date.rst
================================================
``date``
========
The ``date`` filter formats a date to a given format:
.. code-block:: twig
{{ post.published_at|date("m/d/Y") }}
The format specifier is the same as supported by `date`_,
except when the filtered data is of type `DateInterval`_, when the format must conform to
`DateInterval::format`_ instead.
The ``date`` filter accepts strings (it must be in a format supported by the
`strtotime`_ function), `DateTime`_ instances, or `DateInterval`_ instances. For
instance, to display the current date, filter the word "now":
.. code-block:: twig
{{ "now"|date("m/d/Y") }}
To escape words and characters in the date format use ``\\`` in front of each
character:
.. code-block:: twig
{{ post.published_at|date("F jS \\a\\t g:ia") }}
If the value passed to the ``date`` filter is ``null``, it will return the
current date by default. If an empty string is desired instead of the current
date, use a ternary operator:
.. code-block:: twig
{{ post.published_at is empty ? "" : post.published_at|date("m/d/Y") }}
If no format is provided, Twig will use the default one: ``F j, Y H:i``. This
default can be changed by calling the ``setDateFormat()`` method on the
``core`` extension instance. The first argument is the default format for
dates and the second one is the default format for date intervals::
$twig = new \Twig\Environment($loader);
$twig->getExtension(\Twig\Extension\CoreExtension::class)->setDateFormat('d/m/Y', '%d days');
Timezone
--------
By default, the date is displayed by applying the default timezone (the one
specified in php.ini or declared in Twig -- see below), but you can override
it by explicitly specifying a supported `timezone`_:
.. code-block:: twig
{{ post.published_at|date("m/d/Y", "Europe/Paris") }}
If the date is already a DateTime object, and if you want to keep its current
timezone, pass ``false`` as the timezone value:
.. code-block:: twig
{{ post.published_at|date("m/d/Y", false) }}
The default timezone can also be set globally by calling ``setTimezone()``::
$twig = new \Twig\Environment($loader);
$twig->getExtension(\Twig\Extension\CoreExtension::class)->setTimezone('Europe/Paris');
Arguments
---------
* ``format``: The date format (default format is ``F j, Y H:i``, which will render as ``January 11, 2024 15:17``)
* ``timezone``: The date timezone
.. _`strtotime`: https://www.php.net/strtotime
.. _`DateTime`: https://www.php.net/DateTime
.. _`DateInterval`: https://www.php.net/DateInterval
.. _`date`: https://www.php.net/date
.. _`DateInterval::format`: https://www.php.net/DateInterval.format
.. _`timezone`: https://www.php.net/manual/en/timezones.php
================================================
FILE: doc/filters/date_modify.rst
================================================
``date_modify``
===============
The ``date_modify`` filter modifies a date with a given modifier string:
.. code-block:: twig
{{ post.published_at|date_modify("+1 day")|date("m/d/Y") }}
The ``date_modify`` filter accepts strings (it must be in a format supported
by the `strtotime`_ function) or `DateTime`_ instances. You can combine
it with the :doc:`date` filter for formatting.
Arguments
---------
* ``modifier``: The modifier
.. _`strtotime`: https://www.php.net/strtotime
.. _`DateTime`: https://www.php.net/DateTime
================================================
FILE: doc/filters/default.rst
================================================
``default``
===========
The ``default`` filter returns the passed default value if the value is
undefined or empty, otherwise the value of the variable:
.. code-block:: twig
{{ var|default('var is not defined') }}
{{ user.name|default('name item on user is not defined') }}
{{ user['name']|default('name item on user is not defined') }}
{{ ''|default('passed var is empty') }}
When using the ``default`` filter on an expression that uses variables in some
method calls, be sure to use the ``default`` filter whenever a variable can be
undefined:
.. code-block:: twig
{{ user.value(name|default('username'))|default('not defined') }}
Using the ``default`` filter on a boolean variable might trigger unexpected
behavior, as ``false`` is treated as an empty value. Consider using ``??``
instead:
.. code-block:: twig
{% set value = false %}
{{ value|default(true) }} {# true #}
{{ value ?? true }} {# false #}
.. note::
Read the documentation for the :doc:`defined<../tests/defined>` and
:doc:`empty<../tests/empty>` tests to learn more about their semantics.
Arguments
---------
* ``default``: The default value
================================================
FILE: doc/filters/escape.rst
================================================
``escape``
==========
The ``escape`` filter escapes a string using strategies that depend on the
context.
By default, it uses the HTML escaping strategy:
.. code-block:: html+twig
{{ user.username|escape }}
For convenience, the ``e`` filter is defined as an alias:
.. code-block:: html+twig
{{ user.username|e }}
The ``escape`` filter can also be used in other contexts than HTML thanks to
an optional argument which defines the escaping strategy to use:
.. code-block:: twig
{{ user.username|e }}
{# is equivalent to #}
{{ user.username|e('html') }}
And here is how to escape variables included in JavaScript code:
.. code-block:: twig
{{ user.username|escape('js') }}
{{ user.username|e('js') }}
The ``escape`` filter supports the following escaping strategies for HTML
documents:
* ``html``: escapes a string for the **HTML body** context,
or for HTML attributes values **inside quotes**.
* ``js``: escapes a string for the **JavaScript** context. This is intended for
use in JavaScript or JSON strings, and encodes values using backslash escape
sequences.
* ``css``: escapes a string for the **CSS** context. CSS escaping can be
applied to any string being inserted into CSS and escapes everything except
alphanumerics.
* ``url``: escapes a string for the **URI or parameter** contexts. This should
not be used to escape an entire URI; only a subcomponent being inserted.
* ``html_attr``: escapes a string when used as an **HTML attribute** name, and
also when used as the value of an HTML attribute **without quotes**
(e.g. ``data-attribute={{ some_value }}``).
* ``html_attr_relaxed``: like ``html_attr``, but **does not** escape the ``@``, ``:``,
``[`` and ``]`` characters. You may want to use this in combination with front-end
frameworks that use attribute names like ``v-bind:href`` or ``@click``. But, be
aware that in some processing contexts like XML, characters like the colon ``:``
may have meaning like for XML namespace separation.
.. versionadded:: 3.24
The ``html_attr_relaxed`` strategy has been added in 3.23.
Note that doing contextual escaping in HTML documents is hard and choosing the
right escaping strategy depends on a lot of factors. Please, read related
documentation like `the OWASP prevention cheat sheet
`_
to learn more about this topic.
.. note::
Internally, ``escape`` uses the PHP native `htmlspecialchars`_ function
for the HTML escaping strategy.
.. caution::
When using automatic escaping, Twig tries to not double-escape a variable
when the automatic escaping strategy is the same as the one applied by the
escape filter; but that does not work when using a variable as the
escaping strategy:
.. code-block:: twig
{% set strategy = 'html' %}
{% autoescape 'html' %}
{{ var|escape('html') }} {# won't be double-escaped #}
{{ var|escape(strategy) }} {# will be double-escaped #}
{% endautoescape %}
When using a variable as the escaping strategy, you should disable
automatic escaping:
.. code-block:: twig
{% set strategy = 'html' %}
{% autoescape 'html' %}
{{ var|escape(strategy)|raw }} {# won't be double-escaped #}
{% endautoescape %}
.. tip::
The ``html_attr`` escaping strategy can be useful when you need to escape a
**dynamic HTML attribute name**:
.. code-block:: html+twig
It can also be used for escaping a **dynamic HTML attribute value** if it is
not quoted, but this is **less performant**. Instead, it is recommended to
quote the HTML attribute value and use the ``html`` escaping strategy:
.. code-block:: html+twig
{# this is equivalent, but less performant #}
Custom Escapers
---------------
.. versionadded:: 3.10
The ``EscaperRuntime`` class has been added in 3.10. On previous versions,
you can define custom escapers by calling the ``setEscaper()`` method on
the escaper extension instance. The first argument is the escaper strategy
(to be used in the ``escape`` call) and the second one must be a valid PHP
callable::
use Twig\Extension\EscaperExtension;
$twig = new \Twig\Environment($loader);
$twig->getExtension(EscaperExtension::class)->setEscaper('csv', 'csv_escaper');
When called by Twig, the callable receives the Twig environment instance,
the string to escape, and the charset.
You can define custom escapers by calling the ``setEscaper()`` method on the
escaper runtime instance. It accepts two arguments: the strategy name and a PHP
callable that accepts a string to escape and the charset::
use Twig\Runtime\EscaperRuntime;
$twig = new \Twig\Environment($loader);
$escaper = fn ($string, $charset) => $string;
$twig->getRuntime(EscaperRuntime::class)->setEscaper('identity', $escaper);
# Usage in a template:
# {{ 'Twig'|escape('identity') }}
.. note::
Built-in escapers cannot be overridden mainly because they should be
considered as the final implementation and also for better performance.
Arguments
---------
* ``strategy``: The escaping strategy
* ``charset``: The string charset
.. _`htmlspecialchars`: https://www.php.net/htmlspecialchars
================================================
FILE: doc/filters/filter.rst
================================================
``filter``
==========
The ``filter`` filter filters elements of a sequence or a mapping using an arrow
function. The arrow function receives the value of the sequence or mapping:
.. code-block:: twig
{% set sizes = [34, 36, 38, 40, 42] %}
{{ sizes|filter(v => v > 38)|join(', ') }}
{# output 40, 42 #}
Combined with the ``for`` tag, it allows you to filter the items to iterate over:
.. code-block:: twig
{% for v in sizes|filter(v => v > 38) -%}
{{ v }}
{% endfor %}
{# output 40 42 #}
It also works with mappings:
.. code-block:: twig
{% set sizes = {
xs: 34,
s: 36,
m: 38,
l: 40,
xl: 42,
} %}
{% for k, v in sizes|filter(v => v > 38) -%}
{{ k }} = {{ v }}
{% endfor %}
{# output l = 40 xl = 42 #}
The arrow function also receives the key as a second argument:
.. code-block:: twig
{% for k, v in sizes|filter((v, k) => v > 38 and k != "xl") -%}
{{ k }} = {{ v }}
{% endfor %}
{# output l = 40 #}
Note that the arrow function has access to the current context.
Arguments
---------
* ``array``: The sequence or mapping
* ``arrow``: The arrow function
================================================
FILE: doc/filters/find.rst
================================================
``find``
========
.. versionadded:: 3.11
The ``find`` filter was added in Twig 3.11.
The ``find`` filter returns the first element of a sequence matching an arrow
function. The arrow function receives the value of the sequence:
.. code-block:: twig
{% set sizes = [34, 36, 38, 40, 42] %}
{{ sizes|find(v => v > 38) }}
{# output 40 #}
It also works with mappings:
.. code-block:: twig
{% set sizes = {
xxs: 32,
xs: 34,
s: 36,
m: 38,
l: 40,
xl: 42,
} %}
{{ sizes|find(v => v > 38) }}
{# output 40 #}
The arrow function also receives the key as a second argument:
.. code-block:: twig
{{ sizes|find((v, k) => 's' not in k) }}
{# output 38 #}
Note that the arrow function has access to the current context:
.. code-block:: twig
{% set my_size = 39 %}
{{ sizes|find(v => v >= my_size) }}
{# output 40 #}
Arguments
---------
* ``array``: The sequence or mapping
* ``arrow``: The arrow function
================================================
FILE: doc/filters/first.rst
================================================
``first``
=========
The ``first`` filter returns the first "element" of a sequence, a mapping, or
a string:
.. code-block:: twig
{{ [1, 2, 3, 4]|first }}
{# outputs 1 #}
{{ {a: 1, b: 2, c: 3, d: 4}|first }}
{# outputs 1 #}
{{ '1234'|first }}
{# outputs 1 #}
.. note::
It also works with objects implementing the `Traversable`_ interface.
.. _`Traversable`: https://www.php.net/manual/en/class.traversable.php
================================================
FILE: doc/filters/format.rst
================================================
``format``
==========
The ``format`` filter formats a given string by replacing the placeholders
(placeholders follows the `sprintf`_ notation):
.. code-block:: twig
{% set fruit = 'apples' %}
{{ "I like %s and %s."|format(fruit, "oranges") }}
{# outputs I like apples and oranges #}
.. seealso::
:doc:`replace`
.. _`sprintf`: https://www.php.net/sprintf
================================================
FILE: doc/filters/format_currency.rst
================================================
``format_currency``
===================
The ``format_currency`` filter formats a number as a currency:
.. code-block:: twig
{# €1,000,000.00 #}
{{ '1000000'|format_currency('EUR') }}
You can pass attributes to tweak the output:
.. code-block:: twig
{# €12.34 #}
{{ '12.345'|format_currency('EUR', {rounding_mode: 'floor'}) }}
{# €1,000,000.0000 #}
{{ '1000000'|format_currency('EUR', {fraction_digit: 4}) }}
The list of supported options:
* ``grouping_used``: Specifies whether to use grouping separator for thousands::
{# €1,234,567.89 #}
{{ 1234567.89 | format_currency('EUR', {grouping_used:true}, 'en') }}
* ``decimal_always_shown``: Specifies whether to always show the decimal part, even if it's zero::
{# €123.00 #}
{{ 123 | format_currency('EUR', {decimal_always_shown:true}, 'en') }}
* ``max_integer_digit``:
* ``min_integer_digit``:
* ``integer_digit``: Define constraints on the integer part::
{# €345.68 #}
{{ 12345.6789 | format_currency('EUR', {max_integer_digit:3, min_integer_digit:2}, 'en') }}
* ``max_fraction_digit``:
* ``min_fraction_digit``:
* ``fraction_digit``: Define constraints on the fraction part::
{# €123.46 #}
{{ 123.456789 | format_currency('EUR', {max_fraction_digit:2, min_fraction_digit:1}, 'en') }}
* ``multiplier``: Multiplies the value before formatting::
{# €123,000.00 #}
{{ 123 | format_currency('EUR', {multiplier:1000}, 'en') }}
* ``grouping_size``:
* ``secondary_grouping_size``: Set the size of the primary and secondary grouping separators::
{# €1,23,45,678.00 #}
{{ 12345678 | format_currency('EUR', {grouping_size:3, secondary_grouping_size:2}, 'en') }}
* ``rounding_mode``:
* ``rounding_increment``: Control rounding behavior, here is a list of all rounding_mode available:
* ``ceil``: Ceiling rounding
* ``floor``: Floor rounding
* ``down``: Rounding towards zero
* ``up``: Rounding away from zero
* ``half_even``: Round halves to the nearest even integer
* ``half_up``: Round halves up
* ``half_down``: Round halves down
.. code-block:: twig
{# €123.50 #}
{{ 123.456 | format_currency('EUR', {rounding_mode:'ceiling', rounding_increment:0.05}, 'en') }}
* ``format_width``:
* ``padding_position``: Set width and padding for the formatted number, here is a list of all padding_position available:
* ``before_prefix``: Pad before the currency symbol
* ``after_prefix``: Pad after the currency symbol
* ``before_suffix``: Pad before the suffix (currency symbol)
* ``after_suffix``: Pad after the suffix (currency symbol)
.. code-block:: twig
{# €123.00 #}
{{ 123 | format_currency('EUR', {format_width:10, padding_position:'before_suffix'}, 'en') }}
* ``significant_digits_used``:
* ``min_significant_digits_used``:
* ``max_significant_digits_used``: Control significant digits in formatting::
{# €123.4568 #}
{{ 123.456789 | format_currency('EUR', {significant_digits_used:true, min_significant_digits_used:4, max_significant_digits_used:7}, 'en') }}
* ``lenient_parse``: If true, allows lenient parsing of the input::
{# €123.00 #}
{{ 123 | format_currency('EUR', {lenient_parse:true}, 'en') }}
By default, the filter uses the current locale. You can pass it explicitly::
{# 1.000.000,00 € #}
{{ '1000000'|format_currency('EUR', locale: 'de') }}
.. note::
The ``format_currency`` filter is part of the ``IntlExtension`` which is not
installed by default. Install it first:
.. code-block:: bash
$ composer require twig/intl-extra
Then, on Symfony projects, install the ``twig/extra-bundle``:
.. code-block:: bash
$ composer require twig/extra-bundle
Otherwise, add the extension explicitly on the Twig environment::
use Twig\Extra\Intl\IntlExtension;
$twig = new \Twig\Environment(...);
$twig->addExtension(new IntlExtension());
Arguments
---------
* ``currency``: The currency (ISO 4217 code)
* ``attrs``: A map of attributes
* ``locale``: The locale code as defined in `RFC 5646`_
.. note::
Internally, Twig uses the PHP `NumberFormatter::formatCurrency`_ function.
.. _RFC 5646: https://www.rfc-editor.org/info/rfc5646
.. _`NumberFormatter::formatCurrency`: https://www.php.net/manual/en/numberformatter.formatcurrency.php
================================================
FILE: doc/filters/format_date.rst
================================================
``format_date``
===============
The ``format_date`` filter formats a date. It behaves in the exact same way as
the :doc:`format_datetime` filter, but without the time.
.. note::
The ``format_date`` filter is part of the ``IntlExtension`` which is not
installed by default. Install it first:
.. code-block:: bash
$ composer require twig/intl-extra
Then, on Symfony projects, install the ``twig/extra-bundle``:
.. code-block:: bash
$ composer require twig/extra-bundle
Otherwise, add the extension explicitly on the Twig environment::
use Twig\Extra\Intl\IntlExtension;
$twig = new \Twig\Environment(...);
$twig->addExtension(new IntlExtension());
Arguments
---------
* ``locale``: The locale code as defined in `RFC 5646`_
* ``dateFormat``: The date format
* ``pattern``: A date time pattern
* ``timezone``: The date timezone
* ``calendar``: The calendar ("gregorian" by default)
.. _RFC 5646: https://www.rfc-editor.org/info/rfc5646
================================================
FILE: doc/filters/format_datetime.rst
================================================
``format_datetime``
===================
The ``format_datetime`` filter formats a date time:
.. code-block:: twig
{# Aug 7, 2019, 11:39:12 PM #}
{{ '2019-08-07 23:39:12'|format_datetime() }}
.. note::
The ``format_datetime`` filter is part of the ``IntlExtension`` which is not
installed by default. Install it first:
.. code-block:: bash
$ composer require twig/intl-extra
Then, on Symfony projects, install the ``twig/extra-bundle``:
.. code-block:: bash
$ composer require twig/extra-bundle
Otherwise, add the extension explicitly on the Twig environment::
use Twig\Extra\Intl\IntlExtension;
$twig = new \Twig\Environment(...);
$twig->addExtension(new IntlExtension());
Format
------
You can tweak the output for the date part and the time part:
.. code-block:: twig
{# 23:39 #}
{{ '2019-08-07 23:39:12'|format_datetime('none', 'short', locale: 'fr') }}
{# 07/08/2019 #}
{{ '2019-08-07 23:39:12'|format_datetime('short', 'none', locale: 'fr') }}
{# mercredi 7 août 2019 23:39:12 UTC #}
{{ '2019-08-07 23:39:12'|format_datetime('full', 'full', locale: 'fr') }}
Supported values are: ``none``, ``short``, ``medium``, ``long``, and ``full``.
.. versionadded:: 3.6
``relative_short``, ``relative_medium``, ``relative_long``, and ``relative_full`` are also supported when running on
PHP 8.0 and superior or when using a polyfill that define the ``IntlDateFormatter::RELATIVE_*`` constants and
associated behavior.
For greater flexibility, you can even define your own pattern
(see the `ICU user guide`_ for supported patterns).
.. code-block:: twig
{# 11 oclock PM, GMT #}
{{ '2019-08-07 23:39:12'|format_datetime(pattern: "hh 'oclock' a, zzzz") }}
Locale
------
By default, the filter uses the current locale. You can pass it explicitly:
.. code-block:: twig
{# 7 août 2019 23:39:12 #}
{{ '2019-08-07 23:39:12'|format_datetime(locale: 'fr') }}
Timezone
--------
By default, the date is displayed by applying the default timezone (the one
specified in php.ini or declared in Twig -- see below), but you can override
it by explicitly specifying a timezone:
.. code-block:: twig
{{ datetime|format_datetime(locale: 'en', timezone: 'Pacific/Midway') }}
If the date is already a DateTime object, and if you want to keep its current
timezone, pass ``false`` as the timezone value:
.. code-block:: twig
{{ datetime|format_datetime(locale: 'en', timezone: false) }}
The default timezone can also be set globally by calling ``setTimezone()``::
$twig = new \Twig\Environment($loader);
$twig->getExtension(\Twig\Extension\CoreExtension::class)->setTimezone('Europe/Paris');
.. note::
The ``format_datetime`` filter is part of the ``IntlExtension`` which is not
installed by default. Install it first:
.. code-block:: bash
$ composer require twig/intl-extra
Then, on Symfony projects, install the ``twig/extra-bundle``:
.. code-block:: bash
$ composer require twig/extra-bundle
Otherwise, add the extension explicitly on the Twig environment::
use Twig\Extra\Intl\IntlExtension;
$twig = new \Twig\Environment(...);
$twig->addExtension(new IntlExtension());
Arguments
---------
* ``locale``: The locale code as defined in `RFC 5646`_
* ``dateFormat``: The date format
* ``timeFormat``: The time format
* ``pattern``: A date time pattern
* ``timezone``: The date timezone name
* ``calendar``: The calendar ("gregorian" by default)
.. _ICU user guide: https://unicode-org.github.io/icu/userguide/format_parse/datetime/#datetime-format-syntax
.. _RFC 5646: https://www.rfc-editor.org/info/rfc5646
================================================
FILE: doc/filters/format_number.rst
================================================
``format_number``
=================
The ``format_number`` filter formats a number:
.. code-block:: twig
{{ '12.345'|format_number }}
You can pass attributes to tweak the output:
.. code-block:: twig
{# 12.34 #}
{{ '12.345'|format_number({rounding_mode: 'floor'}) }}
{# 1000000.0000 #}
{{ '1000000'|format_number({fraction_digit: 4}) }}
The list of supported options:
* ``grouping_used``: Specifies whether to use grouping separator for thousands::
{# 1,234,567.89 #}
{{ 1234567.89|format_number({grouping_used:true}, locale: 'en') }}
* ``decimal_always_shown``: Specifies whether to always show the decimal part, even if it's zero::
{# 123. #}
{{ 123|format_number({decimal_always_shown:true}, locale: 'en') }}
* ``max_integer_digit``:
* ``min_integer_digit``:
* ``integer_digit``: Define constraints on the integer part::
{# 345.679 #}
{{ 12345.6789|format_number({max_integer_digit:3, min_integer_digit:2}, locale: 'en') }}
* ``max_fraction_digit``:
* ``min_fraction_digit``:
* ``fraction_digit``: Define constraints on the fraction part::
{# 123.46 #}
{{ 123.456789|format_number({max_fraction_digit:2, min_fraction_digit:1}, locale: 'en') }}
* ``multiplier``: Multiplies the value before formatting::
{# 123,000 #}
{{ 123|format_number({multiplier:1000}, locale: 'en') }}
* ``grouping_size``:
* ``secondary_grouping_size``: Set the size of the primary and secondary grouping separators::
{# 1,23,45,678 #}
{{ 12345678|format_number({grouping_size:3, secondary_grouping_size:2}, locale: 'en') }}
* ``rounding_mode``:
* ``rounding_increment``: Control rounding behavior, here is a list of all rounding_mode available:
* ``ceil``: Ceiling rounding
* ``floor``: Floor rounding
* ``down``: Rounding towards zero
* ``up``: Rounding away from zero
* ``halfeven``: Round halves to the nearest even integer
* ``halfup``: Round halves up
* ``halfdown``: Round halves down
.. code-block:: twig
{# 123.5 #}
{{ 123.456|format_number({rounding_mode:'ceiling', rounding_increment:0.05}, locale: 'en') }}
* ``format_width``:
* ``padding_position``: Set width and padding for the formatted number, here is a list of all padding_position available:
* ``before_prefix``: Pad before the currency symbol
* ``after_prefix``: Pad after the currency symbol
* ``before_suffix``: Pad before the suffix (currency symbol)
* ``after_suffix``: Pad after the suffix (currency symbol)
.. code-block:: twig
{# 123 #}
{{ 123|format_number({format_width:10, padding_position:'before_suffix'}, locale: 'en') }}
* ``significant_digits_used``:
* ``min_significant_digits_used``:
* ``max_significant_digits_used``: Control significant digits in formatting::
{# 123.4568 #}
{{ 123.456789|format_number({significant_digits_used:true, min_significant_digits_used:4, max_significant_digits_used:7}, locale: 'en') }}
* ``lenient_parse``: If true, allows lenient parsing of the input::
{# 123 #}
{{ 123|format_number({lenient_parse:true}, locale: 'en') }}
Besides plain numbers, the filter can also format numbers in various styles::
{# 1,234% #}
{{ '12.345'|format_number(style: 'percent') }}
{# twelve point three four five #}
{{ '12.345'|format_number(style: 'spellout') }}
{# 12 sec. #}
{{ '12'|format_duration_number }}
The list of supported styles:
* ``decimal``::
{# 1,234.568 #}
{{ 1234.56789 | format_number(style: 'decimal', locale: 'en') }}
* ``currency``::
{# $1,234.56 #}
{{ 1234.56 | format_number(style: 'currency', locale: 'en') }}
* ``percent``::
{# 12% #}
{{ 0.1234 | format_number(style: 'percent', locale: 'en') }}
* ``scientific``::
{# 1.23456789e+3 #}
{{ 1234.56789 | format_number(style: 'scientific', locale: 'en') }}
* ``spellout``::
{# one thousand two hundred thirty-four point five six seven eight nine #}
{{ 1234.56789 | format_number(style: 'spellout', locale: 'en') }}
* ``ordinal``::
{# 1st #}
{{ 1 | format_number(style: 'ordinal', locale: 'en') }}
* ``duration``::
{# 2:30:00 #}
{{ 9000 | format_number(style: 'duration', locale: 'en') }}
As a shortcut, you can use the ``format_*_number`` filters by replacing ``*``
with a style::
{# 1,234% #}
{{ '12.345'|format_percent_number }}
{# twelve point three four five #}
{{ '12.345'|format_spellout_number }}
You can pass attributes to tweak the output::
{# 12.3% #}
{{ '0.12345'|format_percent_number({rounding_mode: 'floor', fraction_digit: 1}) }}
By default, the filter uses the current locale. You can pass it explicitly::
{# 12,345 #}
{{ '12.345'|format_number(locale: 'fr') }}
.. note::
The ``format_number`` filter is part of the ``IntlExtension`` which is not
installed by default. Install it first:
.. code-block:: sh
$ composer require twig/intl-extra
Then, on Symfony projects, install the ``twig/extra-bundle``:
.. code-block:: sh
$ composer require twig/extra-bundle
Otherwise, add the extension explicitly on the Twig environment::
use Twig\Extra\Intl\IntlExtension;
$twig = new \Twig\Environment(...);
$twig->addExtension(new IntlExtension());
Arguments
---------
* ``locale``: The locale code as defined in `RFC 5646`_
* ``attrs``: A map of attributes
* ``style``: The style of the number output
.. _RFC 5646: https://www.rfc-editor.org/info/rfc5646
================================================
FILE: doc/filters/format_time.rst
================================================
``format_time``
===============
The ``format_time`` filter formats a time. It behaves in the exact same way as
the :doc:`format_datetime` filter, but without the date.
.. note::
The ``format_time`` filter is part of the ``IntlExtension`` which is not
installed by default. Install it first:
.. code-block:: bash
$ composer require twig/intl-extra
Then, on Symfony projects, install the ``twig/extra-bundle``:
.. code-block:: bash
$ composer require twig/extra-bundle
Otherwise, add the extension explicitly on the Twig environment::
use Twig\Extra\Intl\IntlExtension;
$twig = new \Twig\Environment(...);
$twig->addExtension(new IntlExtension());
Arguments
---------
* ``locale``: The locale code as defined in `RFC 5646`_
* ``timeFormat``: The time format
* ``pattern``: A date time pattern
* ``timezone``: The date timezone
* ``calendar``: The calendar ("gregorian" by default)
.. _RFC 5646: https://www.rfc-editor.org/info/rfc5646
================================================
FILE: doc/filters/html_attr_merge.rst
================================================
``html_attr_merge``
===================
.. _html_attr_merge:
.. versionadded:: 3.24
The ``html_attr_merge`` filter was added in Twig 3.24.
The ``html_attr_merge`` filter merges multiple mappings that represent
HTML attribute values. Such mappings contain the names of the HTML attributes
as keys, and the corresponding values represent the attributes' values.
It is primarily designed for working with arrays that are passed to the
:ref:`html_attr` function. It closely resembles the :doc:`merge <../filters/merge>`
filter, but has different merge behavior for values that are iterables
themselves, as it will merge such values in turn.
The filter returns a new merged array:
.. code-block:: twig
{% set base = {class: ['btn'], type: 'button'} %}
{% set variant = {class: ['btn-primary'], disabled: true} %}
{% set merged = base|html_attr_merge(variant) %}
{# merged is now: {
class: ['btn', 'btn-primary'],
type: 'button',
disabled: true
} #}
The filter accepts multiple arrays as arguments and merges them from left to right:
.. code-block:: twig
{% set merged = base|html_attr_merge(variant1, variant2, variant3) %}
A common use case is to build attribute mappings conditionally by merging multiple
parts based on conditions. To make this conditional merging more convenient, filter
arguments that are ``false``, ``null`` or empty arrays are ignored:
.. code-block:: twig
{% set button_attrs = {
type: 'button',
class: ['btn']
}|html_attr_merge(
variant == 'primary' ? { class: ['btn-primary'] },
variant == 'secondary' ? { class: ['btn-secondary'] },
size == 'large' ? { class: ['btn-lg'] },
size == 'small' ? { class: ['btn-sm'] },
disabled ? { disabled: true, class: ['btn-disabled'] },
loading ? { 'aria-busy': 'true', class: ['btn-loading'] },
) %}
{# Example with variant='primary', size='large', disabled=false, loading=true:
The false values (secondary variant, small size, disabled state) are ignored.
button_attrs is:
{
type: 'button',
class: ['btn', 'btn-primary', 'btn-lg', 'btn-loading'],
'aria-busy': 'true'
}
#}
Merging Rules
-------------
The filter follows these rules when merging attribute values:
**Scalar values**: Later values override earlier ones.
.. code-block:: twig
{% set result = {id: 'old'}|html_attr_merge({id: 'new'}) %}
{# result: {id: 'new'} #}
**Array values**: Arrays are merged like in PHP's ``array_merge`` function - numeric keys are
appended, non-numeric keys replace.
.. code-block:: twig
{# Numeric keys (appended): #}
{% set result = {class: ['btn']}|html_attr_merge({class: ['btn-primary']}) %}
{# result: {class: ['btn', 'btn-primary']} #}
{# Non-numeric keys (replaced): #}
{% set result = {class: {base: 'btn', size: 'small'}}|html_attr_merge({class: {variant: 'primary', size: 'large'}}) %}
{# result: {class: {base: 'btn', size: 'large', variant: 'primary'}} #}
.. note::
Remember, attribute mappings passed to or returned from this filter are regular
Twig mappings after all. If you want to completely replace an attribute value
that is an iterable with another value, you can use the :doc:`merge <../filters/merge>`
filter to do that.
**``MergeableInterface`` implementations**: For advanced use cases, attribute values can be objects
that implement the ``MergeableInterface``. These objects can define their own, custom merge
behavior that takes precedence over the default rules. See the docblocks in that interface
for details.
.. note::
The ``html_attr_merge`` filter is part of the ``HtmlExtension`` which is not
installed by default. Install it first:
.. code-block:: bash
$ composer require twig/html-extra
Then, on Symfony projects, install the ``twig/extra-bundle``:
.. code-block:: bash
$ composer require twig/extra-bundle
Otherwise, add the extension explicitly on the Twig environment::
use Twig\Extra\Html\HtmlExtension;
$twig = new \Twig\Environment(...);
$twig->addExtension(new HtmlExtension());
Arguments
---------
The filter accepts a variadic list of arguments to merge. Each argument can be:
* A map of attributes
* ``false`` or ``null`` (ignored, useful for conditional merging)
* An empty string ``''`` (ignored, to support implicit else in ternary operators)
.. seealso::
:ref:`html_attr`,
:doc:`html_attr_type`
================================================
FILE: doc/filters/html_attr_type.rst
================================================
``html_attr_type``
==================
.. _html_attr_type:
.. versionadded:: 3.24
The ``html_attr_type`` filter was added in Twig 3.24.
The ``html_attr_type`` filter converts arrays into specialized attribute value
objects that implement custom rendering logic. It is designed for use
with the :ref:`html_attr` function for attributes where
the attribute value follows special formatting rules.
.. code-block:: html+twig
{# Output: #}
Available Types
---------------
Space-Separated Token List (``sst``)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Used for attributes that expect space-separated values, like ``class`` or
``aria-labelledby``:
.. code-block:: html+twig
{% set classes = ['btn', 'btn-primary']|html_attr_type('sst') %}
{# Output: #}
This is the default type used when the :ref:`html_attr` function encounters an
array value (except for ``style`` attributes).
Comma-Separated Token List (``cst``)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Used for attributes that expect comma-separated values, like ``srcset`` or
``sizes``:
.. code-block:: html+twig
{# Output: #}
Inline Style (``style``)
~~~~~~~~~~~~~~~~~~~~~~~~
Used for style attributes. Handles both maps (property - value pairs) and sequences (CSS declarations):
.. code-block:: html+twig
{# Associative array #}
{% set styles = {color: 'red', 'font-size': '14px'}|html_attr_type('style') %}
#}
The ``style`` type is automatically applied by the :ref:`html_attr` function when
it encounters an array value for the ``style`` attribute.
.. note::
The ``html_attr_type`` filter is part of the ``HtmlExtension`` which is not
installed by default. Install it first:
.. code-block:: bash
$ composer require twig/html-extra
Then, on Symfony projects, install the ``twig/extra-bundle``:
.. code-block:: bash
$ composer require twig/extra-bundle
Otherwise, add the extension explicitly on the Twig environment::
use Twig\Extra\Html\HtmlExtension;
$twig = new \Twig\Environment(...);
$twig->addExtension(new HtmlExtension());
Arguments
---------
* ``value``: The sequence of attributes to convert
* ``type``: The attribute type. One of:
* ``sst`` (default): Space-separated token list
* ``cst``: Comma-separated token list
* ``style``: Inline CSS styles
.. seealso::
:ref:`html_attr`,
:ref:`html_attr_merge`
================================================
FILE: doc/filters/html_to_markdown.rst
================================================
``html_to_markdown``
====================
The ``html_to_markdown`` filter converts a block of HTML to Markdown:
.. code-block:: html+twig
{% apply html_to_markdown %}
Hello!
{% endapply %}
You can also use the filter on an entire template which you ``include``:
.. code-block:: twig
{{ include('some_template.html.twig')|html_to_markdown }}
.. note::
The ``html_to_markdown`` filter is part of the ``MarkdownExtension`` which
is not installed by default. Install it first:
.. code-block:: bash
$ composer require twig/markdown-extra
On Symfony projects, you can automatically enable it by installing the
``twig/extra-bundle``:
.. code-block:: bash
$ composer require twig/extra-bundle
Or add the extension explicitly on the Twig environment::
use Twig\Extra\Markdown\MarkdownExtension;
$twig = new \Twig\Environment(...);
$twig->addExtension(new MarkdownExtension());
If you are not using Symfony, you must also register the extension runtime::
use Twig\Extra\Markdown\DefaultMarkdown;
use Twig\Extra\Markdown\MarkdownRuntime;
use Twig\RuntimeLoader\RuntimeLoaderInterface;
$twig->addRuntimeLoader(new class implements RuntimeLoaderInterface {
public function load($class) {
if (MarkdownRuntime::class === $class) {
return new MarkdownRuntime(new DefaultMarkdown());
}
}
});
``html_to_markdown`` is just a frontend; the actual conversion is done by one of
the following compatible libraries, from which you can choose:
* `league/html-to-markdown`_
* `michelf/php-markdown`_
* `erusev/parsedown`_
Depending on the library, you can also add some options by passing them as an argument
to the filter. Example for ``league/html-to-markdown``:
.. code-block:: html+twig
{% apply html_to_markdown({hard_break: false}) %}
Hello!
{% endapply %}
.. _league/html-to-markdown: https://github.com/thephpleague/html-to-markdown
.. _michelf/php-markdown: https://github.com/michelf/php-markdown
.. _erusev/parsedown: https://github.com/erusev/parsedown
================================================
FILE: doc/filters/index.rst
================================================
Filters
=======
.. toctree::
:maxdepth: 1
abs
batch
capitalize
column
convert_encoding
country_name
currency_name
currency_symbol
data_uri
date
date_modify
default
escape
filter
find
first
format
format_currency
format_date
format_datetime
format_number
format_time
html_attr_merge
html_attr_type
html_to_markdown
inline_css
inky_to_html
invoke
join
json_encode
keys
language_name
last
length
locale_name
lower
map
markdown_to_html
merge
nl2br
number_format
plural
raw
reduce
replace
reverse
round
shuffle
singular
slice
slug
sort
spaceless
split
striptags
timezone_name
title
trim
u
upper
url_encode
================================================
FILE: doc/filters/inky_to_html.rst
================================================
``inky_to_html``
================
The ``inky_to_html`` filter processes an `inky email template
`_:
.. code-block:: html+twig
{% apply inky_to_html %}
{% endapply %}
You can also use the filter on an included file:
.. code-block:: twig
{{ include('some_template.inky.twig')|inky_to_html }}
.. note::
The ``inky_to_html`` filter is part of the ``InkyExtension`` which is not
installed by default. Install it first:
.. code-block:: bash
$ composer require twig/inky-extra
Then, on Symfony projects, install the ``twig/extra-bundle``:
.. code-block:: bash
$ composer require twig/extra-bundle
Otherwise, add the extension explicitly on the Twig environment::
use Twig\Extra\Inky\InkyExtension;
$twig = new \Twig\Environment(...);
$twig->addExtension(new InkyExtension());
================================================
FILE: doc/filters/inline_css.rst
================================================
``inline_css``
==============
The ``inline_css`` filter inlines CSS styles in HTML documents:
.. code-block:: html+twig
{% apply inline_css %}
Hello CSS!
{% endapply %}
You can also add some stylesheets by passing them as arguments to the filter:
.. code-block:: html+twig
{% apply inline_css(source("some_styles.css"), source("another.css")) %}
Hello CSS!
{% endapply %}
Styles loaded via the filter override the styles defined in the ``
{% endblock %}
{% block content %}
Index
Welcome on my awesome homepage.
{% endblock %}
The ``extends`` tag is the key here. It tells the template engine that this
template "extends" another template. When the template system evaluates this
template, first it locates the parent. The extends tag should be the first tag
in the template.
Note that since the child template doesn't define the ``footer`` block, the
value from the parent template is used instead.
You can't define multiple ``block`` tags with the same name in the same
template. This limitation exists because a block tag works in "both"
directions. That is, a block tag doesn't just provide a hole to fill - it also
defines the content that fills the hole in the *parent*. If there were two
similarly-named ``block`` tags in a template, that template's parent wouldn't
know which one of the blocks' content to use.
If you want to print a block multiple times you can however use the
``block`` function:
.. code-block:: html+twig
{% block title %}{% endblock %}
{{ block('title') }}
{% block body %}{% endblock %}
Parent Blocks
-------------
It's possible to render the contents of the parent block by using the
:doc:`parent<../functions/parent>` function. This gives back the results of
the parent block:
.. code-block:: html+twig
{% block sidebar %}
Table Of Contents
...
{{ parent() }}
{% endblock %}
Named Block End-Tags
--------------------
Twig allows you to put the name of the block after the end tag for better
readability (the name after the ``endblock`` word must match the block name):
.. code-block:: twig
{% block sidebar %}
{% block inner_sidebar %}
...
{% endblock inner_sidebar %}
{% endblock sidebar %}
Block Nesting and Scope
-----------------------
Blocks can be nested for more complex layouts. Per default, blocks have access
to variables from outer scopes:
.. code-block:: html+twig
{% for item in seq %}
{% block loop_item %}{{ item }}{% endblock %}
{% endfor %}
Block Shortcuts
---------------
For blocks with little content, it's possible to use a shortcut syntax. The
following constructs do the same thing:
.. code-block:: twig
{% block title %}
{{ page_title|title }}
{% endblock %}
.. code-block:: twig
{% block title page_title|title %}
Dynamic Inheritance
-------------------
Twig supports dynamic inheritance by using a variable as the base template:
.. code-block:: twig
{% extends some_var %}
If the variable evaluates to a ``\Twig\Template`` or a ``\Twig\TemplateWrapper``
instance, Twig will use it as the parent template::
// {% extends layout %}
$layout = $twig->load('some_layout_template.html.twig');
$twig->display('template.html.twig', ['layout' => $layout]);
You can also provide a list of templates that are checked for existence. The
first template that exists will be used as a parent:
.. code-block:: twig
{% extends ['layout.html.twig', 'base_layout.html.twig'] %}
Conditional Inheritance
-----------------------
As the template name for the parent can be any valid Twig expression, it's
possible to make the inheritance mechanism conditional:
.. code-block:: twig
{% extends standalone ? "minimum.html.twig" : "base.html.twig" %}
In this example, the template will extend the "minimum.html.twig" layout template
if the ``standalone`` variable evaluates to ``true``, and "base.html.twig"
otherwise.
How do blocks work?
-------------------
A block provides a way to change how a certain part of a template is rendered
but it does not interfere in any way with the logic around it.
Let's take the following example to illustrate how a block works and more
importantly, how it does not work:
.. code-block:: html+twig
{# base.html.twig #}
{% for post in posts %}
{% block post %}
{{ post.title }}
{{ post.body }}
{% endblock %}
{% endfor %}
If you render this template, the result would be exactly the same with or
without the ``block`` tag. The ``block`` inside the ``for`` loop is just a way
to make it overridable by a child template:
.. code-block:: html+twig
{# child.html.twig #}
{% extends "base.html.twig" %}
{% block post %}
{{ post.title }}{{ post.text }}
{% endblock %}
Now, when rendering the child template, the loop is going to use the block
defined in the child template instead of the one defined in the base one; the
executed template is then equivalent to the following one:
.. code-block:: html+twig
{% for post in posts %}
{{ post.title }}{{ post.text }}
{% endfor %}
Let's take another example: a block included within an ``if`` statement:
.. code-block:: html+twig
{% if posts is empty %}
{% block head %}
{{ parent() }}
{% endblock head %}
{% endif %}
Contrary to what you might think, this template does not define a block
conditionally; it just makes overridable by a child template the output of
what will be rendered when the condition is ``true``.
If you want the output to be displayed conditionally, use the following
instead:
.. code-block:: html+twig
{% block head %}
{{ parent() }}
{% if posts is empty %}
{% endif %}
{% endblock head %}
.. seealso::
:doc:`block<../functions/block>`, :doc:`block<../tags/block>`, :doc:`parent<../functions/parent>`, :doc:`use<../tags/use>`
================================================
FILE: doc/tags/flush.rst
================================================
``flush``
=========
The ``flush`` tag tells Twig to flush the output buffer:
.. code-block:: twig
{% flush %}
.. note::
Internally, Twig uses the PHP `flush`_ function.
.. _`flush`: https://www.php.net/flush
================================================
FILE: doc/tags/for.rst
================================================
``for``
=======
Loop over each item in a sequence or a mapping. For example, to display a list
of users provided in a variable called ``users``:
.. code-block:: html+twig
Members
{% for user in users %}
{{ user.username|e }}
{% endfor %}
.. note::
A sequence or a mapping can be either an array or an object implementing
the ``Traversable`` interface.
If you do need to iterate over a sequence of numbers, you can use the ``..``
operator:
.. code-block:: twig
{% for i in 0..10 %}
* {{ i }}
{% endfor %}
The above snippet of code would print all numbers from 0 to 10.
It can be also useful with letters:
.. code-block:: twig
{% for letter in 'a'..'z' %}
* {{ letter }}
{% endfor %}
The ``..`` operator can take any expression at both sides:
.. code-block:: twig
{% for letter in 'a'|upper..'z'|upper %}
* {{ letter }}
{% endfor %}
.. tip::
If you need a step different from 1, you can use the ``range`` function
instead.
The ``loop`` variable
---------------------
Inside of a ``for`` loop block you can access some special variables:
===================== =============================================================
Variable Description
===================== =============================================================
``loop.index`` The current iteration of the loop. (1 indexed)
``loop.index0`` The current iteration of the loop. (0 indexed)
``loop.revindex`` The number of iterations from the end of the loop (1 indexed)
``loop.revindex0`` The number of iterations from the end of the loop (0 indexed)
``loop.first`` True if first iteration
``loop.last`` True if last iteration
``loop.length`` The number of items in the sequence
``loop.parent`` The parent context
===================== =============================================================
.. code-block:: twig
{% for user in users %}
{{ loop.index }} - {{ user.username }}
{% endfor %}
.. note::
The ``loop.length``, ``loop.revindex``, ``loop.revindex0``, and
``loop.last`` variables are only available for PHP arrays, or objects that
implement the ``Countable`` interface.
The ``else`` Clause
-------------------
If no iteration took place because the sequence was empty, you can render a
replacement block by using ``else``:
.. code-block:: html+twig
{% for user in users %}
{{ user.username|e }}
{% else %}
no user found
{% endfor %}
Iterating over Keys
-------------------
By default, a loop iterates over the values of the sequence. You can iterate
on keys by using the ``keys`` filter:
.. code-block:: html+twig
Members
{% for key in users|keys %}
{{ key }}
{% endfor %}
Iterating over Keys and Values
------------------------------
You can also access both keys and values:
.. code-block:: html+twig
Members
{% for key, user in users %}
{{ key }}: {{ user.username|e }}
{% endfor %}
Iterating over a Subset
-----------------------
You might want to iterate over a subset of values. This can be achieved using
the :doc:`slice <../filters/slice>` filter:
.. code-block:: html+twig
Top Ten Members
{% for user in users|slice(0, 10) %}
{{ user.username|e }}
{% endfor %}
Iterating over a String
-----------------------
To iterate over the characters of a string, use the
:doc:`split <../filters/split>` filter:
.. code-block:: html+twig
Characters
{% for char in "諺 / ことわざ"|split('') -%}
{{ char }}
{%- endfor %}
================================================
FILE: doc/tags/from.rst
================================================
``from``
========
The ``from`` tag imports :doc:`macro<../tags/macro>` names into the current
namespace. The tag is documented in detail in the documentation for the
:doc:`macro<../tags/macro>` tag.
================================================
FILE: doc/tags/guard.rst
================================================
``guard``
=========
.. versionadded:: 3.15
The ``guard`` tag was added in Twig 3.15.
The ``guard`` statement checks if some Twig callables are available at
**compilation time** to bypass code compilation that would otherwise fail.
.. code-block:: twig
{% guard function importmap %}
{{ importmap('app') }}
{% endguard %}
The first argument is the Twig callable to test: ``filter``, ``function``, or
``test``. The second argument is the Twig callable name you want to test.
You can also generate different code if the callable does not exist:
.. code-block:: twig
{% guard function importmap %}
{{ importmap('app') }}
{% else %}
{# the importmap function doesn't exist, generate fallback code #}
{% endguard %}
================================================
FILE: doc/tags/if.rst
================================================
``if``
======
The ``if`` statement in Twig is comparable with the if statements of PHP.
In the simplest form you can use it to test if an expression evaluates to
``true``:
.. code-block:: html+twig
{% if online == false %}
Our website is in maintenance mode. Please, come back later.
{% endif %}
You can also test if a sequence or a mapping is not empty:
.. code-block:: html+twig
{% if users %}
{% for user in users %}
{{ user.username|e }}
{% endfor %}
{% endif %}
.. note::
If you want to test if the variable is defined, use ``if users is defined`` instead.
You can also use ``not`` to check for values that evaluate to ``false``:
.. code-block:: html+twig
{% if not user.subscribed %}
You are not subscribed to our mailing list.
{% endif %}
For multiple conditions, ``and`` and ``or`` can be used:
.. code-block:: html+twig
{% if temperature > 18 and temperature < 27 %}
It's a nice day for a walk in the park.
{% endif %}
For multiple branches ``elseif`` and ``else`` can be used like in PHP. You can
use more complex ``expressions`` there too:
.. code-block:: twig
{% if product.stock > 10 %}
Available
{% elseif product.stock > 0 %}
Only {{ product.stock }} left!
{% else %}
Sold-out!
{% endif %}
.. note::
The rules to determine if an expression is ``true`` or ``false`` are the
same as in PHP; here are the edge cases rules:
====================== ====================
Value Boolean evaluation
====================== ====================
empty string false
numeric zero false
NAN (Not A Number) true
INF (Infinity) true
whitespace-only string true
string "0" or '0' false
empty sequence false
empty mapping false
null false
non-empty sequence true
non-empty mapping true
object true
====================== ====================
================================================
FILE: doc/tags/import.rst
================================================
``import``
==========
The ``import`` tag imports :doc:`macro<../tags/macro>` names in a local
variable. The tag is documented in detail in the documentation for the
:doc:`macro<../tags/macro>` tag.
================================================
FILE: doc/tags/include.rst
================================================
``include``
===========
The ``include`` statement includes a template and outputs the rendered content
of that file:
.. code-block:: twig
{% include 'header.html.twig' %}
Body
{% include 'footer.html.twig' %}
.. note::
It is recommended to use the :doc:`include<../functions/include>` function
instead as it provides the same features with a bit more flexibility:
* The ``include`` function is semantically more "correct" (including a
template outputs its rendered contents in the current scope; a tag should
not display anything);
* The ``include`` function is more "composable":
.. code-block:: twig
{# Store a rendered template in a variable #}
{% set content %}
{% include 'template.html.twig' %}
{% endset %}
{# vs #}
{% set content = include('template.html.twig') %}
{# Apply filter on a rendered template #}
{% apply upper %}
{% include 'template.html.twig' %}
{% endapply %}
{# vs #}
{{ include('template.html.twig')|upper }}
* The ``include`` function does not impose any specific order for
arguments thanks to :ref:`named arguments `.
Included templates have access to the variables of the active context.
If you are using the filesystem loader, the templates are looked for in the
paths defined by it.
You can add additional variables by passing them after the ``with`` keyword:
.. code-block:: twig
{# template.html.twig will have access to the variables from the current context and the additional ones provided #}
{% include 'template.html.twig' with {'name': 'Fabien'} %}
{% set vars = {'name': 'Fabien'} %}
{% include 'template.html.twig' with vars %}
You can disable access to the context by appending the ``only`` keyword:
.. code-block:: twig
{# only the name variable will be accessible #}
{% include 'template.html.twig' with {'name': 'Fabien'} only %}
.. code-block:: twig
{# no variables will be accessible #}
{% include 'template.html.twig' only %}
.. tip::
When including a template created by an end user, you should consider
sandboxing it. More information in the :doc:`Twig Sandbox<../sandbox>`
chapter.
The template name can be any valid Twig expression:
.. code-block:: twig
{% include some_var %}
{% include ajax ? 'ajax.html.twig' : 'not_ajax.html.twig' %}
And if the expression evaluates to a ``\Twig\Template`` or a
``\Twig\TemplateWrapper`` instance, Twig will use it directly::
// {% include template %}
$template = $twig->load('some_template.html.twig');
$twig->display('template.html.twig', ['template' => $template]);
You can mark an include with ``ignore missing`` in which case Twig will ignore
the statement if the template to be included does not exist. It has to be
placed just after the template name. Here some valid examples:
.. code-block:: twig
{% include 'sidebar.html.twig' ignore missing %}
{% include 'sidebar.html.twig' ignore missing with {'name': 'Fabien'} %}
{% include 'sidebar.html.twig' ignore missing only %}
You can also provide a list of templates that are checked for existence before
inclusion. The first template that exists will be included:
.. code-block:: twig
{% include ['page_detailed.html.twig', 'page.html.twig'] %}
If ``ignore missing`` is given, it will fall back to rendering nothing if none
of the templates exist, otherwise it will throw an exception.
================================================
FILE: doc/tags/index.rst
================================================
Tags
====
.. toctree::
:maxdepth: 1
apply
autoescape
block
cache
deprecated
do
embed
extends
flush
for
from
guard
if
import
include
macro
sandbox
set
types
use
verbatim
with
================================================
FILE: doc/tags/macro.rst
================================================
``macro``
=========
Macros are comparable with functions in regular programming languages. They
are useful to reuse template fragments to not repeat yourself.
Macros are defined in regular templates.
Imagine having a generic helper template that define how to render HTML forms
via macros (called ``forms.twig``):
.. code-block:: html+twig
{% macro input(name, value, type = "text", size = 20) %}
{% endmacro %}
{% macro textarea(name, value, rows = 10, cols = 40) %}
{% endmacro %}
Each macro argument can have a default value (here ``text`` is the default value
for ``type`` if not provided in the call).
Macros differ from native PHP functions in a few ways:
* Arguments of a macro are always optional.
* If extra positional arguments are passed to a macro, they end up in the
special ``varargs`` variable as a list of values.
But as with PHP functions, macros don't have access to the current template
variables.
.. tip::
You can pass the whole context as an argument by using the special
``_context`` variable.
Importing Macros
----------------
There are two ways to import macros. You can import the complete template
containing the macros into a local variable (via the ``import`` tag) or only
import specific macros from the template (via the ``from`` tag).
To import all macros from a template into a local variable, use the ``import``
tag:
.. code-block:: twig
{% import "forms.html.twig" as forms %}
The above ``import`` call imports the ``forms.html.twig`` file (which can contain
only macros, or a template and some macros), and import the macros as
attributes of the ``forms`` local variable.
The macros can then be called at will in the *current* template:
.. code-block:: html+twig
Alternatively you can import names from the template into the current namespace
via the ``from`` tag:
.. code-block:: html+twig
{% from 'forms.html.twig' import input as input_field, textarea %}
.. caution::
As macros imported via ``from`` are called like functions, be careful that
they shadow existing functions:
.. code-block:: twig
{% from 'forms.html.twig' import input as include %}
{# include refers to the macro and not to the built-in "include" function #}
{{ include() }}
.. tip::
When macro usages and definitions are in the same template, you don't need to
import the macros as they are automatically available under the special
``_self`` variable:
.. code-block:: html+twig
{{ _self.input('password', '', 'password') }}
{% macro input(name, value, type = "text", size = 20) %}
{% endmacro %}
Macros Scoping
--------------
The scoping rules are the same whether you imported macros via ``import`` or
``from``.
Imported macros are always **local** to the current template. It means that
macros are available in all blocks and other macros defined in the current
template, but they are not available in included templates or child templates;
you need to explicitly re-import macros in each template.
Imported macros are not available in the body of ``embed`` tags, you need
to explicitly re-import macros inside the tag.
When calling ``import`` or ``from`` from a ``block`` tag, the imported macros
are only defined in the current block and they shadow macros defined at the
template level with the same names.
Checking if a Macro is defined
------------------------------
You can check if a macro is defined via the ``defined`` test:
.. code-block:: twig
{% import "macros.html.twig" as macros %}
{% from "macros.html.twig" import hello %}
{% if macros.hello is defined -%}
OK
{% endif %}
{% if hello is defined -%}
OK
{% endif %}
Named Macro End-Tags
--------------------
Twig allows you to put the name of the macro after the end tag for better
readability (the name after the ``endmacro`` word must match the macro name):
.. code-block:: twig
{% macro input() %}
...
{% endmacro input %}
================================================
FILE: doc/tags/sandbox.rst
================================================
``sandbox``
===========
.. warning::
The ``sandbox`` tag is deprecated as of Twig 3.15.
Use the ``sandboxed`` option of the ``include`` function instead.
The ``sandbox`` tag can be used to enable the sandboxing mode for an included
template, when sandboxing is not enabled globally for the Twig environment:
.. code-block:: twig
{% sandbox %}
{% include 'user.html.twig' %}
{% endsandbox %}
.. warning::
The ``sandbox`` tag is only available when the sandbox extension is
enabled (see the :doc:`Twig for Developers<../api>` chapter).
.. note::
The ``sandbox`` tag can only be used to sandbox an include tag and it
cannot be used to sandbox a section of a template. The following example
won't work:
.. code-block:: twig
{% sandbox %}
{% for i in 1..2 %}
{{ i }}
{% endfor %}
{% endsandbox %}
================================================
FILE: doc/tags/set.rst
================================================
``set``
=======
Inside code blocks you can also assign values to variables. Assignments use
the ``set`` tag and can have multiple targets.
Here is how you can assign the ``Fabien`` value to the ``name`` variable:
.. code-block:: twig
{% set name = 'Fabien' %}
After the ``set`` call, the ``name`` variable is available in the template like
any other ones:
.. code-block:: twig
{# displays Fabien #}
{{ name }}
The assigned value can be any valid :ref:`Twig expression
`:
.. code-block:: twig
{% set numbers = [1, 2] %}
{% set user = {'name': 'Fabien'} %}
{% set name = 'Fabien' ~ ' ' ~ 'Potencier' %}
.. tip::
To assign a value within an expression, use the :ref:`= operator
`:
.. code-block:: twig
{# use assignment within a larger expression #}
{{ (result = fetch_data()) ? result : 'default' }}
Several variables can be assigned in one block:
.. code-block:: twig
{% set first, last = 'Fabien', 'Potencier' %}
{# is equivalent to #}
{% set first = 'Fabien' %}
{% set last = 'Potencier' %}
The ``set`` tag can also be used to "capture" chunks of text:
.. code-block:: html+twig
{% set content %}
...
{% endset %}
.. caution::
If you enable automatic output escaping, Twig will only consider the
content to be safe when capturing chunks of text.
.. note::
Note that loops are scoped in Twig; therefore a variable declared inside a
``for`` loop is not accessible outside the loop itself:
.. code-block:: twig
{% for item in items %}
{% set value = item %}
{% endfor %}
{# value is NOT available #}
If you want to access the variable, just declare it before the loop:
.. code-block:: twig
{% set value = "" %}
{% for item in items %}
{% set value = item %}
{% endfor %}
{# value is available #}
================================================
FILE: doc/tags/types.rst
================================================
``types``
=========
.. versionadded:: 3.13
The ``types`` tag was added in Twig 3.13. This tag is **experimental** and
can change based on usage and feedback.
Use the ``types`` tag to declare the type of a variable:
.. code-block:: twig
{% types is_correct: 'boolean' %}
{% types score: 'number' %}
Or multiple variables:
.. code-block:: twig
{% types
is_correct: 'boolean',
score: 'number',
%}
You can also enclose types with ``{}``:
.. code-block:: twig
{% types {
is_correct: 'boolean',
score: 'number',
} %}
Declare optional variables by adding a ``?`` suffix:
.. code-block:: twig
{% types {
is_correct: 'boolean',
score?: 'number',
} %}
By default, this tag does not affect the template compilation or runtime behavior.
Its purpose is to enable designers and developers to document and specify the
context's available and/or required variables. While Twig itself does not
validate variables or their types, this tag enables extensions to do this.
Additionally, :ref:`Twig extensions ` can analyze these
tags to perform compile-time and runtime analysis of templates.
.. note::
The types declared in a template are local to that template and must not be
propagated to included templates. This is because a template can be
included from multiple different places, each potentially having different
variable types.
.. note::
The syntax for and contents of type strings are intentionally left out of
scope.
================================================
FILE: doc/tags/use.rst
================================================
``use``
=======
.. note::
Horizontal reuse is an advanced Twig feature that is hardly ever needed in
regular templates. It is mainly used by projects that need to make
template blocks reusable without using inheritance.
Template inheritance is one of the most powerful features of Twig but it is
limited to single inheritance; a template can only extend one other template.
This limitation makes template inheritance simple to understand and easy to
debug:
.. code-block:: twig
{% extends "base.html.twig" %}
{% block title %}{% endblock %}
{% block content %}{% endblock %}
Horizontal reuse is a way to achieve the same goal as multiple inheritance,
but without the associated complexity:
.. code-block:: twig
{% extends "base.html.twig" %}
{% use "blocks.html.twig" %}
{% block title %}{% endblock %}
{% block content %}{% endblock %}
The ``use`` statement tells Twig to import the blocks defined in
``blocks.html.twig`` into the current template (it's like macros, but for blocks):
.. code-block:: twig
{# blocks.html.twig #}
{% block sidebar %}{% endblock %}
In this example, the ``use`` statement imports the ``sidebar`` block into the
main template. The code is mostly equivalent to the following one (the
imported blocks are not outputted automatically):
.. code-block:: twig
{% extends "base.html.twig" %}
{% block sidebar %}{% endblock %}
{% block title %}{% endblock %}
{% block content %}{% endblock %}
.. note::
The ``use`` tag only imports a template if it does not extend another
template, if it does not define macros, and if the body is empty. But it
can *use* other templates.
.. note::
Because ``use`` statements are resolved independently of the context
passed to the template, the template reference cannot be an expression.
The main template can also override any imported block. If the template
already defines the ``sidebar`` block, then the one defined in ``blocks.html.twig``
is ignored. To avoid name conflicts, you can rename imported blocks:
.. code-block:: twig
{% extends "base.html.twig" %}
{% use "blocks.html.twig" with sidebar as base_sidebar, title as base_title %}
{% block sidebar %}{% endblock %}
{% block title %}{% endblock %}
{% block content %}{% endblock %}
The ``parent()`` function automatically determines the correct inheritance
tree, so it can be used when overriding a block defined in an imported
template:
.. code-block:: twig
{% extends "base.html.twig" %}
{% use "blocks.html.twig" %}
{% block sidebar %}
{{ parent() }}
{% endblock %}
{% block title %}{% endblock %}
{% block content %}{% endblock %}
In this example, ``parent()`` will correctly call the ``sidebar`` block from
the ``blocks.html.twig`` template.
.. tip::
Renaming allows you to simulate inheritance by calling the "parent" block:
.. code-block:: twig
{% extends "base.html.twig" %}
{% use "blocks.html.twig" with sidebar as parent_sidebar %}
{% block sidebar %}
{{ block('parent_sidebar') }}
{% endblock %}
.. note::
You can use as many ``use`` statements as you want in any given template.
If two imported templates define the same block, the latest one wins.
================================================
FILE: doc/tags/verbatim.rst
================================================
``verbatim``
============
The ``verbatim`` tag marks sections as being raw text that should not be
parsed. For example to put Twig syntax as example into a template you can use
this snippet:
.. code-block:: html+twig
{% verbatim %}
{% for item in seq %}
{{ item }}
{% endfor %}
{% endverbatim %}
================================================
FILE: doc/tags/with.rst
================================================
``with``
========
Use the ``with`` tag to create a new inner scope. Variables set within this
scope are not visible outside of the scope:
.. code-block:: twig
{% with %}
{% set value = 42 %}
{{ value }} {# value is 42 here #}
{% endwith %}
value is not visible here any longer
Instead of defining variables at the beginning of the scope, you can pass a
mapping of variables you want to define in the ``with`` tag; the previous
example is equivalent to the following one:
.. code-block:: twig
{% with {value: 42} %}
{{ value }} {# value is 42 here #}
{% endwith %}
value is not visible here any longer
{# it works with any expression that resolves to a mapping #}
{% set vars = {value: 42} %}
{% with vars %}
...
{% endwith %}
By default, the inner scope has access to the outer scope context; you can
disable this behavior by appending the ``only`` keyword:
.. code-block:: twig
{% set zero = 0 %}
{% with {value: 42} only %}
{# only value is defined #}
{# zero is not defined #}
{% endwith %}
================================================
FILE: doc/templates.rst
================================================
Twig for Template Designers
===========================
This document describes the syntax and semantics of the template engine and
will be most useful as reference to those creating Twig templates.
Synopsis
--------
A template is a regular text file. It can generate any text-based format (HTML,
XML, CSV, LaTeX, etc.). It doesn't have a specific extension, ``.html`` or
``.xml`` are just fine.
A template contains **variables** or **expressions**, which get replaced with
values when the template is evaluated, and **tags**, which control the
template's logic.
Below is a minimal template that illustrates a few basics. We will cover further
details later on:
.. code-block:: html+twig
My Webpage
{{ a_variable }}
There are two kinds of delimiters: ``{% ... %}`` and ``{{ ... }}``. The first
one is used to execute statements such as for-loops, the latter outputs the
result of an expression.
.. tip::
To experiment with Twig, you can use the `Twig Playground
`_.
Third-party Integrations
------------------------
Many IDEs support syntax highlighting and auto-completion for Twig:
* *Textmate* via the `Twig bundle`_
* *Vim* via the `vim-twig plugin`_
* *Netbeans* (native as of 7.2)
* *PhpStorm* (native as of 2.1)
* *Eclipse* via the `Twig plugin`_
* *Sublime Text* via the `Twig bundle`_
* *GtkSourceView* via the `Twig language definition`_ (used by gedit and other projects)
* *Coda* and *SubEthaEdit* via the `Twig syntax mode`_
* *Coda 2* via the `other Twig syntax mode`_
* *Komodo* and *Komodo Edit* via the Twig highlight/syntax check mode
* *Notepad++* via the `Notepad++ Twig Highlighter`_
* *Emacs* via `web-mode.el`_
* *Atom* via the `PHP-twig for atom`_
* *Visual Studio Code* via the `Twig pack`_, `Modern Twig`_ or `Twiggy`_
You might also be interested in:
* `Twig CS Fixer`_: a tool to check/fix your templates code style
* `Twig Language Server`_: provides some language features like syntax
highlighting, diagnostics, auto complete, ...
* `TwigQI`_: an extension which analyzes your templates for common bugs during compilation
* `TwigStan`_: a static analyzer for Twig templates powered by PHPStan
Variables
---------
Twig templates have access to variables provided by the PHP application and
variables created in templates via the :doc:`set ` tag. These
variables can be manipulated and displayed in the template.
Twig tries to abstract PHP types as much as possible and works with a few basic
types, supported by ``filters``, ``functions``, and ``tests`` among others:
=================== ===============================
Twig Type PHP Type
=================== ===============================
string A string or a Stringable object
number An integer or a float
boolean ``true`` or ``false``
null ``null``
iterable (mapping) An array
iterable (sequence) An array
iterable (object) An iterable object
object An object
=================== ===============================
The ``iterable`` and ``object`` types expose attributes you can access via the
dot (``.``) operator:
.. code-block:: twig
{{ user.name }}
.. note::
It's important to know that the curly braces are *not* part of the
variable but the print statement. When accessing variables inside tags,
don't put the braces around them.
If a variable or attribute does not exist, the behavior depends on the
``strict_variables`` option value (see :ref:`environment options
`):
* When ``false``, it returns ``null``;
* When ``true``, it throws an exception.
Learn more about the :ref:`dot operator `.
Global Variables
~~~~~~~~~~~~~~~~
The following variables are always available in templates:
* ``_self``: references the current template name;
* ``_context``: references the current context;
* ``_charset``: references the current charset.
Setting Variables
~~~~~~~~~~~~~~~~~
You can assign values to variables inside code blocks using either the
:doc:`set` tag or the :ref:`= operator `:
.. code-block:: twig
{% set name = 'Fabien' %}
{% set numbers = [1, 2] %}
{% set map = {'city': 'Paris'} %}
{% set first, last = 'Fabien', 'Potencier' %}
{# or #}
{% do name = 'Fabien' %}
{% do numbers = [1, 2] %}
{% do map = {'city': 'Paris'} %}
{% do [first, last] = ['Fabien', 'Potencier'] %}
The ``set`` tag can also be used to capture template content into
a variable:
.. code-block:: html+twig
{% set content %}
...
{% endset %}
See the :doc:`set` tag documentation for more details.
Filters
-------
Variables and expressions can be modified by **filters**. Filters are separated
from the variable by a pipe symbol (``|``). Multiple filters can be chained.
The output of one filter is applied to the next.
The following example removes all HTML tags from the ``name`` and title-cases
it:
.. code-block:: twig
{{ name|striptags|title }}
Filters that accept arguments have parentheses around the arguments. This
example joins the elements of a list by commas:
.. code-block:: twig
{{ list|join(', ') }}
To apply a filter on a section of code, wrap it with the
:doc:`apply` tag:
.. code-block:: twig
{% apply upper %}
This text becomes uppercase
{% endapply %}
Go to the :doc:`filters` page to learn more about built-in
filters.
.. warning::
As the ``filter`` operator has the highest :ref:`precedence
`, use parentheses when filtering more "complex"
expressions:
.. code-block:: twig
{{ (1..5)|join(', ') }}
{{ ('HELLO' ~ 'FABIEN')|lower }}
Functions
---------
Functions can be called to generate content. Functions are called by their
name followed by parentheses (``()``) and may have arguments.
For instance, the ``range`` function returns a list containing an arithmetic
progression of integers:
.. code-block:: twig
{% for i in range(0, 3) %}
{{ i }},
{% endfor %}
Go to the :doc:`functions` page to learn more about the
built-in functions.
.. _named-arguments:
Named Arguments
---------------
Named arguments are supported everywhere you can pass arguments: functions,
filters, tests, macros, and dot operator arguments.
.. versionadded:: 3.15
Named arguments for macros and dot operator arguments were added in Twig
3.15.
.. versionadded:: 3.12
Twig supports both ``=`` and ``:`` as separators between argument names and
values, but support for ``:`` was introduced in Twig 3.12.
.. code-block:: twig
{% for i in range(low: 1, high: 10, step: 2) %}
{{ i }},
{% endfor %}
Using named arguments makes your templates more explicit about the meaning of
the values you pass as arguments:
.. code-block:: twig
{{ data|convert_encoding('UTF-8', 'iso-2022-jp') }}
{# versus #}
{{ data|convert_encoding(from: 'iso-2022-jp', to: 'UTF-8') }}
Named arguments also allow you to skip some arguments for which you don't want
to change the default value:
.. code-block:: twig
{# the first argument is the date format, which defaults to the global date format if null is passed #}
{{ "now"|date(null, "Europe/Paris") }}
{# or skip the format value by using a named argument for the time zone #}
{{ "now"|date(timezone: "Europe/Paris") }}
You can also use both positional and named arguments in one call, in which
case positional arguments must always come before named arguments:
.. code-block:: twig
{{ "now"|date('d/m/Y H:i', timezone: "Europe/Paris") }}
.. tip::
Each function, filter, and test documentation page has a section where the
names of all supported arguments are listed.
Control Structure
-----------------
A control structure refers to all those things that control the flow of a
program - conditionals (i.e. ``if``/``elseif``/``else``), ``for``-loops, as
well as things like blocks. Control structures appear inside ``{% ... %}``
blocks.
For example, to display a list of users provided in a variable called
``users``, use the :doc:`for` tag:
.. code-block:: html+twig
Members
{% for user in users %}
{{ user.username|e }}
{% endfor %}
The :doc:`if` tag can be used to test an expression:
.. code-block:: html+twig
{% if users|length > 0 %}
{% for user in users %}
{{ user.username|e }}
{% endfor %}
{% endif %}
Go to the :doc:`tags` page to learn more about the built-in tags.
Comments
--------
To comment-out part of a template, use the comment syntax ``{# ... #}``. This
is useful for debugging or to add information for other template designers or
yourself:
.. code-block:: twig
{# note: disabled template because we no longer use this
{% for user in users %}
...
{% endfor %}
#}
.. versionadded:: 3.15
Inline comments were added in Twig 3.15.
If you want to add comments inside a block, variable, or comment, use an inline
comment. They start with ``#`` and continue to the end of the line:
.. code-block:: twig
{{
# this is an inline comment
"Hello World"|upper
# this is an inline comment
}}
{{
{
# this is an inline comment
fruit: 'apple', # this is an inline comment
color: 'red', # this is an inline comment
}|join(', ')
}}
Inline comments can also be on the same line as the expression:
.. code-block:: twig
{{
"Hello World"|upper # this is an inline comment
}}
As inline comments continue until the end of the current line, the following
code does not work as ``}}`` would be part of the comment:
.. code-block:: twig
{{ "Hello World"|upper # this is an inline comment }}
Including other Templates
-------------------------
The :doc:`include` function is useful to include a template
and return the rendered content of that template into the current one:
.. code-block:: twig
{{ include('sidebar.html.twig') }}
By default, included templates have access to the same context as the template
which includes them. This means that any variable defined in the main template
will be available in the included template too:
.. code-block:: twig
{% for box in boxes %}
{{ include('render_box.html.twig') }}
{% endfor %}
The included template ``render_box.html.twig`` is able to access the ``box`` variable.
The name of the template depends on the template loader. For instance, the
``\Twig\Loader\FilesystemLoader`` allows you to access other templates by giving the
filename. You can access templates in subdirectories with a slash:
.. code-block:: twig
{{ include('sections/articles/sidebar.html.twig') }}
This behavior depends on the application embedding Twig.
Template Inheritance
--------------------
The most powerful part of Twig is template inheritance. Template inheritance
allows you to build a base "skeleton" template that contains all the common
elements of your site and defines **blocks** that child templates can
override.
It's easier to understand the concept by starting with an example.
Let's define a base template, ``base.html.twig``, which defines an HTML skeleton
document that might be used for a two-column page:
.. code-block:: html+twig
{% block head %}
{% block title %}{% endblock %} - My Webpage
{% endblock %}
{% block content %}{% endblock %}
In this example, the :doc:`block` tags define four blocks that
child templates can fill in. All the ``block`` tag does is to tell the
template engine that a child template may override those portions of the
template.
A child template might look like this:
.. code-block:: html+twig
{% extends "base.html.twig" %}
{% block title %}Index{% endblock %}
{% block head %}
{{ parent() }}
{% endblock %}
{% block content %}
Index
Welcome to my awesome homepage.
{% endblock %}
The :doc:`extends` tag is the key here. It tells the template
engine that this template "extends" another template. When the template system
evaluates this template, first it locates the parent. The extends tag should
be the first tag in the template.
Note that since the child template doesn't define the ``footer`` block, the
value from the parent template is used instead.
It's possible to render the contents of the parent block by using the
:doc:`parent` function. This gives back the results of the
parent block:
.. code-block:: html+twig
{% block sidebar %}
Table Of Contents
...
{{ parent() }}
{% endblock %}
.. tip::
The documentation page for the :doc:`extends` tag describes
more advanced features like block nesting, scope, dynamic inheritance, and
conditional inheritance.
.. note::
Twig also supports multiple inheritance via "horizontal reuse" with the help
of the :doc:`use` tag.
HTML Escaping
-------------
When generating HTML from templates, there's always a risk that a variable
will include characters that affect the resulting HTML. There are two
approaches: manually escaping each variable or automatically escaping
everything by default.
Twig supports both, automatic escaping is enabled by default.
The automatic escaping strategy can be configured via the
:ref:`autoescape` option and defaults to ``html``.
Working with Manual Escaping
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If manual escaping is enabled, it is **your** responsibility to escape variables
if needed. What to escape? Any variable that comes from an untrusted source.
Escaping works by using the :doc:`escape` or ``e`` filter:
.. code-block:: twig
{{ user.username|e }}
By default, the ``escape`` filter uses the ``html`` strategy, but depending on
the escaping context, you might want to explicitly use another strategy:
.. code-block:: twig
{{ user.username|e('js') }}
{{ user.username|e('css') }}
{{ user.username|e('url') }}
{{ user.username|e('html_attr') }}
Working with Automatic Escaping
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Whether automatic escaping is enabled or not, you can mark a section of a
template to be escaped or not by using the :doc:`autoescape`
tag:
.. code-block:: twig
{% autoescape %}
Everything will be automatically escaped in this block (using the HTML strategy)
{% endautoescape %}
By default, auto-escaping uses the ``html`` escaping strategy. If you output
variables in other contexts, you need to explicitly escape them with the
appropriate escaping strategy:
.. code-block:: twig
{% autoescape 'js' %}
Everything will be automatically escaped in this block (using the JS strategy)
{% endautoescape %}
Escaping
--------
It is sometimes desirable or even necessary to have Twig ignore parts it would
otherwise handle as variables or blocks. For example if the default syntax is
used and you want to use ``{{`` as raw string in the template and not start a
variable you have to use a trick.
The easiest way is to output the variable delimiter (``{{``) by using a variable
expression:
.. code-block:: twig
{{ '{{' }}
For bigger sections it makes sense to mark a block
:doc:`verbatim`.
Macros
------
Macros are comparable with functions in regular programming languages. They are
useful to reuse HTML fragments to not repeat yourself. They are described in the
:doc:`macro` tag documentation.
.. _twig-expressions:
Expressions
-----------
Twig allows expressions everywhere.
Literals
~~~~~~~~
The simplest form of expressions are literals. Literals are representations
for PHP types such as strings, numbers, and arrays. The following literals
exist:
* ``"Hello World"``: Everything between two double or single quotes is a
string. They are useful whenever you need a string in the template (for
example as arguments to function calls, filters or just to extend or include
a template).
Note that certain characters require escaping:
* ``\f``: Form feed
* ``\n``: New line
* ``\r``: Carriage return
* ``\t``: Horizontal tab
* ``\v``: Vertical tab
* ``\x``: Hexadecimal escape sequence
* ``\0`` to ``\377``: Octal escape sequences representing characters
* ``\``: Backslash
When using single-quoted strings, the single quote character (``'``) needs to be escaped with a backslash (``\'``).
When using double-quoted strings, the double quote character (``"``) needs to be escaped with a backslash (``\"``).
For example, a single quoted string can contain a delimiter if it is preceded by a
backslash (``\``) -- like in ``'It\'s good'``. If the string contains a
backslash (e.g. ``'c:\Program Files'``) escape it by doubling it
(e.g. ``'c:\\Program Files'``).
* ``42`` / ``42.23``: Integers and floating point numbers are created by
writing the number down. If a dot is present the number is a float,
otherwise an integer. Underscores can be used as digits separator to
improve readability (``-3_141.592_65`` is equivalent to ``-3141.59265``).
* ``["first_name", "last_name"]``: Sequences are defined by a sequence of expressions
separated by a comma (``,``) and wrapped with squared brackets (``[]``).
* ``{"name": "Fabien"}``: Mappings are defined by a list of keys and values
separated by a comma (``,``) and wrapped with curly braces (``{}``):
.. code-block:: twig
{# keys as string #}
{'name': 'Fabien', 'city': 'Paris'}
{# keys as names (equivalent to the previous mapping) #}
{name: 'Fabien', city: 'Paris'}
{# keys as integer #}
{2: 'Twig', 4: 'Symfony'}
{# keys can be omitted if it is the same as the variable name #}
{Paris}
{# is equivalent to the following #}
{'Paris': Paris}
{# keys as expressions (the expression must be enclosed into parentheses) #}
{% set key = 'name' %}
{(key): 'Fabien', (1 + 1): 2, ('ci' ~ 'ty'): 'city'}
* ``true`` / ``false``: ``true`` represents the true value, ``false``
represents the false value.
* ``null``: ``null`` represents no specific value. This is the value returned
when a variable does not exist. ``none`` is an alias for ``null``.
Sequences and mappings can be nested:
.. code-block:: twig
{% set complex = [1, {"name": "Fabien"}] %}
.. tip::
Using double-quoted or single-quoted strings has no impact on performance
but :ref:`string interpolation ` is only
supported in double-quoted strings.
.. _templates-string-interpolation:
String Interpolation
~~~~~~~~~~~~~~~~~~~~
String interpolation (``#{expression}``) allows any valid expression to appear
within a *double-quoted string*. The result of evaluating that expression is
inserted into the string:
.. code-block:: twig
{{ "first #{middle} last" }}
{{ "first #{1 + 2} last" }}
.. tip::
String interpolations can be ignored by escaping them with a backslash
(``\``):
.. code-block:: twig
{# outputs first #{1 + 2} last #}
{{ "first \#{1 + 2} last" }}
Math
~~~~
Twig allows you to do math in templates; the following operators are supported:
* ``+``: Adds two numbers together (the operands are casted to numbers). ``{{
1 + 1 }}`` is ``2``.
* ``-``: Subtracts the second number from the first one. ``{{ 3 - 2 }}`` is
``1``.
* ``/``: Divides two numbers. The returned value will be a floating point
number. ``{{ 1 / 2 }}`` is ``{{ 0.5 }}``.
* ``%``: Calculates the remainder of an integer division. ``{{ 11 % 7 }}`` is
``4``.
* ``//``: Divides two numbers and returns the floored integer result. ``{{ 20
// 7 }}`` is ``2``, ``{{ -20 // 7 }}`` is ``-3`` (this is just syntactic
sugar for the :doc:`round` filter).
* ``*``: Multiplies the left operand with the right one. ``{{ 2 * 2 }}`` would
return ``4``.
* ``**``: Raises the left operand to the power of the right operand. ``{{ 2 **
3 }}`` would return ``8``. Be careful as the ``**`` operator is right
associative, which means that ``{{ -1**0 }}`` is equivalent to ``{{ -(1**0)
}}`` and not ``{{ (-1)**0 }}``.
.. _template_logic:
Logic
~~~~~
You can combine multiple expressions with the following operators:
* ``and``: Returns true if the left and the right operands are both true.
* ``xor``: Returns true if **either** the left or the right operand is true, but not both.
* ``or``: Returns true if the left or the right operand is true.
* ``not``: Negates a statement.
* ``(expr)``: Groups an expression.
.. note::
Twig also supports bitwise operators (``b-and``, ``b-xor``, and ``b-or``).
.. note::
Operators are case sensitive.
Comparisons
~~~~~~~~~~~
The following mathematical comparison operators are supported in any
expression: ``==``, ``!=``, ``<``, ``>``, ``>=``, and ``<=``.
In addition, the ``===`` and ``!==`` strict comparison operators are supported
(they are equivalent to the ``same as`` and ``not same as`` tests).
Spaceship Operator
~~~~~~~~~~~~~~~~~~
The spaceship operator (``<=>``) is used for comparing two expressions. It
returns ``-1``, ``0`` or ``1`` when the first operand is respectively less
than, equal to, or greater than the second operand.
.. note::
Read more about in the `PHP spaceship operator documentation`_.
Iterable Operators
~~~~~~~~~~~~~~~~~~
Check that an iterable ``has every`` or ``has some`` of its elements return
``true`` using an arrow function. The arrow function receives the value of the
iterable as its argument:
.. code-block:: twig
{% set sizes = [34, 36, 38, 40, 42] %}
{% set hasOnlyOver38 = sizes has every v => v > 38 %}
{# hasOnlyOver38 is false #}
{% set hasOver38 = sizes has some v => v > 38 %}
{# hasOver38 is true #}
For an empty iterable, ``has every`` returns ``true`` and ``has some`` returns
``false``.
Containment Operators
~~~~~~~~~~~~~~~~~~~~~
The ``in`` operator performs containment test. It returns ``true`` if the left
operand is contained in the right:
.. code-block:: twig
{# returns true #}
{{ 1 in [1, 2, 3] }}
{{ 'cd' in 'abcde' }}
.. tip::
You can use this operator to perform a containment test on strings,
sequences, mappings, or objects implementing the ``Traversable`` interface.
To perform a negative test, use the ``not in`` operator:
.. code-block:: twig
{% if 1 not in [1, 2, 3] %}
{# is equivalent to #}
{% if not (1 in [1, 2, 3]) %}
The ``starts with`` and ``ends with`` operators are used to check if a string
starts or ends with a given substring:
.. code-block:: twig
{% if 'Fabien' starts with 'F' %}
{% endif %}
{% if 'Fabien' ends with 'n' %}
{% endif %}
.. note::
For complex string comparisons, the ``matches`` operator allows you to use
`regular expressions`_:
.. code-block:: twig
{% if phone matches '/^[\\d\\.]+$/' %}
{% endif %}
Test Operator
~~~~~~~~~~~~~
The ``is`` operator performs tests. Tests can be used to test a variable against
a common expression. The right operand is name of the test:
.. code-block:: twig
{# find out if a variable is odd #}
{{ name is odd }}
Tests can accept arguments too:
.. code-block:: twig
{% if post.status is constant('Post::PUBLISHED') %}
Tests can be negated by using the ``is not`` operator:
.. code-block:: twig
{% if post.status is not constant('Post::PUBLISHED') %}
{# is equivalent to #}
{% if not (post.status is constant('Post::PUBLISHED')) %}
Go to the :doc:`tests` page to learn more about the built-in
tests.
Other Operators
~~~~~~~~~~~~~~~
The following operators don't fit into any of the other categories:
* ``|``: Applies a filter.
* ``..``: Creates a sequence based on the operand before and after the operator
(this is syntactic sugar for the :doc:`range` function):
.. code-block:: twig
{% for i in 1..5 %}{{ i }}{% endfor %}
{# is equivalent to #}
{% for i in range(1, 5) %}{{ i }}{% endfor %}
Note that you must use parentheses when combining it with the filter operator
due to the :ref:`operator precedence rules `:
.. code-block:: twig
{{ (1..5)|join(', ') }}
* ``~``: Converts all operands into strings and concatenates them. ``{{ "Hello
" ~ name ~ "!" }}`` would return (assuming ``name`` is ``'John'``) ``Hello
John!``.
.. _dot_operator:
* ``.``, ``?.``, ``[]``: Gets an attribute of a variable.
The (``.``) operator abstracts getting an attribute of a variable (methods,
properties or constants of a PHP object, or items of a PHP array):
.. code-block:: twig
{{ user.name }}
The null-safe operator (``?.``) works like the dot operator but returns
``null`` instead of throwing an exception when the left operand is ``null``.
If the operand is part of a chain, the rest of the chain is skipped:
.. code-block:: twig
{{ user?.name }}
{# returns null if user is null, otherwise returns user.name #}
{{ user?.address?.city }}
{# can be chained for safe navigation through potentially null values #}
{{ user?.address.city }}
{# returns null if user is null, the rest of the chain is skipped (address.city is not evaluated) #}
.. versionadded:: 3.23
The null-safe operator was added in Twig 3.23.
After the ``.`` or ``?.``, you can use any expression by wrapping it with
parenthesis ``()``.
One use case is when the attribute contains special characters (like ``-``
that would be interpreted as the minus operator):
.. code-block:: twig
{# equivalent to the non-working user.first-name #}
{{ user.('first-name') }}
{{ user?.('first-name') }}
Another use case is when the attribute is "dynamic" (defined via a variable):
.. code-block:: twig
{{ user.(name) }}
{{ user.('get' ~ name) }}
{{ user?.(name) }}
Before Twig 3.15, use the :doc:`attribute ` function
instead for the two previous use cases.
Twig supports a specific syntax via the ``[]`` operator for accessing items
on sequences and mappings:
.. code-block:: twig
{{ user['name'] }}
When calling a method, you can pass arguments using the ``()`` operator:
.. code-block:: twig
{{ html.generate_input() }}
{{ html.generate_input('pwd', 'password') }}
{# or using named arguments #}
{{ html.generate_input(name: 'pwd', type: 'password') }}
.. sidebar:: PHP Implementation
To resolve ``user.name`` to a PHP call, Twig uses the following algorithm
at runtime:
* check if ``user`` is a PHP array or an ArrayObject/ArrayAccess object and
``name`` a valid element;
* if not, and if ``user`` is a PHP object, check that ``name`` is a valid property;
* if not, and if ``user`` is a PHP object, check that ``name`` is a class constant;
* if not, and if ``user`` is a PHP object, check the following methods and
call the first valid one: ``name()``, ``getName()``, ``isName()``, or
``hasName()``;
* if not, and if ``strict_variables`` is ``false``, return ``null``;
* if not, throw an exception.
To resolve ``user?.name`` to a PHP call, Twig checks if ``user`` is
``null`` first:
* if ``user`` is ``null``, return ``null``;
* otherwise, use the same algorithm as for ``user.name``.
To resolve ``user['name']`` to a PHP call, Twig uses the following algorithm
at runtime:
* check if ``user`` is an array and ``name`` a valid element;
* if not, and if ``strict_variables`` is ``false``, return ``null``;
* if not, throw an exception.
Twig supports a specific syntax via the ``()`` operator for calling methods
on objects, like in ``user.name()``:
* check if ``user`` is an object and has the ``name()``, ``getName()``,
``isName()``, or ``hasName()`` method;
* if not, and if ``strict_variables`` is ``false``, return ``null``;
* if not, throw an exception.
* ``?:``: The ternary operator:
.. code-block:: twig
{{ result ? 'yes' : 'no' }}
{{ result ?: 'no' }} is the same as {{ result ? result : 'no' }}
{{ result ? 'yes' }} is the same as {{ result ? 'yes' : '' }}
* ``??``: The null-coalescing operator:
.. code-block:: twig
{# returns the value of result if it is defined and not null, 'no' otherwise #}
{{ result ?? 'no' }}
* ``...``: The spread operator can be used to expand sequences or mappings or
to expand the arguments of a function call:
.. code-block:: twig
{% set numbers = [1, 2, ...moreNumbers] %}
{% set ratings = {'q1': 10, 'q2': 5, ...moreRatings} %}
{{ 'Hello %s %s!'|format(...['Fabien', 'Potencier']) }}
.. versionadded:: 3.15
Support for expanding the arguments of a function call was introduced in
Twig 3.15.
.. _templates-assignment-operator:
* ``=``: The assignment operator assigns a value to a variable within an
expression:
.. code-block:: twig
{# assign #}
{% do b = 1 + 3 %}
{# assign and output the result #}
{{ b = 1 + 3 }}
{# assignments can be chained #}
{% do a = b = 'foo' %}
{# assignment can be used inside other expressions #}
{% do a = (b = 4) + 5 %}
The assignment operator also supports :ref:`destructuring
`.
.. versionadded:: 3.23
The ``=`` assignment operator was added in Twig 3.23.
* ``=>``: The arrow operator allows the creation of functions. A function is
made of arguments (use parentheses for multiple arguments) and an arrow
(``=>``) followed by an expression to execute. The expression has access to
all passed arguments. Arrow functions are supported as arguments for filters,
functions, tests, macros, and method calls.
For instance, the built-in ``map``, ``reduce``, ``sort``, ``filter``, and
``find`` filters accept arrow functions as arguments:
.. code-block:: twig
{{ people|map(p => p.first_name)|join(', ') }}
Arrow functions can be stored in variables:
.. code-block:: twig
{% set first_name_fn = (p) => p.first_name %}
{{ people|map(first_name_fn)|join(', ') }}
.. versionadded:: 3.15
Arrow function support for functions, macros, and method calls was added in
Twig 3.15 (filters and tests were already supported).
Arrow functions can be called using the :doc:`invoke `
filter.
.. versionadded:: 3.19
The ``invoke`` filter has been added in Twig 3.19.
Operators
~~~~~~~~~
Twig uses operators to perform various operations within templates.
Understanding the precedence of these operators is crucial for writing correct
and efficient Twig templates.
The operator precedence rules are as follows, with the highest-precedence
operators listed first.
.. include:: operators_precedence.rst
Without using any parentheses, the operator precedence rules are used to
determine how to convert the code to PHP:
.. code-block:: twig
{{ 6 b-and 2 or 6 b-and 16 }}
{# it is converted to the following PHP code: (6 & 2) || (6 & 16) #}
Change the default precedence by explicitly grouping expressions with
parentheses:
.. code-block:: twig
{% set greeting = 'Hello ' %}
{% set name = 'Fabien' %}
{{ greeting ~ name|lower }} {# Hello fabien #}
{# use parenthesis to change precedence #}
{{ (greeting ~ name)|lower }} {# hello fabien #}
.. _templates-destructuring:
Destructuring
-------------
.. versionadded:: 3.23
Destructuring was added in Twig 3.23.
Destructuring allows you to extract values from sequences and assign them to
variables in a single operation using the ``=`` :ref:`assignment operator
`.
Like in PHP, destructuring expressions return the right-hand side value, not
the extracted values:
.. code-block:: twig
{# returns the full user object, allowing chained access #}
{{ ({name} = user).email }}
Sequence Destructuring
~~~~~~~~~~~~~~~~~~~~~~
Use square brackets on the left side of an assignment to destructure a
sequence:
.. code-block:: twig
{% do [first, last] = ['Fabien', 'Potencier'] %}
{{ first }} {# Fabien #}
{{ last }} {# Potencier #}
If there are more variables than values, the extra variables are set to
``null``:
.. code-block:: twig
{# extra will be null #}
{% do [first, last, extra] = ['Fabien', 'Potencier'] %}
You can skip values by leaving a slot empty:
.. code-block:: twig
{# only assign the second value #}
{% do [, last] = ['Fabien', 'Potencier'] %}
Object Destructuring
~~~~~~~~~~~~~~~~~~~~
Use curly braces on the left side of an assignment to destructure an object
or mapping by extracting values based on property/key names:
.. code-block:: twig
{% do {name, email} = user %}
{{ name }} {# user.name #}
{{ email }} {# user.email #}
You can rename variables during destructuring by using the ``key: variable``
syntax, where the key is the property to extract and the variable is the name
to assign to:
.. code-block:: twig
{% do {name: userName, email: userEmail} = user %}
{{ userName }} {# user.name #}
{{ userEmail }} {# user.email #}
This is especially useful when you need to destructure multiple objects that
share the same property names:
.. code-block:: twig
{% do {data: product, error: productError} = loadProduct() %}
{% do {data: stock, error: stockError} = loadStock() %}
{{ product }} {# loadProduct().data #}
{{ productError }} {# loadProduct().error #}
{{ stock }} {# loadStock().data #}
{{ stockError }} {# loadStock().error #}
.. note::
Object destructuring uses the :ref:`dot operator ` to access
values, so ``{name} = user`` is equivalent to ``name = user.name`` or
``name = user["name"]`` depending on the type of the variable.
.. _templates-whitespace-control:
Whitespace Control
------------------
The first newline after a template tag is removed automatically (like in PHP).
Whitespace is not further modified by the template engine, so each whitespace
(spaces, tabs, newlines etc.) is returned unchanged.
You can also control whitespace on a per tag level. By using the whitespace
control modifiers on your tags, you can trim leading and or trailing whitespace.
Twig supports two modifiers:
* *Whitespace trimming* via the ``-`` modifier: Removes all whitespace
(including newlines);
* *Line whitespace trimming* via the ``~`` modifier: Removes all whitespace
(excluding newlines). Using this modifier on the right disables the default
removal of the first newline inherited from PHP.
The modifiers can be used on either side of the tags like in ``{%-`` or ``-%}``
and they consume all whitespace for that side of the tag. It is possible to use
the modifiers on one side of a tag or on both sides:
.. code-block:: html+twig
{% set value = 'no spaces' %}
{#- No leading/trailing whitespace -#}
{%- if true -%}
{{- value -}}
{%- endif -%}
{# output 'no spaces' #}
{{ value }}
{# outputs '
\n no spaces
' #}
{{- value }}
{# outputs '
no spaces
' #}
{{~ value }}
{# outputs '
\nno spaces
' #}
Extensions
----------
Twig can be extended. If you want to create your own extensions, read the
:ref:`Creating an Extension ` chapter.
.. _`Twig bundle`: https://github.com/uhnomoli/PHP-Twig.tmbundle
.. _`vim-twig plugin`: https://github.com/lumiliet/vim-twig
.. _`Twig plugin`: https://github.com/pulse00/Twig-Eclipse-Plugin
.. _`Twig language definition`: https://github.com/gabrielcorpse/gedit-twig-template-language
.. _`Twig syntax mode`: https://github.com/bobthecow/Twig-HTML.mode
.. _`other Twig syntax mode`: https://github.com/muxx/Twig-HTML.mode
.. _`Notepad++ Twig Highlighter`: https://github.com/Banane9/notepadplusplus-twig
.. _`web-mode.el`: https://web-mode.org/
.. _`regular expressions`: https://www.php.net/manual/en/pcre.pattern.php
.. _`PHP-twig for atom`: https://github.com/reesef/php-twig
.. _`TwigQI`: https://github.com/alisqi/TwigQI
.. _`TwigStan`: https://github.com/twigstan/twigstan
.. _`Twig pack`: https://marketplace.visualstudio.com/items?itemName=bajdzis.vscode-twig-pack
.. _`Modern Twig`: https://marketplace.visualstudio.com/items?itemName=Stanislav.vscode-twig
.. _`Twig CS Fixer`: https://github.com/VincentLanglet/Twig-CS-Fixer
.. _`Twig Language Server`: https://github.com/kaermorchen/twig-language-server/tree/master/packages/language-server
.. _`Twiggy`: https://marketplace.visualstudio.com/items?itemName=moetelo.twiggy
.. _`PHP spaceship operator documentation`: https://www.php.net/manual/en/language.operators.comparison.php
================================================
FILE: doc/tests/constant.rst
================================================
``constant``
============
``constant`` checks if a variable has the exact same value as a constant. You
can use either global constants or class constants:
.. code-block:: twig
{% if post.status is constant('Post::PUBLISHED') %}
the status attribute is exactly the same as Post::PUBLISHED
{% endif %}
You can test constants from object instances as well:
.. code-block:: twig
{% if post.status is constant('PUBLISHED', post) %}
the status attribute is exactly the same as Post::PUBLISHED
{% endif %}
================================================
FILE: doc/tests/defined.rst
================================================
``defined``
===========
``defined`` checks if a variable is defined in the current context. This is very
useful if you use the ``strict_variables`` option:
.. code-block:: twig
{# defined works with variable names #}
{% if user is defined %}
...
{% endif %}
{# and attributes on variables names #}
{% if user.name is defined %}
...
{% endif %}
{% if user['name'] is defined %}
...
{% endif %}
When using the ``defined`` test on an expression that uses variables in some
method calls, be sure that they are all defined first:
.. code-block:: twig
{% if var is defined and user.name(var) is defined %}
...
{% endif %}
================================================
FILE: doc/tests/divisibleby.rst
================================================
``divisible by``
================
``divisible by`` checks if a variable is divisible by a number:
.. code-block:: twig
{% if loop.index is divisible by(3) %}
...
{% endif %}
================================================
FILE: doc/tests/empty.rst
================================================
``empty``
=========
``empty`` checks if a variable is an empty string, an empty sequence, an empty
mapping, exactly ``false``, or exactly ``null``.
For objects that implement the ``Countable`` interface, ``empty`` will check the
return value of the ``count()`` method.
For objects that implement the ``__toString()`` magic method (and not ``Countable``),
it will check if an empty string is returned.
.. code-block:: twig
{% if user is empty %}
...
{% endif %}
================================================
FILE: doc/tests/even.rst
================================================
``even``
========
``even`` returns ``true`` if the given number is even:
.. code-block:: twig
{{ var is even }}
.. seealso::
:doc:`odd<../tests/odd>`
================================================
FILE: doc/tests/index.rst
================================================
Tests
=====
.. toctree::
:maxdepth: 1
constant
defined
divisibleby
empty
even
iterable
mapping
null
odd
sameas
sequence
================================================
FILE: doc/tests/iterable.rst
================================================
``iterable``
============
``iterable`` checks if a variable is an array or a traversable object:
.. code-block:: twig
{# evaluates to true if the users variable is iterable #}
{% if users is iterable %}
{% for user in users %}
Hello {{ user }}!
{% endfor %}
{% else %}
{# users is probably a string #}
Hello {{ users }}!
{% endif %}
================================================
FILE: doc/tests/mapping.rst
================================================
``mapping``
===========
``mapping`` checks if a variable is a mapping:
.. code-block:: twig
{% set users = {alice: "Alice Dupond", bob: "Bob Smith"} %}
{# evaluates to true if the users variable is a mapping #}
{% if users is mapping %}
{% for key, user in users %}
{{ key }}: {{ user }};
{% endfor %}
{% endif %}
================================================
FILE: doc/tests/null.rst
================================================
``null``
========
``null`` returns ``true`` if the variable is ``null``:
.. code-block:: twig
{{ var is null }}
.. note::
``none`` is an alias for ``null``.
================================================
FILE: doc/tests/odd.rst
================================================
``odd``
=======
``odd`` returns ``true`` if the given number is odd:
.. code-block:: twig
{{ var is odd }}
.. seealso::
:doc:`even<../tests/even>`
================================================
FILE: doc/tests/sameas.rst
================================================
``same as``
===========
``same as`` checks if a variable is the same as another variable.
This is equivalent to ``===`` in PHP:
.. code-block:: twig
{% if user.name is same as(false) %}
the user attribute is the 'false' PHP value
{% endif %}
================================================
FILE: doc/tests/sequence.rst
================================================
``sequence``
============
``sequence`` checks if a variable is a sequence:
.. code-block:: twig
{% set users = ["Alice", "Bob"] %}
{# evaluates to true if the users variable is a sequence #}
{% if users is sequence %}
{% for user in users %}
Hello {{ user }}!
{% endfor %}
{% endif %}
================================================
FILE: extra/cache-extra/.gitattributes
================================================
/Tests export-ignore
/.git* export-ignore
/phpunit.xml.dist export-ignore
================================================
FILE: extra/cache-extra/.gitignore
================================================
vendor/
composer.lock
phpunit.xml
.phpunit.result.cache
================================================
FILE: extra/cache-extra/CacheExtension.php
================================================
cache = $cache;
}
public function getCache(): CacheInterface
{
return $this->cache;
}
}
================================================
FILE: extra/cache-extra/LICENSE
================================================
Copyright (c) 2021-present Fabien Potencier
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
================================================
FILE: extra/cache-extra/Node/CacheNode.php
================================================
setAttribute('raw', true);
$nodes = ['key' => $key, 'body' => $body];
if (null !== $ttl) {
$nodes['ttl'] = $ttl;
}
if (null !== $tags) {
$nodes['tags'] = $tags;
}
parent::__construct($nodes, [], $lineno);
}
public function compile(Compiler $compiler): void
{
$compiler
->addDebugInfo($this)
->raw('$this->env->getRuntime(\'Twig\Extra\Cache\CacheRuntime\')->getCache()->get(')
->subcompile($this->getNode('key'))
->raw(", function (\Symfony\Contracts\Cache\ItemInterface \$item) use (\$context, \$macros, \$blocks) {\n")
->indent()
;
if ($this->hasNode('tags')) {
$compiler
->write('$item->tag(')
->subcompile($this->getNode('tags'))
->raw(");\n")
;
}
if ($this->hasNode('ttl')) {
$compiler
->write('$item->expiresAfter(')
->subcompile($this->getNode('ttl'))
->raw(");\n")
;
}
$compiler
->write('return ')
->subcompile($this->getNode('body'))
->raw(";\n")
->outdent()
->write("})\n")
;
}
}
================================================
FILE: extra/cache-extra/README.md
================================================
Cache Extension
===============
This package is a Twig extension that provides integration with the Symfony
Cache component.
It provides a single [`cache`][1] tag that allows to cache template fragments.
[1]: https://twig.symfony.com/cache
================================================
FILE: extra/cache-extra/Tests/Fixtures/cache.test
================================================
--TEST--
"cache" tag
--TEMPLATE--
{% set foo = "bar" %}
{% set value1 %}
{% cache "test;v1" ttl(3) %}
{% set foo = "bar1" %}
{{ random(1, 1000000) }}
{% endcache %}
{% endset %}
{% set value2 %}
{% cache "test;v1" ttl(3) %}
{{ random(1, 1000000) }}
{% endcache %}
{% endset %}
{{ value1 == value2 ? 'OK' : 'KO' }}
{{ foo == "bar" ? 'OK' : 'KO' }}
--DATA--
return []
--EXPECT--
OK
OK
================================================
FILE: extra/cache-extra/Tests/Fixtures/cache_complex.test
================================================
--TEST--
"cache" tag
--TEMPLATE--
{% cache 'test_%s_%s'|format(10, 10000) ttl(36000) %}
{% set content %}
ok
{% endset %}
{% apply upper %}
{{ content }}
{% endapply %}
{% endcache %}
--DATA--
return []
--EXPECT--
OK
================================================
FILE: extra/cache-extra/Tests/Fixtures/cache_with_blocks.test
================================================
--TEST--
"cache" tag
--TEMPLATE--
{% extends "layout.twig" %}
{% block bar %}
{% cache "foo" %}
{%- block content %}FOO{% endblock %}
{% endcache %}
{% endblock %}
--TEMPLATE(layout.twig)--
{% block content %}{% endblock %}
--DATA--
return []
--EXPECT--
FOO
================================================
FILE: extra/cache-extra/Tests/Fixtures/macro.test
================================================
--TEST--
macro call inside "cache" tag
--TEMPLATE--
{% macro macro_out(bar) %}{{ bar }}{% endmacro %}
1
{% cache "testmacro1" ttl(3) %}
{%~ macro macro_in(bar) %}{{ bar }}{% endmacro %}
2
{{ _self.macro_out(3) }}
{{ _self.macro_in(4) }}
{% endcache %}
5
{{ _self.macro_in(6) }}
--DATA--
return []
--EXPECT--
1
2
3
4
5
6
================================================
FILE: extra/cache-extra/Tests/FunctionalTest.php
================================================
createEnvironment(['index' => '{% cache "city;v1" %}{{- city -}}{% endcache %}'], $cache);
$this->assertSame('Paris', $twig->render('index', ['city' => 'Paris']));
$value = $cache->get('city;v1', static function () { throw new \RuntimeException('Key should be in the cache'); });
$this->assertSame('Paris', $value);
}
public function testTtlNoArgs()
{
$twig = $this->createEnvironment(['index' => '{% cache "ttl_no_args" ttl() %}{% endcache %}']);
$this->expectException(SyntaxError::class);
$this->expectExceptionMessage('The "ttl" modifier takes exactly one argument (0 given) in "index" at line 1.');
$twig->render('index');
}
public function testTtlTooManyArgs()
{
$twig = $this->createEnvironment(['index' => '{% cache "ttl_too_many_args" ttl(0, 1) %}{% endcache %}']);
$this->expectException(SyntaxError::class);
$this->expectExceptionMessage('The "ttl" modifier takes exactly one argument (2 given) in "index" at line 1.');
$twig->render('index');
}
public function testTagsNoArgs()
{
$twig = $this->createEnvironment(['index' => '{% cache "tags_no_args" tags() %}{% endcache %}']);
$this->expectException(SyntaxError::class);
$this->expectExceptionMessage('The "tags" modifier takes exactly one argument (0 given) in "index" at line 1.');
$twig->render('index');
}
public function testTagsTooManyArgs()
{
$twig = $this->createEnvironment(['index' => '{% cache "tags_too_many_args" tags(["foo"], 1) %}{% endcache %}']);
$this->expectException(SyntaxError::class);
$this->expectExceptionMessage('The "tags" modifier takes exactly one argument (2 given) in "index" at line 1.');
$twig->render('index');
}
private function createEnvironment(array $templates, ?ArrayAdapter $cache = null): Environment
{
$twig = new Environment(new ArrayLoader($templates));
$cache = $cache ?? new ArrayAdapter();
$twig->addExtension(new CacheExtension());
$twig->addRuntimeLoader(new class($cache) implements RuntimeLoaderInterface {
private $cache;
public function __construct(CacheInterface $cache)
{
$this->cache = $cache;
}
public function load(string $class): ?object
{
return CacheRuntime::class === $class ? new CacheRuntime($this->cache) : null;
}
});
return $twig;
}
}
================================================
FILE: extra/cache-extra/Tests/IntegrationTest.php
================================================
parser->getStream();
$key = $this->parser->parseExpression();
$ttl = null;
$tags = null;
while ($stream->test(Token::NAME_TYPE)) {
$k = $stream->getCurrent()->getValue();
if (!\in_array($k, ['ttl', 'tags'], true)) {
throw new SyntaxError(\sprintf('Unknown "%s" configuration.', $k), $stream->getCurrent()->getLine(), $stream->getSourceContext());
}
$stream->next();
$stream->expect(Token::OPERATOR_TYPE, '(');
$line = $stream->getCurrent()->getLine();
if ($stream->test(Token::PUNCTUATION_TYPE, ')')) {
throw new SyntaxError(\sprintf('The "%s" modifier takes exactly one argument (0 given).', $k), $line, $stream->getSourceContext());
}
$arg = $this->parser->parseExpression();
if ($stream->test(Token::PUNCTUATION_TYPE, ',')) {
throw new SyntaxError(\sprintf('The "%s" modifier takes exactly one argument (2 given).', $k), $line, $stream->getSourceContext());
}
$stream->expect(Token::PUNCTUATION_TYPE, ')');
if ('ttl' === $k) {
$ttl = $arg;
} elseif ('tags' === $k) {
$tags = $arg;
}
}
$stream->expect(Token::BLOCK_END_TYPE);
$body = $this->parser->subparse([$this, 'decideCacheEnd'], true);
$stream->expect(Token::BLOCK_END_TYPE);
$body = new CacheNode($key, $ttl, $tags, $body, $token->getLine());
return new PrintNode(new RawFilter($body), $token->getLine());
}
public function decideCacheEnd(Token $token): bool
{
return $token->test('endcache');
}
public function getTag(): string
{
return 'cache';
}
}
================================================
FILE: extra/cache-extra/composer.json
================================================
{
"name": "twig/cache-extra",
"type": "library",
"description": "A Twig extension for Symfony Cache",
"keywords": ["twig", "html", "cache"],
"homepage": "https://twig.symfony.com",
"license": "MIT",
"minimum-stability": "dev",
"authors": [
{
"name": "Fabien Potencier",
"email": "fabien@symfony.com",
"homepage": "http://fabien.potencier.org",
"role": "Lead Developer"
}
],
"require": {
"php": ">=8.1.0",
"symfony/cache": "^5.4|^6.4|^7.0|^8.0",
"twig/twig": "^3.21|^4.0"
},
"require-dev": {
"symfony/phpunit-bridge": "^6.4|^7.0"
},
"autoload": {
"psr-4" : { "Twig\\Extra\\Cache\\" : "" },
"exclude-from-classmap": [
"/Tests/"
]
}
}
================================================
FILE: extra/cache-extra/phpunit.xml.dist
================================================
././Tests./vendor./Tests/
================================================
FILE: extra/cssinliner-extra/.gitattributes
================================================
/Tests export-ignore
/.git* export-ignore
/phpunit.xml.dist export-ignore
================================================
FILE: extra/cssinliner-extra/.gitignore
================================================
vendor/
composer.lock
phpunit.xml
.phpunit.result.cache
================================================
FILE: extra/cssinliner-extra/CssInlinerExtension.php
================================================
['all']]),
];
}
/**
* @internal
*/
public static function inlineCss(string $body, string ...$css): string
{
static $inliner;
if (null === $inliner) {
$inliner = new CssToInlineStyles();
}
return $inliner->convert($body, implode("\n", $css));
}
}
================================================
FILE: extra/cssinliner-extra/LICENSE
================================================
Copyright (c) 2019-present Fabien Potencier
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
================================================
FILE: extra/cssinliner-extra/README.md
================================================
Twig CssInliner Extension
=========================
This package is a Twig extension that provides the following:
* [`inline_css`][1] filter: inlines CSS styles in HTML documents.
[1]: https://twig.symfony.com/inline_css
================================================
FILE: extra/cssinliner-extra/Resources/functions.php
================================================