Creating our first basic theme

Now that we have reviewed the basics of how a theme is constructed, there is no better time than the present to create our first basic theme. We will begin by creating a theme named twig that we will use to work with exploring how Twig and the Theme system works in Drupal 8.

In order to make sure that we all are working from the same baseline, let's open up the Chapter03/start folder located in the exercise files and select the drupal8.sql database file. We will use this database snapshot to restore our current database instance. Refer to Chapter 1, Setting Up Our Development Environment for instructions on how to perform a database restore.

Now that we all have the same baseline Drupal instance, we can navigate to our Drupal 8 folder using MAC Finder or Windows Explorer and follow these next six steps to create a theme.

Step One – creating a new folder

Create a new folder under our themes folder and call it twig, as shown in the following image:

Step One – creating a new folder

Step two – create an info file

Create a new *.info.yml file named twig.info.yml and add the following configuration information to the file:

name: Twig
type: theme
description: 'A Twig theme for demonstrating TWIG syntax'
core: 8.x
base theme: false

Step three – copy core templates

Copy the html.html.twig and page.html.twig templates from the core/modules/system/templates folder and paste it into our themes/twig folder. Open up page.html.twig in our editor and replace the HTML structure below the comments with the following code:

<h1>Welcome to Twig</h1>
{{ page.content }}

Step four – include a screenshot

Not always a required step but one that will definitely help is including a screenshot that displays or represents our theme within the Appearance admin. In general, we would generate a screenshot based on the finished theme, but because we are just starting out, we can copy an existing one from our exercise files.

Begin by navigating to the Chapter03/end folder and copy the screenshot.png file to our newly created themes/twig folder.

Step five – installing our theme

Next, we will need to install our new theme by navigating to /admin/appearance and locating our new theme named Twig under the Uninstalled themes section. Click on the Install and set as default link to install our new theme, as shown in the following image:

Step five – installing our theme

Step six – Welcome to Twig

We have successfully created our first theme. Although there is not much to it, we can preview what our website looks like by browsing back to the home page of our Drupal instance. We should see our new theme displaying a message of Welcome to Twig, as shown in the following image:

Step six – Welcome to Twig

With our new theme in place, we can begin taking a deeper look into Twig and all of the great features that Drupal 8 introduces to us with this new templating engine.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.188.138