// plain
Yield in Twig with PHP can be used to create a template that can be reused in multiple places. It allows you to create a template that can be used to render a specific part of a page.
{% set my_template = '<div>{{ content }}</div>' %}
{% macro my_macro(content) %}
{{ my_template|format(content=content) }}
{% endmacro %}
{{ my_macro('Hello World!') }}
<div>Hello World!</div>
{% set my_template = '<div>{{ content }}</div>' %}
: This sets a variable calledmy_template
to a string containing a template.{% macro my_macro(content) %}
: This defines a macro calledmy_macro
which takes a parameter calledcontent
.{{ my_template|format(content=content) }}
: This uses theformat
filter to render themy_template
variable with thecontent
parameter passed to the macro.{{ my_macro('Hello World!') }}
: This calls themy_macro
macro with the stringHello World!
as thecontent
parameter.
onelinerhub: How to use yield in Twig with PHP?