Skip to content

josh-clarke/nunjucks-web-starter-files

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Nunjucks Web Starter Files

New Features in v3.0+

  • Gulp 4 (alpha) with an ES6 Babel gulpfile
  • Config file for paths gulp-config.js
  • Uses Yarn instead of Bower+NPM
  • Uses gulp-include directives to bundle all scripts
  • Relies on binaries from the local project's ./node_modules/bin instead of globals (except Yarn)

These starter files can be used to generate a prototype or flat-file website. The Nunjucks template language is used for the HTML due to its close relationship to Twig.

After using this starter kit to build flat HTML prototypes, the Nunjucks files can then be re-used as a starter point for Twig-based templates such as Wordpress with Timber or Craft CMS, and any platforms which can use Nunjucks or Jinja.

Open Issues Project Forks GitHub Stars GitHub license

Requirements:

Getting Started

  1. Fork and clone repository or download the ZIP file
  2. From the terminal inside the project folder, run yarn
  3. Run gulp to build and watch for changes

Features

HTML

  • Nunjucks template language (nearly equivalent to Twig), compiles to flat HTML
    • Extend/Include templates, Variables, If/Else logic and more
  • Starter template based on HTML5 Boilerplate

CSS

JavaScript

  • Automatically includes and concatenates using gulp-include
    • jQuery included by default
    • IE Object Fit polyfill included
    • Console log error prevention script included

JavaScript files are included using //=require or //=include directives within the script file:

//=require plugins/prevent-console-errors.js
//=require ../../../node_modules/jquery/dist/jquery.js
  • Compresses JS files with Uglify
  • No linter—add your own if needed
  • Sourcemaps is available but not configured for JavaScript

Images

  • Images placed in ./src/assets/images/ are optimized by default with imagemin
    • Place directly in ./dist/assets/images/ if optimization is not needed or undesired

Templating

The best way to template with Nunjucks, a Twig-like template language, is to extend from a base template. Base templates or partials should be prefixed with an underscore _ to prevent them from compiling into their own standalone templates. For example:

Base Template (Simplified): _base.njk

<!DOCTYPE html>
<html>
  <head><title>{{ title }}</title></head>
  <body>
    {% block content %}
      <p>Fallback content</p>
    {% endblock content %}
  </body>
</html>

Extended Template: index.njk

{% extends "_base.njk" %}

{% set title = "Hello World" %}

{% block content %}
  <h1>Hello World!</h1>
  <p>You've reached my site!</p>
{% endblock content %}

Outputs (Simplified): index.html

<!DOCTYPE html>
<html>
  <head><title>Hello World</title></head>
  <body>
    <h1>Hello World!</h1>
    <p>You've reached my site!</p>
  </body>
</html>

You can also include partials with a simple include line:

<header>
  {% include "_partials/_hero-slider.njk" %}
</header>

For more advanced techniques, check out the Nunjucks templating documentation.

Default Setup

By default, the following setup is used:

  • Working and Production directories (change in gulp-config.js)
    • src - Working directory
    • dist - Production directory
      • This directory and its subfolders will not be created until the first gulp build command
    • assets - Assets directory for images, CSS, scripts
    • templates - Templates directory for Nunjucks files
  • Gulp
    • gulp command for default build with BrowserSync preview, also starts watching files
    • gulp watch command to watch files for changes
  • HTML/Nunjucks
    • Build templates in ./src/templates/ using either the .njk, .nunjucks, .njk.html or .html extension
    • Name partials and base extends with a leading underscore _
    • Builds to ./dist/
  • CSS/SASS
    • Write SCSS or SASS in ./src/assets/sass/
      • main.scss - Import all partials in here
      • base/ - Base styles
    • Name partials with a leading underscore _
    • Concatenates to ./dist/assets/css/main.css
    • Note: Use yarn add <module> --dev to maintain SASS libraries and link to them directly
      • Example: @import '../../../node_modules/modularscale-sass/stylesheets/modularscale';
  • JS
    • User script file in ./src/assets/js/scripts.js uses gulp-include directives to set bundle order of additional scripts
    • Place additional scripts in plugins/
    • Note: Use yarn add <module> --dev to maintain JS libraries and link to them directly
      • Example: //=require ../../../node_modules/jquery/dist/jquery.js
  • Images
    • Place images for processing in ./src/assets/images/
    • Runs imagemin default optimization (change in gulpfile.babel.js)
    • Outputs to ./dist/assets/images/

Working File Layout

./
 └ dist/                # distribution folder
    └ assets
      └ css/      
        └ main.css      # main CSS file
      └ images/   
      └ js/
          └ scripts.js  # bundled scripts
 └ src/                 # working folder
    └ assets
      └ images/         # images to be optimized
      └ js/
          └ scripts.js  # use this for custom scripts
          └ plugins/    # use for non-package.json scripts
      └ scss/
          └ main.scss   # use this to include partials
          └ base/        
              └ _breakpoint-query.scss
              └ _buttons.scss
              └ _normalize.scss
              └ _scaffolding.scss
              └ _typography.scss
              └ _variables.scss
     └ templates/       # use for HTML/Nunjucks templates
        └ _base.njk     # base template
        └ index.njk     # index.html, extends _base.njk

Saving New Components

Use the following commands to add modules or libraries to package.json. When you clone the repo to a new directory or machine, just run the yarn add command. Yarn uses the same repositories as npm, and locks the version.

  • New project modules/libraries can be added with yarn add <package-name>
  • New build modules/libraries (such as gulp plugins) can be added with yarn add <package-name> --dev
  • If you want to shift all packages to their latest versions, you can do yarn upgrade. Do not do this unless you know what you are doing, as it could break your project.

Thanks and Credits

Inspired by:

MIT

Released under an MIT license.

About

Gulp-based website starter files with Nunjucks templating.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published