Skip to content

Commit 90d2b7d

Browse files
committed
feat: set up Vite HMR
0 parents  commit 90d2b7d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+1695
-0
lines changed

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/composer.lock
2+
/vendor/
3+
/wordpress/
4+
node_modules/
5+
dist/

404.php

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
/**
3+
* The template for displaying 404 pages (Not Found)
4+
*
5+
* Methods for TimberHelper can be found in the /functions sub-directory
6+
*
7+
* @package WordPress
8+
* @subpackage Timber
9+
* @since Timber 0.1
10+
*/
11+
12+
$context = Timber::context();
13+
Timber::render( '404.twig', $context );

LICENSE

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright (c) 2012-2013 Jared Novack
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# The Timber Starter Theme
2+
3+
[![Build Status](https://travis-ci.com/timber/starter-theme.svg?branch=master)](https://travis-ci.com/github/timber/starter-theme)
4+
[![Packagist Version](https://img.shields.io/packagist/v/upstatement/timber-starter-theme?include_prereleases)](https://packagist.org/packages/upstatement/timber-starter-theme)
5+
6+
The "_s" for Timber: a dead-simple theme that you can build from. The primary purpose of this theme is to provide a file structure rather than a framework for markup or styles. Configure your SASS files, scripts, and task runners however you would like!
7+
8+
## Installing the theme
9+
10+
Follow the guide on [how to Install Timber using the Starter Theme](https://timber.github.io/docs/v2/installation/installation/#use-the-starter-theme).
11+
12+
Then,
13+
14+
1. Rename the theme folder to something that makes sense for your website. You could keep the name `timber-starter-theme` but the point of a starter theme is to make it your own!
15+
2. Activate the theme in the WordPress Dashboard under **Appearance → Themes**.
16+
3. Do your thing! And read [the docs](https://timber.github.io/docs/).
17+
18+
## The `StarterSite` class
19+
20+
In **functions.php**, we call `new StarterSite();`. The `StarterSite` class sits in the **src** folder. You can update this class to add functionality to your theme. This approach is just one example for how you could do it.
21+
22+
The **src** folder would be the right place to put your classes that [extend Timber’s functionality](https://timber.github.io/docs/v2/guides/extending-timber/).
23+
24+
Small tip: You can make use of Composer’s [autoloading functionality](https://getcomposer.org/doc/04-schema.md#psr-4) to automatically load your PHP classes when they are requested instead of requiring one by one in **functions.php**.
25+
26+
## What else is there?
27+
28+
- `static/` is where you can keep your static front-end scripts, styles, or images. In other words, your Sass files, JS files, fonts, and SVGs would live here.
29+
- `views/` contains all of your Twig templates. These pretty much correspond 1 to 1 with the PHP files that respond to the WordPress template hierarchy. At the end of each PHP template, you’ll notice a `Timber::render()` function whose first parameter is the Twig file where that data (or `$context`) will be used. Just an FYI.
30+
- `tests/` ... basically don’t worry about (or remove) this unless you know what it is and want to.
31+
32+
## Other Resources
33+
34+
* [This branch](https://github.com/laras126/timber-starter-theme/tree/tackle-box) of the starter theme has some more example code with ACF and a slightly different set up.
35+
* [Twig for Timber Cheatsheet](http://notlaura.com/the-twig-for-timber-cheatsheet/)
36+
* [Timber and Twig Reignited My Love for WordPress](https://css-tricks.com/timber-and-twig-reignited-my-love-for-wordpress/) on CSS-Tricks
37+
* [A real live Timber theme](https://github.com/laras126/yuling-theme).
38+
* [Timber Video Tutorials](http://timber.github.io/timber/#video-tutorials) and [an incomplete set of screencasts](https://www.youtube.com/playlist?list=PLuIlodXmVQ6pkqWyR6mtQ5gQZ6BrnuFx-) for building a Timber theme from scratch.

archive.php

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
/**
3+
* The template for displaying Archive pages.
4+
*
5+
* Used to display archive-type pages if nothing more specific matches a query.
6+
* For example, puts together date-based pages if no date.php file exists.
7+
*
8+
* Learn more: http://codex.wordpress.org/Template_Hierarchy
9+
*
10+
* Methods for TimberHelper can be found in the /lib sub-directory
11+
*
12+
* @package WordPress
13+
* @subpackage Timber
14+
* @since Timber 0.2
15+
*/
16+
17+
$templates = array( 'archive.twig', 'index.twig' );
18+
19+
$context = Timber::context();
20+
21+
$context['title'] = 'Archive';
22+
if ( is_day() ) {
23+
$context['title'] = 'Archive: ' . get_the_date( 'D M Y' );
24+
} elseif ( is_month() ) {
25+
$context['title'] = 'Archive: ' . get_the_date( 'M Y' );
26+
} elseif ( is_year() ) {
27+
$context['title'] = 'Archive: ' . get_the_date( 'Y' );
28+
} elseif ( is_tag() ) {
29+
$context['title'] = single_tag_title( '', false );
30+
} elseif ( is_category() ) {
31+
$context['title'] = single_cat_title( '', false );
32+
array_unshift( $templates, 'archive-' . get_query_var( 'cat' ) . '.twig' );
33+
} elseif ( is_post_type_archive() ) {
34+
$context['title'] = post_type_archive_title( '', false );
35+
array_unshift( $templates, 'archive-' . get_post_type() . '.twig' );
36+
}
37+
38+
$context['posts'] = Timber::get_posts();
39+
40+
Timber::render( $templates, $context );

author.php

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
/**
3+
* The template for displaying Author Archive pages
4+
*
5+
* Methods for TimberHelper can be found in the /lib sub-directory
6+
*
7+
* @package WordPress
8+
* @subpackage Timber
9+
* @since Timber 0.1
10+
*/
11+
12+
global $wp_query;
13+
14+
$context = Timber::context();
15+
$context['posts'] = Timber::get_posts();
16+
if ( isset( $wp_query->query_vars['author'] ) ) {
17+
$author = Timber::get_user( $wp_query->query_vars['author'] );
18+
$context['author'] = $author;
19+
$context['title'] = 'Author Archives: ' . $author->name();
20+
}
21+
Timber::render( array( 'author.twig', 'archive.twig' ), $context );

composer.json

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"name": "upstatement/timber-starter-theme",
3+
"description": "Starter theme to build a Timber theme",
4+
"type":"wordpress-theme",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"email": "jared@upstatement.com",
9+
"name": "jarednova"
10+
}
11+
],
12+
"repositories": [
13+
{
14+
"type": "composer",
15+
"url": "https://wpackagist.org"
16+
}
17+
],
18+
"require": {
19+
"timber/timber": "2.x-dev"
20+
},
21+
"require-dev": {
22+
"automattic/wordbless": "^0.4.2",
23+
"yoast/wp-test-utils": "^1.0"
24+
},
25+
"extra": {
26+
"installer-paths": {
27+
"vendor/automattic/wordbless/": [
28+
"automattic/wordbless"
29+
]
30+
},
31+
"wordpress-install-dir": "wordpress"
32+
},
33+
"config": {
34+
"allow-plugins": {
35+
"roots/wordpress-core-installer": true,
36+
"composer/installers": true
37+
}
38+
},
39+
"scripts": {
40+
"test": "phpunit"
41+
}
42+
}

footer.php

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
/**
3+
* Third party plugins that hijack the theme will call wp_footer() to get the footer template.
4+
* We use this to end our output buffer (started in header.php) and render into the view/page-plugin.twig template.
5+
*
6+
* If you're not using a plugin that requries this behavior (ones that do include Events Calendar Pro and
7+
* WooCommerce) you can delete this file and header.php
8+
*
9+
* @package WordPress
10+
* @subpackage Timber
11+
* @since Timber 0.1
12+
*/
13+
14+
$timberContext = $GLOBALS['timberContext']; // @codingStandardsIgnoreFile
15+
if ( ! isset( $timberContext ) ) {
16+
throw new \Exception( 'Timber context not set in footer.' );
17+
}
18+
$timberContext['content'] = ob_get_contents();
19+
ob_end_clean();
20+
$templates = array( 'page-plugin.twig' );
21+
Timber::render( $templates, $timberContext );

functions.php

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/**
4+
* Timber starter-theme
5+
* https://github.com/timber/starter-theme
6+
*/
7+
8+
// Load Composer dependencies.
9+
require_once __DIR__ . '/vendor/autoload.php';
10+
11+
require_once __DIR__ . '/src/StarterSite.php';
12+
13+
require_once(__DIR__ . '/inc/Vite.php');
14+
require_once(__DIR__ . '/inc/Theme.php');
15+
16+
Timber\Timber::init();
17+
18+
// Sets the directories (inside your theme) to find .twig files.
19+
Timber::$dirname = ['templates', 'views'];
20+
21+
new StarterSite();

header.php

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
/**
3+
* Third party plugins that hijack the theme will call wp_head() to get the header template.
4+
* We use this to start our output buffer and render into the view/page-plugin.twig template in footer.php
5+
*
6+
* If you're not using a plugin that requries this behavior (ones that do include Events Calendar Pro and
7+
* WooCommerce) you can delete this file and footer.php
8+
*
9+
* @package WordPress
10+
* @subpackage Timber
11+
* @since Timber 0.1
12+
*/
13+
14+
$GLOBALS['timberContext'] = Timber::context();
15+
ob_start();

humans.txt

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
## Team
2+
3+
Your name or company
4+
https://yoursite.example
5+
@your_twitter_handle
6+
7+
8+
## Site
9+
10+
Software: Built with [Timber](https://upstatement.com/timber/) by Upstatement

inc/Theme.php

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
class Theme
4+
{
5+
6+
public function __construct()
7+
{
8+
9+
// enqueue admin styles scripts
10+
add_action('wp_enqueue_scripts', [$this, 'enqueue_styles_scripts'], 20);
11+
}
12+
13+
/**
14+
* @return void
15+
* @throws Exception
16+
*/
17+
public function enqueue_styles_scripts(): void
18+
{
19+
20+
// enqueue the Vite module
21+
Vite::enqueue_module();
22+
23+
// register theme-style-css
24+
$filename = Vite::asset('resources/styles/styles.scss');
25+
26+
// enqueue theme-style-css into our head
27+
wp_enqueue_style('theme-style', $filename, [], null, 'screen');
28+
29+
// register theme-script-js
30+
$filename = Vite::asset('resources/scripts/scripts.js');
31+
32+
// enqueue theme-script-js into our head (change false to true for footer)
33+
wp_enqueue_script('theme-script', $filename, [], null, false);
34+
35+
// update html script type to module wp hack
36+
Vite::script_type_module('theme-script');
37+
}
38+
}
39+
40+
new Theme();

0 commit comments

Comments
 (0)