My last article Modernize Your Drupal Theme tackled adding some modern touches to your legacy Drupal themes such as SCSS and Compass. With Drupal 8 on the horizon, Drupal theming is changing in some substantial ways. I’d like to take a look over the course of a few posts at the process of converting a current Drupal 7 theme to work with Drupal 8.
Along with some structural changes to how theme packages are laid out, one of the major changes in Drupal 8 theming is the switch from PHPTemplate to Twig for creating your page templates. At first it sounds scary; especially if you feel like you just finally learned PHP and theming for Drupal 7. The truth is, templating in Twig is no harder than templating in PHPTemplate, in fact the syntax and changes to Drupal’s theming variables is probably some what easier.
In this article I want to look at a simple example; the header portion of Singlebrook’s Drupal Streamline Theme page.tpl.php file:
This is the old page.tpl.php of our Drupal 7 starter theme. It is really generic and we significantly alter it for each project but it gets the job done for our purposes today. We see a typical interspersion of PHP and HTML here. You can make a template a lot simpler than this, and we frequently do. For themes that don’t need the logo, site name, and slogan to be edittable on the backend (and really, unless you are distributing a theme for contrib use, most folks don’t need this). For illustration, I am leaving all this in the gists so that we can see more of the twig variable and function use.
So to summarize what we have above; first we open with an HTML header tag and give it a unique role on line one; then we have a php conditional block that takes care of rendering our site logo if we are using one (in a default Drupal install this is our friendly drop logo) on lines three through seven.
We are making some calls to find out if the variable ‘$logo’ is defined, and if it is we are then printing an anchor tag that links to our home page (again using our Drupal variables), printing our logo, adding some other info like titles and alt tags (making sure to sanitize them and make them available to be translated with the t() function) and closing the conditional with the ‘endif’ php tag.
For a front end themer this can be a pain, understanding some of the ternary operator short tags here isn’t immediately obvious. For the developer who jumps back and forth between front end and back end Drupal work, seeing PHP everywhere tends to make us want to put PHP everywhere (probably places we shouldn’t!).
This covers the basics of how we theme for drupal in Drupal 7, we are using html and interspersing some php to call variables from the system to render them into our layouts. We can do some nice things like call assets from our theme directory using some drupal specific functions for path definitions or some global varibles that the system already tracks for us. In Drupal 8, things are going to be very similar, just with some new syntax and some tweaks to the variables available to us. Here is a gist of the same section from the Drupal 8 port of the same theme:
The first thing you will notice is that there is no PHP here. We start with the same opening header tag and we tackle our friend the logo on lines three through seven again.
Visit website 1001
Visit website 1002
Visit website 1003
Visit website 1004
Visit website 1005
Visit website 1006
Visit website 1007
Visit website 1008
Visit website 1009
Visit website 1010
Visit website 1011
Visit website 1012
Visit website 1013
Visit website 1014
Visit website 1015
Visit website 1016
Visit website 1017
Visit website 1018
Visit website 1019
Visit website 1020
Visit website 1021
Visit website 1022
Visit website 1023
Visit website 1024
Visit website 1025
Visit website 1026
Visit website 1027
Visit website 1028
Visit website 1029
Visit website 1030
Visit website 1031
Visit website 1032
Visit website 1033
Visit website 1034
Visit website 1035
Visit website 1036
Visit website 1037
Visit website 1038
Visit website 1039
Visit website 1040
Visit website 1041
Visit website 1042
Visit website 1043
Visit website 1044
Visit website 1045
Visit website 1046
Visit website 1047
Visit website 1048
Visit website 1049
Visit website 1050
Visit website 1051
Visit website 1052
Visit website 1053
Visit website 1054
Visit website 1055
Visit website 1056
Visit website 1057
Visit website 1058
Visit website 1059
Visit website 1060
Visit website 1061
Visit website 1062
Visit website 1063
Visit website 1064
Visit website 1065
Visit website 1066
Visit website 1067
Visit website 1068
Visit website 1069
Visit website 1070
Visit website 1071
Visit website 1072
Visit website 1073
Visit website 1074
Visit website 1075
Visit website 1076
Visit website 1077
Visit website 1078
Visit website 1079
Visit website 1080
Visit website 1081
Visit website 1082
Visit website 1083
Visit website 1084
Visit website 1085
Visit website 1086
Visit website 1087
Visit website 1088
Visit website 1089
Visit website 1090
Visit website 1091
Visit website 1092
Visit website 1093
Visit website 1094
Visit website 1095
Visit website 1096
Visit website 1097
Visit website 1098
Visit website 1099
Visit website 1100
Visit website 1101
Visit website 1102
Visit website 1103
Visit website 1104
Visit website 1105
Visit website 1106
Visit website 1107
Visit website 1108
Visit website 1109
Visit website 1110
Visit website 1111
Visit website 1112
Visit website 1113
Visit website 1114
Visit website 1115
Visit website 1116
Visit website 1117
Visit website 1118
Visit website 1119
Visit website 1120
Visit website 1121
Visit website 1122
Visit website 1123
Visit website 1124
Visit website 1125
Visit website 1126
Visit website 1127
Visit website 1128
Visit website 1129
Visit website 1130
Visit website 1131
Visit website 1132
Visit website 1133
Visit website 1134
Visit website 1135
Visit website 1136
Visit website 1137
Visit website 1138
Visit website 1139
Visit website 1140
Visit website 1141
Visit website 1142
Visit website 1143
Visit website 1144
Visit website 1145
Visit website 1146
Visit website 1147
Visit website 1148
Visit website 1149
Visit website 1150
Visit website 1151
Visit website 1152
Visit website 1153
Visit website 1154
Visit website 1155
Visit website 1156
Visit website 1157
Visit website 1158
Visit website 1159
Visit website 1160
Visit website 1161
Visit website 1162
Visit website 1163
Visit website 1164
Visit website 1165
Visit website 1166
Visit website 1167
Visit website 1168
Visit website 1169
Visit website 1170
Visit website 1171
Visit website 1172
Visit website 1173
Visit website 1174
Visit website 1175
Visit website 1176
Visit website 1177
Visit website 1178
Visit website 1179
Visit website 1180
Visit website 1181
Visit website 1182
Visit website 1183
Visit website 1184
Visit website 1185
Visit website 1186
Visit website 1187
Visit website 1188
Visit website 1189
Visit website 1190
Visit website 1191
Visit website 1192
Visit website 1193
Visit website 1194
Visit website 1195
Visit website 1196
Visit website 1197
Visit website 1198
Visit website 1199
Visit website 1200
Visit website 1201
Visit website 1202
Visit website 1203
Visit website 1204
Visit website 1205
Visit website 1206
Visit website 1207
Visit website 1208
Visit website 1209
Visit website 1210
Visit website 1211
Visit website 1212
Visit website 1213
Visit website 1214
Visit website 1215
Visit website 1216
Visit website 1217
Visit website 1218
Visit website 1219
Visit website 1220
Visit website 1221
Visit website 1222
Visit website 1223
Visit website 1224
Visit website 1225
Visit website 1226
Visit website 1227
Visit website 1228
Visit website 1229
Visit website 1230
Visit website 1231
Visit website 1232
Visit website 1233
Visit website 1234
Visit website 1235
Visit website 1236
Visit website 1237
Visit website 1238
Visit website 1239
Visit website 1240
Visit website 1241
Visit website 1242
Visit website 1243
Visit website 1244
Visit website 1245
Visit website 1246
Visit website 1247
Visit website 1248
Visit website 1249
Visit website 1250
Visit website 1251
Visit website 1252
Visit website 1253
Visit website 1254
Visit website 1255
Visit website 1256
Visit website 1257
Visit website 1258
Visit website 1259
Visit website 1260
Visit website 1261
Visit website 1262
Visit website 1263
Visit website 1264
Visit website 1265
Visit website 1266
Visit website 1267
Visit website 1268
Visit website 1269
Visit website 1270
Visit website 1271
Visit website 1272
Visit website 1273
Visit website 1274
Visit website 1275
Visit website 1276
Visit website 1277
Visit website 1278
Visit website 1279
Visit website 1280
Visit website 1281
Visit website 1282
Visit website 1283
Visit website 1284
Visit website 1285
Visit website 1286
Visit website 1287
Visit website 1288
Visit website 1289
Visit website 1290
Visit website 1291
Visit website 1292
Visit website 1293
Visit website 1294
Visit website 1295
Visit website 1296
Visit website 1297
Visit website 1298
Visit website 1299
Visit website 1300
Visit website 1301
Visit website 1302
Visit website 1303
Visit website 1304
Visit website 1305
Visit website 1306
Visit website 1307
Visit website 1308
Visit website 1309
Visit website 1310
Visit website 1311
Visit website 1312
Visit website 1313
Visit website 1314
Visit website 1315
Visit website 1316
Visit website 1317
Visit website 1318
Visit website 1319
Visit website 1320
Visit website 1321
Visit website 1322
Visit website 1323
Visit website 1324
Visit website 1325
Visit website 1326
Visit website 1327
Visit website 1328
Visit website 1329
Visit website 1330
Visit website 1331
Visit website 1332
Visit website 1333
Visit website 1334
Visit website 1335
Visit website 1336
Visit website 1337
Visit website 1338
Visit website 1339
Visit website 1340
Visit website 1341
Visit website 1342
Visit website 1343
Visit website 1344
Visit website 1345
Visit website 1346
Visit website 1347
Visit website 1348
Visit website 1349
Visit website 1350
Visit website 1351
Visit website 1352
Visit website 1353
Visit website 1354
Visit website 1355
Visit website 1356
Visit website 1357
Visit website 1358
Visit website 1359
Visit website 1360
Visit website 1361
Visit website 1362
Visit website 1363
Visit website 1364
Visit website 1365
Visit website 1366
Visit website 1367
Visit website 1368
Visit website 1369
Visit website 1370
Visit website 1371
Visit website 1372
Visit website 1373
Visit website 1374
Visit website 1375
Visit website 1376
Visit website 1377
Visit website 1378
Visit website 1379
Visit website 1380
Visit website 1381
Visit website 1382
Visit website 1383
Visit website 1384
Visit website 1385
Visit website 1386
Visit website 1387
Visit website 1388
Visit website 1389
Visit website 1390
Visit website 1391
Visit website 1392
Visit website 1393
Visit website 1394
Visit website 1395
Visit website 1396
Visit website 1397
Visit website 1398
Visit website 1399
Visit website 1400
Visit website 1401
Visit website 1402
Visit website 1403
Visit website 1404
Visit website 1405
Visit website 1406
Visit website 1407
Visit website 1408
Visit website 1409
Visit website 1410
Visit website 1411
Visit website 1412
Visit website 1413
Visit website 1414
Visit website 1415
Visit website 1416
Visit website 1417
Visit website 1418
Visit website 1419
Visit website 1420
Visit website 1421
Visit website 1422
Visit website 1423
Visit website 1424
Visit website 1425
Visit website 1426
Visit website 1427
Visit website 1428
Visit website 1429
Visit website 1430
Visit website 1431
Visit website 1432
Visit website 1433
Visit website 1434
Visit website 1435
Visit website 1436
Visit website 1437
Visit website 1438
Visit website 1439
Visit website 1440
Visit website 1441
Visit website 1442
Visit website 1443
Visit website 1444
Visit website 1445
Visit website 1446
Visit website 1447
Visit website 1448
Visit website 1449
Visit website 1450
Visit website 1451
Visit website 1452
Visit website 1453
Visit website 1454
Visit website 1455
Visit website 1456
Visit website 1457
Visit website 1458
Visit website 1459
Visit website 1460
Visit website 1461
Visit website 1462
Visit website 1463
Visit website 1464
Visit website 1465
Visit website 1466
Visit website 1467
Visit website 1468
Visit website 1469
Visit website 1470
Visit website 1471
Visit website 1472
Visit website 1473
Visit website 1474
Visit website 1475
Visit website 1476
Visit website 1477
Visit website 1478
Visit website 1479
Visit website 1480
Visit website 1481
Visit website 1482
Visit website 1483
Visit website 1484
Visit website 1485
Visit website 1486
Visit website 1487
Visit website 1488
Visit website 1489
Visit website 1490
Visit website 1491
Visit website 1492
Visit website 1493
Visit website 1494
Visit website 1495
Visit website 1496
Visit website 1497
Visit website 1498
Visit website 1499
Visit website 1500