danburzo / lathe

A Timber-based WordPress starter theme
https://danburzo.ro/projects/lathe/
MIT License
17 stars 0 forks source link
advanced-custom-fields timber timber-theme twig wordpress-starter-theme

Lathe

A Timber-based WordPress starter theme.

Since it's written around it, Lathe requires the Timber plugin to work. It also adds some neat functionality when you have Advanced Custom Fields (the free version) or ACF Pro installed.

Here's what you get out of the box:

Table of contents

Guides

Getting started

Lathe is designed as a starting point for developing your theme. To get started:

Run npm install in your theme folder to install all the dependencies.

When you add the theme to your WordPress installation, make sure install the Timber plugin and, optionally, a version of the ACF plugin.

The Timber docs is a handy reference for developing your theme.

Local development environment

Lathe uses wp-now for local development. The npm run start script launches a fully-functional WordPress environment with the theme pre-installed at http://localhost:8081.

Static assets bundling

This theme is set up to process CSS, JavaScript, and other static assets with esbuild.

There are a couple of npm scripts available:

The bundles are automatically generated in the build/ folder. If you change these files by hand, they risk being overwritten!

Assets Manifest

The set of files to process is defined in the assets.txt file, located in the theme's root folder. Here's an example:

assets.txt

# Front-end scripts
static/index.js

# Stylesheets
style.css

The format is simple: you include one file per line, and lines starting with # are considered to be comments and thus ingored by the processor.

In your theme code, use the asset() Twig function to include any of these assets on the pages that need them. For the two assets included in our example manifest, the equivalent Twig code to include them is:

templates/my-template.twig

{{ asset('static/index.js', true) }}
{{ asset('style.css', true) }}

When the page gets rendered, you'll see:

<link
    rel="stylesheet"
    id="style.css-css"
    type="text/css"
    media="all"
    href="http://example.com/wp-content/themes/lathe/build/style.281d1dd0.css?ver=5.2.2"
/>

<script
    type="text/javascript"
    src="http://example.com/wp-content/themes/lathe/build/index.117076fb.js?ver=5.2.2"
></script>

Notice that the paths to the bundled assets contain hashes — sequences of alphanumeric characters that help deal with the browser cache. Whenever you make a change to a file, its gets a new hash.

Custom post types and taxonomies

Custom Post Types (CPT) and custom taxonomies are ideally handled in dedicated plugins rather than in the theme. Lathe includes sample code for a CPT and a taxonomy in the wp-plugins folder.

The wp-plugins folder should be excluded when syncing theme files to the server. Instead, its content should be copied separately to the wp-content/plugins/ folder.

Reference

Twig functions

The asset() function

A helper function to reference bundled assets in your theme. See Static assets bundling for more details.

{{ asset($handle, $enqueue = false) }}

Options:

$handle: The identifier for the asset, which is the path you referenced in your Assets Manifest. This will normally be the path to the asset relative to the root of your theme.

$enqueue defines how the asset should be included:

Twig filters

The asset filter

You can call the asset() function as a filter, too. These are equivalent:

{# As a function... #}
{{ asset('style.css', true) }}

{# ...or as a filter #}
{{ 'style.css' | asset(true) }}

The size filter

Timber already has a lot of flexibility in dealing with images. This theme adds the size filter, allowing you resize images from a set of predefined sizes.

Usage:

{% if post.thumbnail %}
    <img src='{{ Image(post.thumbnail).src | size("thumbnail") }}'/>
{% endif %}

The static $image_sizes definition in function.php lets you configure the predefined sizes.

Note: These are different from the Media sizes you can configure from the WordPress admin UI.

Related projects

This theme is inspired by Timber's own starter theme and borrows some tricks from Skela by Upstatement.

Contributing

Contributions are welcome. Please open a GitHub issue before submitting a pull request, unless the changes are straightforward.