Skip to content

Commit af563d0

Browse files
authored
feat: move style registration from template to StarterSite.php (#169)
* feat: move style registration from template to StarterSite.php * load styles from assets/styles/main.css instead * parametrize $main_stylesheet
1 parent 8180832 commit af563d0

File tree

4 files changed

+21
-2
lines changed

4 files changed

+21
-2
lines changed

assets/styles/main.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/* Add your own styles here */

src/StarterSite.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public function __construct() {
2626
add_action( 'after_setup_theme', [ $this, 'theme_supports' ] );
2727
add_action( 'init', [ $this, 'register_post_types' ] );
2828
add_action( 'init', [ $this, 'register_taxonomies' ] );
29+
add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_styles' ] );
2930

3031
add_filter( 'timber/context', [ $this, 'add_to_context' ] );
3132
add_filter( 'timber/twig/filters', [ $this, 'add_filters_to_twig' ] );
@@ -35,6 +36,19 @@ public function __construct() {
3536
parent::__construct();
3637
}
3738

39+
/**
40+
* This enqueues theme styles.
41+
*/
42+
public function enqueue_styles() {
43+
$main_stylesheet = '/assets/styles/main.css';
44+
wp_enqueue_style(
45+
'timber-starter-style',
46+
get_template_directory_uri() . $main_stylesheet,
47+
[],
48+
filemtime(get_template_directory() . $main_stylesheet)
49+
);
50+
}
51+
3852
/**
3953
* This is where you can register custom post types.
4054
*/

style.css

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,9 @@
22
* Theme Name: My Timber 2.x Starter Theme
33
* Description: Starter Theme to use with Timber
44
* Author: Timber Team and You!
5-
*/
5+
*/
6+
7+
/* NOTE:
8+
* This file is reserved for the theme metadata and is not loaded.
9+
* Write styles in assets/styles/main.css instead.
10+
*/

views/partials/head.twig

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<head>
22
<meta charset="{{ site.charset }}" />
3-
<link rel="stylesheet" href="{{ site.theme.link }}/style.css" type="text/css" media="screen" />
43
<meta name="viewport" content="width=device-width, initial-scale=1" />
54
<link rel="author" href="{{ site.theme.link }}/humans.txt" />
65
<link rel="profile" href="http://gmpg.org/xfn/11" />

0 commit comments

Comments
 (0)