Uncategorized

Let’s Debug Twig in Drupal 8!

Turn On Twig Debugging

When I am theming a Drupal site, I need to know which variables are available on a template file. In Drupal 8, the template engine is Twig, so we’re going to need to know a little bit of Twig to make this work. So, if Twig is totally new to you, don’t worry. Today, you’ll learn some Twig!

Update and important note! The following instructions work in the alpha version of Drupal 8 only. In the latest development release, the twig_debug settings are no longer in settings.php. It’s in sites/example.settings.local.php and the suggestion is to take that file and copy it into sites/default. (Link to change record.) Thanks to Cottser for the heads-up!

The first thing we need to do is to turn on twig debugging. We’ll need to open up sites/default/settings.php to do that. In settings.php, after searching for “Twig” I found the following section on Twig debugging:

/**
 * Twig debugging:
 *
 * When debugging is enabled:
 * - The markup of each Twig template is surrounded by HTML comments that
 *   contain theming information, such as template file name suggestions.
 * - Note that this debugging markup will cause automated tests that directly
 *   check rendered HTML to fail. When running automated tests, 'twig_debug'
 *   should be set to FALSE.
 * - The dump() function can be used in Twig templates to output information
 *   about template variables.
 * - Twig templates are automatically recompiled whenever the source code
 *   changes (see twig_auto_reload below).
 *
 * Note: changes to this setting will only take effect once the cache is
 * cleared.
 *
 * For more information about debugging Twig templates, see
 * <a href="http://drupal.org/node/1906392">http://drupal.org/node/1906392</a>.
 *
 * Not recommended in production environments (Default: FALSE).
 */
# $settings['twig_debug'] = TRUE;

Un-comment* the line that contains the twig_debug setting so that…

# $settings['twig_debug'] = TRUE;

becomes

$settings['twig_debug'] = TRUE;

* Remove the pound (#) sign at the beginning of the line. A pound sign (#) enables you to comment out part of a line in Twig. See Comments in Twig for Template Designers.

Clear That Cache

Now I need to clear the cache. On my Drupal 8 site, I’ll navigate to Configuration > Performance and click the Clear all caches button. While I’m at it, I’ll add this page to my shortcut menu by clicking the star next to the page title, because I haven’t got Drush updated yet. (TODO: Update Drush to a version that supports Drupal 8 on your local.)

Hack Core (Really)

In this post, I just want to focus on debugging Twig in a local sandbox environment, so instead of spinning up a new theme, I am going to hack core in this tutorial. (I did cringe a little bit at first, if that helps.)

By “hacking core” what I mean is: I’m going to navigate to core/themes/bartik/templates and open up page.html.twig file in my IDE, phpStorm. (But, you can use whatever text editor or IDE you like, preferable one with some Twig support, like autocompleting all those double curly braces. See Twig for Template Designers for a list of IDEs that support Twig.)

Opening core/templates/Bartik/templates/page.html.twig, I can now add my debugging function…but which one? I can’t just drop in a php var_dump into a template file anymore.

 

Visit website 1
Visit website 2
Visit website 3
Visit website 4
Visit website 5
Visit website 6
Visit website 7
Visit website 8
Visit website 9
Visit website 10
Visit website 11
Visit website 12
Visit website 13
Visit website 14
Visit website 15
Visit website 16
Visit website 17
Visit website 18
Visit website 19
Visit website 20
Visit website 21
Visit website 22
Visit website 23
Visit website 24
Visit website 25
Visit website 26
Visit website 27
Visit website 28
Visit website 29
Visit website 30
Visit website 31
Visit website 32
Visit website 33
Visit website 34
Visit website 35
Visit website 36
Visit website 37
Visit website 38
Visit website 39
Visit website 40
Visit website 41
Visit website 42
Visit website 43
Visit website 44
Visit website 45
Visit website 46
Visit website 47
Visit website 48
Visit website 49
Visit website 50
Visit website 51
Visit website 52
Visit website 53
Visit website 54
Visit website 55
Visit website 56
Visit website 57
Visit website 58
Visit website 59
Visit website 60
Visit website 61
Visit website 62
Visit website 63
Visit website 64
Visit website 65
Visit website 66
Visit website 67
Visit website 68
Visit website 69
Visit website 70
Visit website 71
Visit website 72
Visit website 73
Visit website 74
Visit website 75
Visit website 76
Visit website 77
Visit website 78
Visit website 79
Visit website 80
Visit website 81
Visit website 82
Visit website 83
Visit website 84
Visit website 85
Visit website 86
Visit website 87
Visit website 88
Visit website 89
Visit website 90
Visit website 91
Visit website 92
Visit website 93
Visit website 94
Visit website 95
Visit website 96
Visit website 97
Visit website 98
Visit website 99
Visit website 100
Visit website 101
Visit website 102
Visit website 103
Visit website 104
Visit website 105
Visit website 106
Visit website 107
Visit website 108
Visit website 109
Visit website 110
Visit website 111
Visit website 112
Visit website 113
Visit website 114
Visit website 115
Visit website 116
Visit website 117
Visit website 118
Visit website 119
Visit website 120
Visit website 121
Visit website 122
Visit website 123
Visit website 124
Visit website 125
Visit website 126
Visit website 127
Visit website 128
Visit website 129
Visit website 130
Visit website 131
Visit website 132
Visit website 133
Visit website 134
Visit website 135
Visit website 136
Visit website 137
Visit website 138
Visit website 139
Visit website 140
Visit website 141
Visit website 142
Visit website 143
Visit website 144
Visit website 145
Visit website 146
Visit website 147
Visit website 148
Visit website 149
Visit website 150
Visit website 151
Visit website 152
Visit website 153
Visit website 154
Visit website 155
Visit website 156
Visit website 157
Visit website 158
Visit website 159
Visit website 160
Visit website 161
Visit website 162
Visit website 163
Visit website 164
Visit website 165
Visit website 166
Visit website 167
Visit website 168
Visit website 169
Visit website 170
Visit website 171
Visit website 172
Visit website 173
Visit website 174
Visit website 175
Visit website 176
Visit website 177
Visit website 178
Visit website 179
Visit website 180
Visit website 181
Visit website 182
Visit website 183
Visit website 184
Visit website 185
Visit website 186
Visit website 187
Visit website 188
Visit website 189
Visit website 190
Visit website 191
Visit website 192
Visit website 193
Visit website 194
Visit website 195
Visit website 196
Visit website 197
Visit website 198
Visit website 199
Visit website 200
Visit website 201
Visit website 202
Visit website 203
Visit website 204
Visit website 205
Visit website 206
Visit website 207
Visit website 208
Visit website 209
Visit website 210
Visit website 211
Visit website 212
Visit website 213
Visit website 214
Visit website 215
Visit website 216
Visit website 217
Visit website 218
Visit website 219
Visit website 220
Visit website 221
Visit website 222
Visit website 223
Visit website 224
Visit website 225
Visit website 226
Visit website 227
Visit website 228
Visit website 229
Visit website 230
Visit website 231
Visit website 232
Visit website 233
Visit website 234
Visit website 235
Visit website 236
Visit website 237
Visit website 238
Visit website 239
Visit website 240
Visit website 241
Visit website 242
Visit website 243
Visit website 244
Visit website 245
Visit website 246
Visit website 247
Visit website 248
Visit website 249
Visit website 250
Visit website 251
Visit website 252
Visit website 253
Visit website 254
Visit website 255
Visit website 256
Visit website 257
Visit website 258
Visit website 259
Visit website 260
Visit website 261
Visit website 262
Visit website 263
Visit website 264
Visit website 265
Visit website 266
Visit website 267
Visit website 268
Visit website 269
Visit website 270
Visit website 271
Visit website 272
Visit website 273
Visit website 274
Visit website 275
Visit website 276
Visit website 277
Visit website 278
Visit website 279
Visit website 280
Visit website 281
Visit website 282
Visit website 283
Visit website 284
Visit website 285
Visit website 286
Visit website 287
Visit website 288
Visit website 289
Visit website 290
Visit website 291
Visit website 292
Visit website 293
Visit website 294
Visit website 295
Visit website 296
Visit website 297
Visit website 298
Visit website 299
Visit website 300
Visit website 301
Visit website 302
Visit website 303
Visit website 304
Visit website 305
Visit website 306
Visit website 307
Visit website 308
Visit website 309
Visit website 310
Visit website 311
Visit website 312
Visit website 313
Visit website 314
Visit website 315
Visit website 316
Visit website 317
Visit website 318
Visit website 319
Visit website 320
Visit website 321
Visit website 322
Visit website 323
Visit website 324
Visit website 325
Visit website 326
Visit website 327
Visit website 328
Visit website 329
Visit website 330
Visit website 331
Visit website 332
Visit website 333
Visit website 334
Visit website 335
Visit website 336
Visit website 337
Visit website 338
Visit website 339
Visit website 340
Visit website 341
Visit website 342
Visit website 343
Visit website 344
Visit website 345
Visit website 346
Visit website 347
Visit website 348
Visit website 349
Visit website 350
Visit website 351
Visit website 352
Visit website 353
Visit website 354
Visit website 355
Visit website 356
Visit website 357
Visit website 358
Visit website 359
Visit website 360
Visit website 361
Visit website 362
Visit website 363
Visit website 364
Visit website 365
Visit website 366
Visit website 367
Visit website 368
Visit website 369
Visit website 370
Visit website 371
Visit website 372
Visit website 373
Visit website 374
Visit website 375
Visit website 376
Visit website 377
Visit website 378
Visit website 379
Visit website 380
Visit website 381
Visit website 382
Visit website 383
Visit website 384
Visit website 385
Visit website 386
Visit website 387
Visit website 388
Visit website 389
Visit website 390
Visit website 391
Visit website 392
Visit website 393
Visit website 394
Visit website 395
Visit website 396
Visit website 397
Visit website 398
Visit website 399
Visit website 400
Visit website 401
Visit website 402
Visit website 403
Visit website 404
Visit website 405
Visit website 406
Visit website 407
Visit website 408
Visit website 409
Visit website 410
Visit website 411
Visit website 412
Visit website 413
Visit website 414
Visit website 415
Visit website 416
Visit website 417
Visit website 418
Visit website 419
Visit website 420
Visit website 421
Visit website 422
Visit website 423
Visit website 424
Visit website 425
Visit website 426
Visit website 427
Visit website 428
Visit website 429
Visit website 430
Visit website 431
Visit website 432
Visit website 433
Visit website 434
Visit website 435
Visit website 436
Visit website 437
Visit website 438
Visit website 439
Visit website 440
Visit website 441
Visit website 442
Visit website 443
Visit website 444
Visit website 445
Visit website 446
Visit website 447
Visit website 448
Visit website 449
Visit website 450
Visit website 451
Visit website 452
Visit website 453
Visit website 454
Visit website 455
Visit website 456
Visit website 457
Visit website 458
Visit website 459
Visit website 460
Visit website 461
Visit website 462
Visit website 463
Visit website 464
Visit website 465
Visit website 466
Visit website 467
Visit website 468
Visit website 469
Visit website 470
Visit website 471
Visit website 472
Visit website 473
Visit website 474
Visit website 475
Visit website 476
Visit website 477
Visit website 478
Visit website 479
Visit website 480
Visit website 481
Visit website 482
Visit website 483
Visit website 484
Visit website 485
Visit website 486
Visit website 487
Visit website 488
Visit website 489
Visit website 490
Visit website 491
Visit website 492
Visit website 493
Visit website 494
Visit website 495
Visit website 496
Visit website 497
Visit website 498
Visit website 499
Visit website 500

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button