php - Difference between Include, Extends, Use, Macro, Embed in Twig -


what difference between use , include in twig?

documentation:

include

the include statement includes template , returns rendered content of template current one:

{% include 'header.html' %} body here... {% include 'footer.html' %} 

use

the use statement tells twig import blocks defined in blocks.html current template (it's macros, blocks):

blocks.html

{% block sidebar %}{% endblock %} 

main.html

{% extends "base.html" %} {% use "blocks.html" %} {% block title %}{% endblock %} {% block content %}{% endblock %} 

possible answer:

i think this should explain difference:

include code external file , import actual file @ right location of call.

use different parses linked file find particular section of code , overwrites blocks same name, in current file, 1 found in external file.

include "go find file , render page here".

use "parse other file find block definitions use instead of owns defined here".

if use command finds nothing matching task, nothing displayed @ file.


question

is explanation correct? there other explanations difference?

thanks

after months, posting answer further reference question. added description extends & import & macro & embed more clearance:

there various types of inheritance , code reuse in twig:

include

the main goal code reuse. consider using header.html.twig & footer.html.twig inside base.html.twig example.

header.html.twig

<nav>    <div>homepage</div>    <div>about</div> </nav> 

base.html.twig

{% include 'header.html.twig' %} <main>{% block main %}{% endblock %}</main> 

extends

the main goal vertical inheritance. consider extending base.html.twig inside homepage.html.twig , about.html.twig example.

base.html.twig

{% include 'header.html.twig' %} <main>{% block main %}{% endblock %}</main> 

homepage.html.twig

{% extends 'base.html.twig' %}  {% block main %} <p>you @ homepage</p> {% endblock %} 

about.html.twig

{% extends 'base.html.twig' %}  {% block main %} <p>you @ page</p> {% endblock %} 

use

the main goal horizontal reuse. consider using sidebar.product.html.twig inside single.product.html.twig (extends product.layout.html.twig) , single.service.html.twig (extends 'service.layout.html.page') pages. (it's macros, blocks)

sidebar.html.twig

<aside>{% block sidebar %}{% endblock %}</aside> 

single.product.html.twig

{% extends 'product.layout.html.twig' %}  {% use 'sidebar.html.twig' %} {% block main %} <p>you @ product page product number 123</p> {% endblock %} 

single.service.html.twig

{% extends 'service.layout.html.twig' %}  {% use 'sidebar.html.twig' %} {% block main %} <p>you @ service page service number 456</p> {% endblock %} 

macro

the main goal having reusable markup across many templates variables. consider function gets variables , outputs markup.

form.html.twig

{% macro input(name, value, type) %}     <input type="{{ type|default('text') }}" name="{{ name }}" value="{{ value|e }}" }}" /> {% endmacro %} 

profile.service.html.twig

{% import "forms.html.twig" forms %}  <div>{{ forms.input('username') }}</div> 

embed

the main goal block overriding. has functionality of both use & include together. consider embedding pagination.html.twig in product.table.html.twig & service.table.html.twig.

pagination.html.twig

<div>     <div>{% block first %}{% endblock %}</div>     {% in (min + 1)..(max - 1) %}         <div>{{ }}</div>     {% endfor %}     <div>{% block last %}{% endblock %}</div> </div> 

product.table.html.twig

{% set min, max = 1, products.itemperpage %}  {% embed 'pagination.html.twig' %}     {% block first %}first product page{% endblock %}     {% block last %}last product page{% endblock %} {% endembed %} 

service.table.html.twig

{% set min, max = 1, services.itemperpage %}  {% embed 'pagination.html.twig' %}     {% block first %}first service page{% endblock %}     {% block last %}last service page{% endblock %} {% endembed %} 

please note embedded file (pagination.html.twig here) has access current context (min, max variables here). may pass variables embedded file:

pagination.html.twig

<p>{{ count }} items</p> <div>     <div>{% block first %}{% endblock %}</div>     {% in (min + 1)..(max - 1) %}         <div>{{ }}</div>     {% endfor %}     <div>{% block last %}{% endblock %}</div> </div> 

product.table.html.twig

{% set min, max = 1, products|length %}  {% embed 'pagination.html.twig' {'count': products|length } %}     {% block first %}first product page{% endblock %}     {% block last %}last product page{% endblock %} {% endembed %} 

Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -