twig
Sjabloonovererving
Zoeken…
Een basissjabloon gebruiken
base.twig.html
<!DOCTYPE html>
<html>
<head>
<title>{{ title | default('Hello World') }}</title>
<link rel="stylesheet" type="text/css" href="theme.css">
{% block css %}
{% endblock %}
</head>
<body>
{% block body %}
<nav>
{% block navigation %}
<a href="#">Link</a>
<a href="#">Link</a>
<a href="#">Link</a>
{% endblock navigation %}
</nav>
<section id="container">
<section id="content">
{% block content %}
<p>
Lorem ipsum dolor sit amet.
</p>
</section>
{% endblock content %}
</section>
{% endblock body %}
</body>
</html>
page.twig.html
{% extends base.twig.html %}
{% block navigation %}
<a href="page2.html">Page 2</a>
<a href="page3.html">Page 3</a>
<a href="page4.html">Page 4</a>
{% endblock %}
{% block content %}
This is my first page
{% endblock content %}
Wijzig de inhoud van een opgenomen sjabloon
article.twig.html
<article>
<h1>{{ article.title }}</h1>
{% block content %}
<p>{{ article.content }}</p>
{% endblock %}
</article>
articles.twig.html
{# use default template for article #}
{% for article in articles %}
{% include "article.twig.html" %}
{% endfor %}
{# use custom template for article #}
{% for article in articles %}
{% embed "article.twig.html" %}
{% block content %}
<img src="{{ article.image }}" alt="{{ article.title }}" />
{{ parent() }}
{% endblock %}
{% endembed %}
{% endfor %}
Variabele overerving
parent.html.twig
<!DOCTYPE html>
<html>
<head>
<title>{{ title_variable | default('Normal Page') }}</title>
</head>
<body>
<h1>This is the {{ title_variable }} Page</h1>
</body>
</html>
child.html.twig
{% extends "parent.html.twig" %}
{% set title_variable = "Child" %}
Vergelijking van Opnemen, Verlengen, Gebruik, Macro, Insluiten
Er zijn verschillende soorten overerving en hergebruik van code in Twig:
omvatten
Het hoofddoel is hergebruik van code . Overweeg header.html.twig & footer.html.twig in base.html.twig als voorbeeld.
header.html.twig
<nav>
<div>Homepage</div>
<div>About</div>
</nav>
base.html.twig
{% include 'header.html.twig' %}
<main>{% block main %}{% endblock %}</main>
breidt
Het hoofddoel is verticale erfenis . Overwegen base.html.twig binnen homepage.html.twig en about.html.twig als voorbeeld.
base.html.twig
{% include 'header.html.twig' %}
<main>{% block main %}{% endblock %}</main>
homepage.html.twig
{% extends 'base.html.twig' %}
{% block main %}
<p>You are at the homepage</p>
{% endblock %}
about.html.twig
{% extends 'base.html.twig' %}
{% block main %}
<p>You are at the about page</p>
{% endblock %}
Gebruik
Het hoofddoel is horizontaal hergebruik . Overweeg het gebruik van sidebar.product.html.twig binnen single.product.html.twig (verlengt product.layout.html.twig ) en single.service.html.twig (verlengt 'service.layout.html.page') pagina's. (het is net als macro's, maar voor blokken)
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 are at the product page for product number 123</p>
{% endblock %}
single.service.html.twig
{% extends 'service.layout.html.twig' %}
{% use 'sidebar.html.twig' %}
{% block main %}
<p>You are at the service page for service number 456</p>
{% endblock %}
macro
Het belangrijkste doel is herbruikbare markeringen voor veel sjablonen met variabelen . Overweeg een functie die enkele variabelen ophaalt en markeringen uitvoert.
form.html.twig
{% macro-invoer (naam, waarde, type)%} <input type = "{{type | default ('text')}}" name = "{{name}}" value = "{{value | e}} "}}" /> {% endmacro%}
profile.service.html.twig
{% import "forms.html.twig" as forms %}
<div>{{ forms.input('username') }}</div>
Integreren
Het hoofddoel is het opheffen van blokken . Het heeft functionaliteit van zowel Use & Include samen. Overweeg pagination.html.twig in te pagination.html.twig in product.table.html.twig & service.table.html.twig .
pagination.html.twig
<div>
<div>{% block first %}{% endblock %}</div>
{% for i in (min + 1)..(max - 1) %}
<div>{{ i }}</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 %}
Houd er rekening mee dat het ingesloten bestand ( pagination.html.twig hier) toegang heeft tot de huidige context ( min , max variabelen hier). U kunt ook extra variabelen doorgeven aan het ingesloten bestand:
pagination.html.twig
<p>{{ count }} items</p>
<div>
<div>{% block first %}{% endblock %}</div>
{% for i in (min + 1)..(max - 1) %}
<div>{{ i }}</div>
{% endfor %}
<div>{% block last %}{% endblock %}</div>
</div>
product.table.html.twig
{% set min, max = 1, products|length %}
{% embed 'pagination.html.twig' with {'count': products|length } %}
{% block first %}First Product Page{% endblock %}
{% block last %}Last Product Page{% endblock %}
{% endembed %}