Frogg logo

Frogg's web tools

Hand Crafted Tools

Home > Coding Symfony > Symfony Templating (Twig)
Welcome on Frogg's web tools | Current date :
16/10/2024

Symfony Templating (Twig)

Twig is a modern template engine for PHP

  • 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 proprietaires
https://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 : Business

Providers (basics)

copy to clipboard
composer require symfony/yaml
src/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) }}
Page created by the 27/02/2018 22:21
Generated in 0.002 sec & displayed in ... sec