-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
412 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# Enabling Environment Variables | ||
|
||
Environment variables is a feature that can be optionally enabled in Hozokit. | ||
|
||
When enabled, Hozokit will make use of environment variables to configure certain features such as [hot reloading](./hot_reload.md) and change the way assets are bundled (e.g styles and scripts are not minified on a development environment) | ||
|
||
Additionally, developers can take advantage of environment variables and access them in `.twig` templates or in their `.php` scripts. | ||
|
||
|
||
## Setup Environment Variables | ||
|
||
1. Create an `.env` filed in the [theme folder directory](../wp-content/themes/hozokit). An [`.env.example`](../wp-content/themes/hozokit/.env.example) file is provided as a starting point. | ||
|
||
2. Set the app environment. If the value is set to development Hozokit won't minify styles which can be helpful when debugging CSS and JavaScript: | ||
|
||
```php | ||
APP_ENVIRONMENT='development' | ||
``` | ||
|
||
Each environment will have its own `.env`. For example, the production server will have its `APP_ENVIRONMENT` set to `'production'`, staging to `'staging'` and so on. Each version of the site can have its own configuration. | ||
|
||
## Using environment variables | ||
|
||
Developers can take advantage of environment variables by using them in `.twig` templates and `.php` scripts. | ||
|
||
1. Add one or more variables to `.env`: | ||
|
||
```php | ||
APP_ENVIRONMENT='development' | ||
API_KEY='m3dMf6XC9NQ^ao8H3csx' | ||
UPDATES_ENABLED=true | ||
``` | ||
|
||
2. Use a variable on a template. All variables become available to context by accessing `env`: | ||
|
||
```twig | ||
{% block example %} | ||
<section class="example"> | ||
<h1>hozokit</h1> | ||
<p>An example component.</p> | ||
{# Makes use of environment variable to determine if markup should be rendered. #} | ||
{% if env.UPDATES_ENABLED %} | ||
<ul> | ||
<li>New shiny colors!</li> | ||
<li>Orders now get delivered right to your door for free.</li> | ||
<li>Accounts can be created so users can keep track or their orders.</li> | ||
</ul> | ||
{% endif %} | ||
</section> | ||
{% endblock %} | ||
``` | ||
|
||
3. Use a variable on `functions.php`. In this case a constant is used to get hold of the variable instead: | ||
|
||
```php | ||
$payments_key = $_ENV['API_KEY']; | ||
setup_payments($payments_key); | ||
``` | ||
|
||
## Additional Notes | ||
|
||
Hozokit makes use of Vance Lucas' [phpdotenv](https://github.com/vlucas/phpdotenv) to environment variables on the `php` side and Scott Motte's [dotenv](https://github.com/motdotla/dotenv) package to make them available in internal Node scripts (`gulpfile.js`). | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,38 @@ | ||
<?php | ||
// Importing Hozokit Internal Plugins | ||
require_once(__DIR__ . '/plugins/index.php'); | ||
|
||
// Initializing Timber. | ||
require_once(__DIR__ . '/vendor/autoload.php'); | ||
$timber = new \Timber\Timber(); | ||
$timber::$dirname = array('templates', 'templates/components/'); | ||
|
||
class Site extends Timber\Site { | ||
public function __construct() { | ||
// Enables admin bar styling. | ||
add_theme_support( 'admin-bar', array( 'callback' => '__return_false' ) ); | ||
add_theme_support( 'post-thumbnails' ); | ||
add_theme_support( 'menus' ); | ||
add_theme_support( 'html5', array( 'comment-list', 'comment-form', 'search-form', 'gallery', 'caption' ) ); | ||
add_filter( 'timber_context', array( $this, 'add_to_context' ) ); | ||
// Makes menu locations available. | ||
register_nav_menus(array( | ||
'primary' => __('Primary'), | ||
'footer' => __('Footer'), | ||
// 'other' => __('Other'), | ||
)); | ||
hozokit_plugins_init(); | ||
parent::__construct(); | ||
} | ||
|
||
function add_to_context( $context ) { | ||
$context['menus'] = array( | ||
'primary' => new Timber\Menu('primary'), | ||
'footer' => new Timber\Menu('footer'), | ||
// 'other' => new Timber\Menu('other'), | ||
); | ||
$context['site'] = $this; | ||
return $context; | ||
} | ||
} | ||
new Site(); | ||
|
||
// Insert styles and scripts to be used in the theme. | ||
// The stylesheet is compiled from SASS files in /styles, scripts from /scripts using Gulp. | ||
function loadStylesAndScripts() { | ||
$theme_version = wp_get_theme()['Version']; | ||
wp_enqueue_style( 'normalize', get_template_directory_uri() . '/assets/css/normalize.css' ); | ||
wp_enqueue_style( 'style', get_stylesheet_uri() ); | ||
wp_register_script( 'script', get_template_directory_uri() . '/assets/scripts/bundle.js', "", $theme_version, true ); | ||
wp_enqueue_script('script'); | ||
$translation_array = array( 'templateUrl' => get_template_directory_uri() ); | ||
//after wp_enqueue_script | ||
wp_localize_script( 'script', 'wordpress', $translation_array ); | ||
} | ||
|
||
// Load styles and scripts (might need wp_head or wp_footer). | ||
add_action('wp_enqueue_scripts', 'loadStylesAndScripts'); | ||
|
||
// Temporary: Resets metabox data positions. | ||
// This needs to be commented out when the ACF field structure becomes permanent. | ||
function prefix_reset_metabox_positions(){ | ||
delete_user_meta( wp_get_current_user()->ID, 'meta-box-order_post' ); | ||
delete_user_meta( wp_get_current_user()->ID, 'meta-box-order_page' ); | ||
} | ||
|
||
// Temporary: Remove admin bar in order to avoid extra margins. | ||
show_admin_bar(false); | ||
|
||
// Temporary: Keeps Gutenberg from rendering <p> tags on Timber's post.preview. | ||
remove_filter( 'the_content', 'wpautop' ); | ||
/* | ||
* Hozokit | ||
* The statement below imports Hozokit Core and other internal plugins. | ||
* Please keep this require statement at all times at the top. | ||
* | ||
*/ | ||
defined( 'ABSPATH' ) || exit; | ||
require_once(__DIR__ . '/plugins/index.php'); | ||
|
||
/* !IMPORTANT: Only run this one when adding new custom post types. | ||
* Remove when related features are added. | ||
/* | ||
* Functions | ||
* Add theme specific functions below. | ||
* | ||
*/ | ||
// flush_rewrite_rules(); | ||
|
||
// Loads bundled styles and scripts into the theme. | ||
// This enqueues the files the way Wordpress intends but | ||
// if these need to be included a different way, remove this statement and | ||
// use the enqueue functions instead: https://developer.wordpress.org/reference/hooks/wp_enqueue_scripts/#user-contributed-notes | ||
// It resets styles by default (with Normalize). | ||
Hozokit::load_styles_scripts( | ||
array( | ||
'reset_styles' => true | ||
) | ||
); | ||
|
||
// Adds additional data to the site context. | ||
// This makes it available in the templates. | ||
// The filter is required so the data is added at the correct stage. | ||
add_filter( 'timber/context', function( $context ) { | ||
$global_context = array( | ||
'example' => 'add new entries to this array to make them available anywhere in any Twig template.' | ||
); | ||
|
||
$context = $context + $global_context; | ||
return $context; | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,14 @@ | ||
<?php | ||
// Get Timber's context. | ||
$context = Timber::get_context(); | ||
|
||
// Add new data to this specific view. | ||
// In this case the current instance of the post (userful to get the title of the page for example) | ||
// and an array with all posts | ||
$context['post'] = new Timber\Post(); | ||
$context['posts'] = Timber::get_posts(); | ||
|
||
// Then the Twig template that should be rendered is specified | ||
// and the $context with the new added values is passed to it. | ||
Timber::render( 'index.twig', $context); | ||
?> | ||
?> |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.