Symfony Templating (Twig)
Twig is a modern template engine for PHP
Official link : Symfony Twig
https://symfony.com/doc/current/templating.html copy to clipboard
- Fast: Twig compiles templates down to plain optimized PHP code. The overhead compared to regular PHP code was reduced to the very minimum.
- Secure: Twig has a sandbox mode to evaluate untrusted template code. This allows Twig to be used as a template language for applications where users may modify the template design.
- Flexible: Twig is powered by a flexible lexer and parser. This allows the developer to define its own custom tags and filters, and create its own DSL.
Official link : Symfony Twig
Templating
Twig balises proprietaireshttps://symfony.com/doc/current/templating.html copy to clipboard
class IndexController extends Controller
...
public function index() {
return $this->render('base.html.twig');
}
extends @@@TODO@@@
==> extends des twig
{% extends "base.html.twig" %}
{#AUCUN CONTENU NE DOIT ETRE MIS EN DEHORS DES BLOCK QUAND IL Y A UN EXTEND !!#}
{% block css %}
{% endblock %}
{% block html %}
Welcome into this page
{% endblock %}
{% block js %}
{% endblock %}
{{ dump(articles) }}
concatennation avec ~
{{ asset('images/product/'~article.featuredImage) }}
{% for article in articles %}
{% endfor %}
but de asset generation de lien absolu vers le fichier
https://symfony.com/doc/current/templating.html
route complete vers fichier
{{ asset('css/font-awesome.min.css') }
pareil pour les images
{{ asset('img/logo.png') }}
nom asset avec "_" en prefixe pour include
public function index() : Response
{
return $this->render('index/index.html.twig');
}
assets @@@TODO@@@
Links
Génération des URLs : Doc de Référence : https://symfony.com/doc/current/templating.html#linking-to-pages Nous allons maintenant mettre en place la navigation inter-pages de notre site. Accueil Ici, la fonction url() va demander à Symfony de créer une URL absolue pour la route index. Le résultat après compilation sera alors : Accueil Lorsqu’il y à des paramètres, nous procédons de la façons suivantes : Business Ce qui donnera : BusinessProviders (basics)
copy to clipboard
composer require symfony/yamlsrc/Service/Article/articles.yaml copy to clipboard
data:
article1 :
id : 1
title : 'This is a fake article title'
content : 'This is a fake content'
featuredimage : '3.jpg'
special : false
spotlight : true
datecreation : 2019-02-27 23:41:57
category:
id : 2
label : 'Business'
article2 :...
article3 :...
public function index(ArticleProvider $articleProvider)
$articles = $articleProvider->getArticles();
return $this->render('index/index.html.twig', [
'articles' => $articles
]);
{{ dump(articles) }}
