Browse Source

Ch. 5: Best practices for JS/CSS assets, parent() in Twig blocks, Twig app variable.

Frederic G. MARAND 6 years ago
parent
commit
7cc99a4e75

+ 3 - 0
public/styles/blog.css

@@ -0,0 +1,3 @@
+.byline {
+  font-style: oblique;
+}

+ 1 - 1
src/Controller/BlogController.php

@@ -12,7 +12,7 @@ class BlogController extends Controller {
     1 => [
       'id' => '1',
       'title' => 'Title 1',
-      'body' => 'Body 1',
+      'body' => 'Body 1 <3 escaped',
       'authorName' => 'Fred',
       ],
     2 => [

+ 1 - 1
src/Controller/DefaultController.php

@@ -20,7 +20,7 @@ class DefaultController extends AbstractController {
    * )
    */
   public function index() {
-    return new Response("Hello");
+    return $this->render('hello.html.twig', ["message" => "Hello!"]);
   }
 
   /**

+ 3 - 2
templates/base.html.twig

@@ -3,8 +3,9 @@
     <head>
         <meta charset="UTF-8">
         <title>{% block title %}Test application{% endblock %}</title>
-        {% block stylesheets %}
-        <link rel="stylesheet" href="{{ absolute_url(asset('styles/app.css')) }}" />{% endblock %}
+        {% block stylesheets -%}
+        <link rel="stylesheet" href="{{ absolute_url(asset('styles/app.css')) }}" />
+        {% endblock %}
     </head>
     <body>
         <div id="sidebar">

+ 5 - 0
templates/blog/index.html.twig

@@ -18,3 +18,8 @@
 
     <p>{{ pager }}</p>
 {% endblock %}
+
+{% block stylesheets %}
+{{ parent() -}}
+        <link rel="stylesheet" href="{{ asset('styles/blog.css') }}" />
+{% endblock %}

+ 12 - 0
templates/hello.html.twig

@@ -0,0 +1,12 @@
+{% extends "base.html.twig" %}
+
+{% block body %}
+<h1>{{ message }}</h1>
+
+<h2>Environment</h2>
+    <p>{{ app.environment }}</p>
+
+<h2>Debug ?</h2>
+    {% if app.debug %}Yes{% else %}No{% endif %}
+
+{% endblock %}