Browse Source

Ch. 5: Including templates, path/url/asset/absolute_url Twig functions.

Frederic G. MARAND 6 years ago
parent
commit
7a7c1bc815

+ 1 - 0
.idea/php.xml

@@ -45,6 +45,7 @@
       <path value="$PROJECT_DIR$/vendor/symfony/security" />
       <path value="$PROJECT_DIR$/vendor/symfony/inflector" />
       <path value="$PROJECT_DIR$/vendor/symfony/property-access" />
+      <path value="$PROJECT_DIR$/vendor/symfony/asset" />
     </include_path>
   </component>
   <component name="PhpProjectSharedConfiguration" php_language_level="7.1" />

+ 3 - 0
.idea/sf4-book.iml

@@ -18,6 +18,7 @@
       <excludeFolder url="file://$MODULE_DIR$/vendor/psr/simple-cache" />
       <excludeFolder url="file://$MODULE_DIR$/vendor/sensio/framework-extra-bundle" />
       <excludeFolder url="file://$MODULE_DIR$/vendor/sensiolabs/security-checker" />
+      <excludeFolder url="file://$MODULE_DIR$/vendor/symfony/asset" />
       <excludeFolder url="file://$MODULE_DIR$/vendor/symfony/cache" />
       <excludeFolder url="file://$MODULE_DIR$/vendor/symfony/config" />
       <excludeFolder url="file://$MODULE_DIR$/vendor/symfony/console" />
@@ -68,6 +69,7 @@
           <root url="file://$MODULE_DIR$/vendor/psr/simple-cache" />
           <root url="file://$MODULE_DIR$/vendor/sensio/framework-extra-bundle" />
           <root url="file://$MODULE_DIR$/vendor/sensiolabs/security-checker" />
+          <root url="file://$MODULE_DIR$/vendor/symfony/asset" />
           <root url="file://$MODULE_DIR$/vendor/symfony/cache" />
           <root url="file://$MODULE_DIR$/vendor/symfony/config" />
           <root url="file://$MODULE_DIR$/vendor/symfony/console" />
@@ -113,6 +115,7 @@
           <root url="file://$MODULE_DIR$/vendor/psr/simple-cache" />
           <root url="file://$MODULE_DIR$/vendor/sensio/framework-extra-bundle" />
           <root url="file://$MODULE_DIR$/vendor/sensiolabs/security-checker" />
+          <root url="file://$MODULE_DIR$/vendor/symfony/asset" />
           <root url="file://$MODULE_DIR$/vendor/symfony/cache" />
           <root url="file://$MODULE_DIR$/vendor/symfony/config" />
           <root url="file://$MODULE_DIR$/vendor/symfony/console" />

+ 1 - 0
composer.json

@@ -36,6 +36,7 @@
         "ext-iconv": "*",
         "sensio/framework-extra-bundle": "^5.1",
         "sensiolabs/security-checker": "^4.1",
+        "symfony/asset": "^4.0",
         "symfony/console": "^4.0",
         "symfony/flex": "^1.0",
         "symfony/framework-bundle": "^4.0",

+ 57 - 1
composer.lock

@@ -4,7 +4,7 @@
         "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
         "This file is @generated automatically"
     ],
-    "content-hash": "45d8142816c016eb652bdb41a5994204",
+    "content-hash": "25a9e093c66ee90666fb93eb83472ce9",
     "packages": [
         {
             "name": "composer/ca-bundle",
@@ -769,6 +769,62 @@
             "description": "A security checker for your composer.lock",
             "time": "2017-10-29T18:48:08+00:00"
         },
+        {
+            "name": "symfony/asset",
+            "version": "v4.0.2",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/symfony/asset.git",
+                "reference": "3f5117a59af74dd3cecf3ebc70d1e35478ae2d01"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/symfony/asset/zipball/3f5117a59af74dd3cecf3ebc70d1e35478ae2d01",
+                "reference": "3f5117a59af74dd3cecf3ebc70d1e35478ae2d01",
+                "shasum": ""
+            },
+            "require": {
+                "php": "^7.1.3"
+            },
+            "require-dev": {
+                "symfony/http-foundation": "~3.4|~4.0",
+                "symfony/http-kernel": "~3.4|~4.0"
+            },
+            "suggest": {
+                "symfony/http-foundation": ""
+            },
+            "type": "library",
+            "extra": {
+                "branch-alias": {
+                    "dev-master": "4.0-dev"
+                }
+            },
+            "autoload": {
+                "psr-4": {
+                    "Symfony\\Component\\Asset\\": ""
+                },
+                "exclude-from-classmap": [
+                    "/Tests/"
+                ]
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "MIT"
+            ],
+            "authors": [
+                {
+                    "name": "Fabien Potencier",
+                    "email": "fabien@symfony.com"
+                },
+                {
+                    "name": "Symfony Community",
+                    "homepage": "https://symfony.com/contributors"
+                }
+            ],
+            "description": "Symfony Asset Component",
+            "homepage": "https://symfony.com",
+            "time": "2017-11-07T14:45:01+00:00"
+        },
         {
             "name": "symfony/cache",
             "version": "v4.0.2",

BIN
public/images/wikimedia.jpeg


+ 6 - 0
public/styles/app.css

@@ -0,0 +1,6 @@
+img.logo {
+  clear: both;
+  float: right;
+  margin: 0 1em 1em;
+  width: 100px;
+}

+ 3 - 0
src/Controller/BlogController.php

@@ -13,16 +13,19 @@ class BlogController extends Controller {
       'id' => '1',
       'title' => 'Title 1',
       'body' => 'Body 1',
+      'authorName' => 'Fred',
       ],
     2 => [
       'id' => '2',
       'title' => 'Title 2',
       'body' => 'Body 2',
+      'authorName' => 'Outi',
     ],
     3 => [
       'id' => '3',
       'title' => 'Title 3',
       'body' => 'Body 3',
+      'authorName' => 'François',
     ],
   ];
 

+ 3 - 0
symfony.lock

@@ -56,6 +56,9 @@
             "ref": "576d653444dade07f272c889d52fe4594caa4fc3"
         }
     },
+    "symfony/asset": {
+        "version": "v4.0.2"
+    },
     "symfony/cache": {
         "version": "v4.0.2"
     },

+ 6 - 3
templates/base.html.twig

@@ -3,14 +3,17 @@
     <head>
         <meta charset="UTF-8">
         <title>{% block title %}Test application{% endblock %}</title>
-        {% block stylesheets %}{% endblock %}
+        {% block stylesheets %}
+        <link rel="stylesheet" href="{{ absolute_url(asset('styles/app.css')) }}" />{% endblock %}
     </head>
     <body>
         <div id="sidebar">
             {% block sidebar %}
+                {# Note: asset() is only available with composer require asset #}
+                <img src="{{ asset('images/wikimedia.jpeg') }}" class="logo" alt="Logo Wikimedia" />
                 <ul>
-                    <li><a href="/">Home</a></li>
-                    <li><a href="/blog">Blog</a></li>
+                    <li><a href="{{ path('front') }}">Home</a></li>
+                    <li><a href="{{ url('blog_list') }}">Blog</a></li>
                 </ul>
             {% endblock %}
         </div>

+ 10 - 2
templates/blog/index.html.twig

@@ -4,8 +4,16 @@
 
 {% block body %}
     {% for entry in blog_entries %}
-        <h2>{{ entry.title }}</h2>
-        <p>{{ entry.body }}</p>
+        {#
+        - Notice the use of multiple possible templates
+        - The parameters passing here would be useless: by default all
+           parameters are transmitted to included templates. But they are needed
+           because we added with_context = false
+         - ignore_missing: don't complain if no matching template is found
+         - sandbox: sanitize used-submitted templates
+         #}
+        {{ include(['blog/post-inline.html.twig', 'blog/post-block.html.twig'], { 'article': entry }, with_context = false) }}
+        {% if not loop.last %}<hr />{% endif %}
     {% endfor %}
 
     <p>{{ pager }}</p>

+ 5 - 0
templates/blog/post-block.html.twig

@@ -0,0 +1,5 @@
+<h2>{{ article.title }}</h2>
+<h3 class="byline">By {{ article.authorName }}</h3>
+<p>
+    {{ article.body }}
+</p>

+ 1 - 1
templates/bundles/WebProfilerBundle/Profiler/base_js.html.twig

@@ -4,7 +4,7 @@
     {# Caution: the contents of this file are processed by Twig before loading
                 them as JavaScript source code. Always use '/*' comments instead
                 of '//' comments to avoid impossible-to-debug side-effects #}
-  alert('This uses the overridden profiler template');
+  console.log('This uses the overridden profiler template');
   Sfjs = (function() {
     "use strict";