Skip to content

Commit

Permalink
Merge pull request #128 from flyntwp/templates
Browse files Browse the repository at this point in the history
feat(templates): move wordpress templates into root
  • Loading branch information
domtra authored Aug 9, 2019
2 parents 074470c + 96014ec commit 51e9f3c
Show file tree
Hide file tree
Showing 22 changed files with 31 additions and 58 deletions.
2 changes: 1 addition & 1 deletion templates/404.php → 404.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

$context = Timber::get_context();

Timber::render('twig/404.twig', $context);
Timber::render('templates/404.twig', $context);
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@ For organisation, `./inc` has three subfolders. We recommend using these three f
After the files from './lib' and './inc' are loaded, all [components](#components) from the `./Components` folder are loaded.

### Page Templates
All template files can be found in the `./templates` directory. Flynt uses [Timber](https://www.upstatement.com/timber/) to structure its page templates and [Twig](https://twig.symfony.com/) for rendering them. [Timber's documentation](https://timber.github.io/docs/) is extensive and up to date, so be sure to get familiar with it.
Flynt uses [Timber](https://www.upstatement.com/timber/) to structure its page templates and [Twig](https://twig.symfony.com/) for rendering them. [Timber's documentation](https://timber.github.io/docs/) is extensive and up to date, so be sure to get familiar with it.

There are two Twig functions added in Flynt to render components into templates:
* `renderComponent(componentName, data)` renders a single component. [For example, in the `index.twig` template](https://github.com/flyntwp/flynt/tree/master/templates/twig/index.twig).
* `renderFlexibleContent(flexibleContentField)` renders all components passed from an Advanced Custom Fields *Flexible Content* field. [For example, in the `single.twig` template.](https://github.com/flyntwp/flynt/tree/master/templates/twig/single.twig)
* `renderComponent(componentName, data)` renders a single component. [For example, in the `index.twig` template](https://github.com/flyntwp/flynt/tree/master/templates/index.twig).
* `renderFlexibleContent(flexibleContentField)` renders all components passed from an Advanced Custom Fields *Flexible Content* field. [For example, in the `single.twig` template.](https://github.com/flyntwp/flynt/tree/master/templates/single.twig)

Besides the main document structure (in `./templates/twig/_document.twig`), everything else is a component.
Besides the main document structure (in `./templates/_document.twig`), everything else is a component.

### Components
A component is a self-contained building-block. Each component contains its own layout, its ACF fields, PHP logic, scripts, and styles.
Expand Down Expand Up @@ -275,7 +275,7 @@ Flynt includes several utility functions for creating Advanced Custom Fields opt

### WPML

If you are using Flynt and [WPML](https://wpml.org/), you will need to create a Must-Use (MU) Plugin in order to load Twig from Timber before the WPML code is executed. This is because WPML includes an outdated version of Twig.
If you are using Flynt and [WPML](https://wpml.org/), you will need to create a Must-Use (MU) Plugin in order to load Twig from Timber before the WPML code is executed. This is because WPML includes an outdated version of Twig.

To do this, create `/wp-content/mu-plugins/flynt-wpml-compat.php` and add this code:

Expand Down
2 changes: 1 addition & 1 deletion templates/basestyle.php → basestyle.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@

$context = Timber::get_context();

Timber::render('twig/basestyle.twig', $context);
Timber::render('templates/basestyle.twig', $context);
1 change: 1 addition & 0 deletions build-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ module.exports = {
reloadDebounce: 1000,
ui: false,
files: [
'*.php',
'templates/**/*',
'lib/**/*',
'inc/**/*',
Expand Down
6 changes: 0 additions & 6 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@
define('WP_ENV', 'production');
}

// This needs to happen first.
// Reason: In case the theme was just activated and the plugin is not active,
// we still need to run the after_switch_theme action, which is
// defined here.
Init::setTemplateDirectory();

// Check if the required plugins are installed and activated.
// If they aren't, this function redirects the template rendering to use
// plugin-inactive.php instead and shows a warning in the admin backend.
Expand Down
4 changes: 2 additions & 2 deletions inc/baseStyle.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/**
* Loads a `basestyle.php` template from the templates folder to render base style markup for proper base styling, if user is on non-production environment or has edit rights at least.
* Loads a `basestyle.php` template to render base style markup for proper base styling, if user is on non-production environment or has edit rights at least.
*
* Example usage:
* 1. Log into your WordPress Backend with an Administrator account.
Expand Down Expand Up @@ -51,7 +51,7 @@ function templateInclude($template)
global $wp_query;

if (isset($wp_query->query_vars['BaseStyle'])) {
return get_template_directory() . '/templates/basestyle.php';
return get_template_directory() . '/basestyle.php';
}

return $template;
Expand Down
2 changes: 1 addition & 1 deletion inc/theContentFix.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@
$postId = $attrs['id'];
$context = Timber::get_context();
$context['post'] = $post = new Post($postId);
return Timber::fetch('templates/twig/theContentFix.twig', $context);
return Timber::fetch('templates/theContentFix.twig', $context);
});
14 changes: 11 additions & 3 deletions index.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
<?php

if (defined('ABSPATH')) {
update_option('template', get_option('template') . '/templates');
use Timber\Timber;
use Timber\PostQuery;
use Flynt\Utils\Options;
use const Flynt\Archives\POST_TYPES;

$context = Timber::get_context();
$context['posts'] = new PostQuery();

if (isset($_GET['contentOnly'])) {
$context['contentOnly'] = true;
}

die();
Timber::render('templates/index.twig', $context);
15 changes: 0 additions & 15 deletions lib/Init.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,6 @@ public static function loadComponents()
do_action('Flynt/afterRegisterComponents');
}

public static function setTemplateDirectory()
{
add_action('after_switch_theme', function () {
$stylesheet = get_option('stylesheet');

if (basename($stylesheet) !== 'templates') {
update_option('stylesheet', $stylesheet . '/templates');
}
});

add_filter('stylesheet', function ($stylesheet) {
return dirname($stylesheet);
});
}

public static function checkRequiredPlugins()
{
$acfActive = class_exists('acf');
Expand Down
2 changes: 1 addition & 1 deletion templates/page.php → page.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
$context = Timber::get_context();
$context['post'] = new Post();

Timber::render('twig/page.twig', $context);
Timber::render('templates/page.twig', $context);
2 changes: 1 addition & 1 deletion templates/search.php → search.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
$context['posts'] = new PostQuery();
$context['searchQuery'] = get_search_query();

Timber::render('twig/search.twig', $context);
Timber::render('templates/search.twig', $context);
2 changes: 1 addition & 1 deletion templates/single.php → single.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
$context = Timber::get_context();
$context['post'] = new Post();

Timber::render('twig/single.twig', $context);
Timber::render('templates/single.twig', $context);
2 changes: 1 addition & 1 deletion templates/twig/404.twig → templates/404.twig
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% extends 'twig/_document.twig' %}
{% extends 'templates/_document.twig' %}

{% block content %}
{{ renderComponent('BlockNotFound') }}
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion templates/twig/basestyle.twig → templates/basestyle.twig
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% extends 'twig/_document.twig' %}
{% extends 'templates/_document.twig' %}

{% block content %}
<div class="centerMaxWidthContainer">
Expand Down
15 changes: 0 additions & 15 deletions templates/index.php

This file was deleted.

2 changes: 1 addition & 1 deletion templates/twig/index.twig → templates/index.twig
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% extends contentOnly ? 'twig/_contentOnly.twig' : 'twig/_document.twig' %}
{% extends contentOnly ? 'templates/_contentOnly.twig' : 'templates/_document.twig' %}

{% block content %}
{{ renderComponent('GridPostsArchive', { posts: posts }) }}
Expand Down
2 changes: 1 addition & 1 deletion templates/twig/page.twig → templates/page.twig
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% extends 'twig/_document.twig' %}
{% extends 'templates/_document.twig' %}

{% block content %}
{{ renderFlexibleContent(post.meta('pageComponents')) }}
Expand Down
2 changes: 1 addition & 1 deletion templates/twig/search.twig → templates/search.twig
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% extends 'twig/_document.twig' %}
{% extends 'templates/_document.twig' %}

{% block content %}
{{ renderComponent('ListSearchResults', {posts: posts, pagination: posts.pagination, searchTerm: searchTerm}) }}
Expand Down
2 changes: 1 addition & 1 deletion templates/twig/single.twig → templates/single.twig
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% extends 'twig/_document.twig' %}
{% extends 'templates/_document.twig' %}

{% block content %}
<article class="post">
Expand Down
File renamed without changes.

0 comments on commit 51e9f3c

Please sign in to comment.