From dede66936e814b7c0ef133b38ea1389211ff6b89 Mon Sep 17 00:00:00 2001 From: Tom Johnson Date: Tue, 4 Apr 2017 15:37:33 -0700 Subject: [PATCH] New tutorial: Convert an HTML site to Jekyll (#5881) Merge pull request 5881 --- docs/_data/tutorials.yml | 1 + docs/_tutorials/create-jekyll-theme.md | 490 +++++++++++++++++++++++++ docs/img/jekylllayoutconcept.png | Bin 0 -> 34133 bytes 3 files changed, 491 insertions(+) create mode 100644 docs/_tutorials/create-jekyll-theme.md create mode 100644 docs/img/jekylllayoutconcept.png diff --git a/docs/_data/tutorials.yml b/docs/_data/tutorials.yml index 809c7e55..baf77896 100644 --- a/docs/_data/tutorials.yml +++ b/docs/_data/tutorials.yml @@ -4,6 +4,7 @@ - navigation - orderofinterpretation - custom-404-page + - create-jekyll-theme #- title: Another section # tutorials: diff --git a/docs/_tutorials/create-jekyll-theme.md b/docs/_tutorials/create-jekyll-theme.md new file mode 100644 index 00000000..348ff31a --- /dev/null +++ b/docs/_tutorials/create-jekyll-theme.md @@ -0,0 +1,490 @@ +--- +layout: tutorials +permalink: /tutorials/create-jekyll-theme/ +title: Create your first Jekyll theme +--- + +If you're looking for themes for your Jekyll site, you don't have to restrict yourself to existing Jekyll themes. It's pretty easy to convert almost any static HTML site into a Jekyll theme. + +In many ways, any site that is currently a static site *already* is a Jekyll site. Jekyll just allows you to automate parts of the site (like inserting pages into templates, rendering lists for navigation, generating feeds and sitemaps, and more) as it processes the files. + +Understanding how to convert any HTML site into a Jekyll website will open your world to many more options for Jekyll themes. Instead of searching online for "Jekyll themes," you can choose from the large variety of HTML templates for your site, quickly Jekyll-ize the HTML template as you need to, and build the output with Jekyll. + +Although websites can have sophisticated features and controls, we'll keep things simple in this tutorial. This tutorial contains the following sections: + +* TOC +{:toc} + +## Understand a basic Jekyll site + +First, let's start with a grounding in the basics. Stripping a Jekyll site down to an extremely basic level will help clarify what happens in a Jekyll site. If you haven't already installed the jekyll gem, [install it]({% link _docs/installation.md %}). + +A simple Jekyll site might consist of just 3 files: + +``` +├── _config.yaml +├── _layouts +│   └── default.html +└── index.md +``` + +Manually create these 3 files in a folder called `myjekyllsite`. (Put `default.html` inside a folder called `_layouts`.) Then populate the content of the `default.html` and `index.md` files as follows: + +**_layouts/default.html** + +```html + + + {% raw %}{{ content }}{% endraw %} + + +``` + +**index.md** + +```yaml +--- +title: My page +layout: default.html +--- + +Some **bold** content. +``` + +Now `cd` to `myjekyllsite` and build the site: + +``` +jekyll serve +``` + +When you build the site, you get a preview URL such as `http://127.0.0.1:4001/`. The site's files are built in the `_site` folder. + +This is a Jekyll site at the most basic level. Here's what is happening: + +* The `_config.yml` file contains settings that Jekyll uses as it processes your site. An empty config file will use default values for building a Jekyll site. For example, to convert Markdown to HTML, Jekyll will automatically use the [kramdown Markdown filter](https://rubygems.org/gems/kramdown/), without any need to specify it. +* Jekyll looks for files with [front matter tags]({% link _docs/frontmatter.md %}) (the two sets of dashed lines `---` like those in `index.md`) and processes the files (populating site variables, rendering any [Liquid](https://shopify.github.io/liquid/), and converting Markdown to HTML). +* Jekyll pushes the content from all pages and posts into the `{% raw %}{{ content }}{% endraw %}` tags in the layout specified (`default`) in the front matter tags. +* The processed files get written as `.html` files in the `_site` directory. + +You can read more about how Jekyll processes the files in [Order of Interpretation](/tutorials/orderofinterpretation/). + +With this basic foundation of how a Jekyll site works, you can convert almost any HTML theme into a Jekyll site. The following sections will take you through a step-by-step tutorial on converting an HTML template into a Jekyll site. + +## 1. Create a template for your default layout + +Find your HTML theme and save it as a default layout. If you're converting or cloning an existing site, you can right-click the page and view the source code. + +For example, suppose you're cloning your company site to create a documentation site with the same branding. Or suppose you have a personal site that you built with HTML and now want to make it a Jekyll theme. Get the HTML source code for your site. + +{: .note .info} +**Note:** Regardless of the site, check the license and make sure you have permission to copy the code. + +Copy and paste the source code into a file called `default.html`. Put the `default.html` file inside a folder called `_layouts`. This will be the default layout template for your pages and posts — that is, each page or post will use this layout when Jekyll builds the site. + +Note that in looking for templates, you want the HTML output of the template. If the template has PHP tags or other dynamic scripts, these dynamic elements will need to be converted to HTML or to [Liquid](https://shopify.github.io/liquid/) scripting where possible. Jekyll uses Liquid in its templating system to retrieve dynamic content. + +Open `default.html` into your browser locally to ensure the site looks and functions like it does online. You will likely need to adjust CSS, JS, and image paths so they work. + +For example, if the paths were relative on the site you copied, you'll need to either download the same assets into your Jekyll site or use absolute paths to the same assets in the cloud. (Syntax such as `src="//` requires a prefix such as `src="http://` to work in your local browser.) + +Jekyll provides some [filters](/docs/templates/#filters) to prepend a site URL before path. For example, you could preface your stylesheet like this: + +```liquid +{% raw %}{{ "/assets/style.css" | relative_url }}{% endraw %} +``` + +The `relative_url` filter will prepend the `baseurl` value from your config file to the input. This is useful if your site is hosted at a subpath rather than the root of the domain (for example, `http://mysite.com/blog/`). + +You can also use an `absolute_url` filter. This filter will prepend the `url` *and* `baseurl` value to the input: + +```liquid +{% raw %}{{ "/assets/style.css" | absolute_url }}{% endraw %} +``` + +Again, both `url` and `baseurl` must be defined in your site's config file, like this: + +``` +url: http://mysite.com +baseurl: /blog +``` + +The result in the output will be `http://mysite.com/blog/assets/style.css`. + +Note that the `url` property of any page begins with a forward slash (`/`), so omit this at the end of your `url` or `baseurl` property. + +You don't have to prepend filters to link paths like this. You could also use relative links across your entire site. However you decide to code the paths to your assets, make sure they render correctly. + +Does your local `default.html` page look good in your browser? Are all images, styles, and other elements showing up correctly? If so, great. Keep going. You'll use this template as the layout for all your pages and posts. + +In the next section, you'll blank out the content of the layout and replace it with placeholder tags that get populated dynamically with your Jekyll pages. + +## 2. Identify the content part of the layout + +In `default.html`, find where the page content begins (usually at `h1` or `h2` tags). Replace the title that appears inside these tags with `{% raw %}{{ page.title }}{% endraw %}`. + +Remove the page content (but not code from the top nav, sidebar, or footer) and replace the page content with `{% raw %}{{ content }}{% endraw %}`. + +Check the layout again in your browser and make sure you didn't corrupt or alter it up by inadvertently removing a crucial `div` tag or other element. The only change should be to the title and page content, which are now blanked out or showing the placeholder tag. + +## 3. Create a couple of files with front matter tags + +Create a couple of files (`index.md` and `about.md`) in your root directory. + +In your `index.md` file, add some front matter tags containing a `title` and `layout` property, like this: + +```yaml +--- +title: Home +layout: default +--- + +Some page content here... +``` + +Create another page for testing called `about.md` with similar front matter tags. + +{: .note .info} +**Note:** If you don't specify a layout in your pages, Jekyll will automatically use the template labeled `default`. We specify it here only to make it explicit what's happening. + +## 4. Add a configuration file + +Add a `_config.yml` file in your root directory. In `_config.yml`, you can optionally specify the markdown filter you want. By default, [kramdown](https://kramdown.gettalong.org/)) is used (without the need to specify it). If no other filter is specified, your config file will automatically apply the following as a default setting: + +``` +markdown: kramdown +``` + +You can also specify [some options](https://kramdown.gettalong.org/converter/html.html) for kramdown to make it behave more like Github-flavored Markdown: + +``` +kramdown: + input: GFM + auto_ids: true + hard_wrap: false + syntax_highlighter: rouge +``` + +## 5. Test your pages + +Now run `jekyll serve` and toggle between your `index.html` and `about.html` pages. The default layout should load for both pages. + +You've now extracted your content out into separate files and defined a common layout for pages. + +You could define any number of layouts you want for pages. Then just identify the layout you want that particular page to use. For example: + +``` +--- +title: Sample page +layout: homepage +--- +``` + +This page would then use the `homepage.html` template in the `_layouts` folder. + +You can even set [default front matter tags](/docs/configuration/#front-matter-defaults) for pages, posts, or [collections]({% link _docs/collections.md %}) in your `_config.yml` file so that you don't have to specify the layout in your front matter tags. However, setting defaults is more advanced than this basic tutorial will cover. + +## 6. Configure site variables + +You already configured the page title using `{% raw %}{{ page.title }}{% endraw %}` tags. But there are more `title` tags to populate. Pages also have a [`title`](https://moz.com/learn/seo/title-tag) tag that appears in the browser tab or window. Typically you put the page title followed by the site title here. + +In your `default.html` layout, look for the `title` tags below your `head` tags: + +``` +ACME Website +``` + +Insert the following site variables: + +``` +{% raw %}{{ page.title }} | {{ site.title }}{% endraw %} +``` + +Open `_config.yml` and add a `title` property for your site's name. + +``` +title: ACME Website +``` + +Any properties you add in your `_config.yml` file are accessible through the `site` namespace. Similarly, any properties in your page's front matter are accessible through the `page` namespace. Use dot notation after `site` or `page` to access the value. + +Stop your Jekyll server (**Ctrl + C**) and restart it. Verify that the `title` tags are populating correctly. + +{: .note .info} +**Note:** Every time you modify your config file, you have to restart Jekyll for the changes to take effect. When you modify other files, Jekyll automatically picks up the changes when it rebuilds. + +If you have other variables to populate in your site, do so following this same pattern. + +## 7. Show posts on a page + +It's common to show a list of posts on the homepage. First, let's create some posts so that our loop will have something to display. + +Add some posts in a `_posts` folder following the standard `YYYY-MM-DD-title.md` post format: + +* `2017-01-02-my-first-post.md` +* `2017-01-15-my-second-post.md` +* `2017-02-08-my-third-post.md` + +In each post, add some basic content: + +``` +--- +title: My First Post +layout: default +--- + +Some sample content... +``` + +Now let's create a layout that will display the posts. Create a new file in `_layouts` called `home.html`. In your `home.html` layout, add the following logic: + +``` +--- +layout: default +--- + +{% raw %}{% endraw %} +``` + +Create a file called `blog.md` in your root directory and specify the `home` layout: + +``` +--- +title: Blog +layout: home +--- +``` + +In this case, `home.md` will be pushed into the `{% raw %}{{ content }}{% endraw %}` tags in the `home` layout. Then the `home` layout will be pushed into the `{% raw %}{{ content }}{% endraw %}` tags of the `default` layout. + +### How layouts work + +When a layout specifies another layout, it means the content of the first layout will be stuffed into the `{% raw %}{{ content }}{% endraw %}` tag of the second layout. As an analogy, think of Russian dolls that fit into each other. Each layout fits into another layout that it specifies. + +The following diagram shows how layouts work in Jekyll: + +Concept of Jekyll layouts + +In this case, the content from a page that specifies the layout `page` gets pushed into the `{% raw %}{{ content }}{% endraw %}` tag of the layout file named `page`. Because the `page` layout itself specifies another layout (`docs`), the content from `page` gets pushed into the `{% raw %}{{ content }}{% endraw %}` tag in the `docs` layout. Because the `docs` layout specifies another layout (`default`), the content from docs gets pushed into the `{% raw %}{{ content }}{% endraw %}` tag of the `default` layout. + +You don't need multiple layouts. You could just use one: `default`. You have options for how you design your site. In general, it's common to define one layout for pages and another layout for posts, but for both of these layouts to inherit the `default` template (which usually defines the top and bottom parts of the site). + +In your browser, go to `home.html` and see the list of posts. (Note that you didn't have to use the method described here. You could have simply added the `for` loop to any page, such as `index.md`, to display these posts. But given that you may have more complex logic for other features, it can be helpful to store your logic in templates separate from the page area where you frequently type your content.) + +### For loops + +By the way, let's pause here to look at the `for` loop logic a little more closely. [For loops in Liquid](https://help.shopify.com/themes/liquid/tags/iteration-tags#for) are one of the most commonly used Liquid tags. For loops let you iterate through content in your Jekyll site and build out a result. The `for` loop also has [certain properties available](https://help.shopify.com/themes/liquid/objects/for-loops) (like first or last iteration) based on the loop's position in the loop as well. + +We've only scratched the surface of what you can do with `for` loops in retrieving posts. For example, if you wanted to display posts from a specific category, you could do so by adding a `categories` property to your post's front matter and then look in those categories. Further, you could limit the number of results by adding a `limit` property. Here's an example: + +```liquid +{% raw %}