Giter Club home page Giter Club logo

modern-templates's Introduction

WP Modern Templates

Software License Build Status

A WordPress Plugin allowing the usage of Twig, Blade, Mustache and Smarty templates in any WordPress view.

Install

Via git in the WordPress plugins directory

$ git clone https://github.com/MartinSotirov/modern-templates.git

Usage

Enabling Twig

add_filter( 'wpmt_engine', 'make_twig_engine', 2, 10 );
function make_twig_engine( $engine, $WPMT ) {
    $settings = array(
        'templatePath' => get_stylesheet_directory() . '/templates',
        'cachePath'    => get_stylesheet_directory() . '/templates/cache'
    );
	$engine = $WPMT->makeEngine('Twig', $settings);
	return $engine;
}

Enabling Blade

add_filter( 'wpmt_engine', 'make_blade_engine', 2, 10 );
function make_blade_engine( $engine, $WPMT ) {
    $settings = array(
        'templatePath' => get_stylesheet_directory() . '/templates',
        'cachePath'    => get_stylesheet_directory() . '/templates/cache'
    );
	$engine = $WPMT->makeEngine('Blade', $settings);
	return $engine;
}

Enabling Smarty

add_filter( 'wpmt_engine', 'make_smarty_engine', 2, 10 );
function make_smarty_engine( $engine, $WPMT ) {
    $settings = array(
        'templatePath' => get_stylesheet_directory() . '/templates',
        'cachePath'    => get_stylesheet_directory() . '/templates/cache',
        'compilePath'  => get_stylesheet_directory() . '/templates/compile',
        'configPath'   => get_stylesheet_directory() . '/templates/config',
    );
	$engine = $WPMT->makeEngine('Smarty', $settings);
	return $engine;
}

Enabling Mustache

add_filter( 'wpmt_engine', 'make_mustache_engine', 2, 10 );
function make_mustache_engine( $engine, $WPMT ) {
    $settings = array(
        'templatePath' => get_stylesheet_directory() . '/templates',
        'cachePath'    => get_stylesheet_directory() . '/templates/cache',
        'partialsPath' => get_stylesheet_directory() . '/templates/partials',
    );
	$engine = $WPMT->makeEngine('Mustache', $settings);
	return $engine;
}

Rendering templates

The plugin abstracts away the technicalities of all four templating engines and provides a consistent API for rendering. With the WordPress plugin enabled, you have access to the global variable $WPMT. Use it to get an instance of the templating engine you instanciated in the wpmt_engine hook

$twig = $WPMT->getEngine();
$data = [
    'wp_head'    => $WPMT->get('wp_head'),
    'wp_footer'  => $WPMT->get('wp_footer'),
    'body_class' => $WPMT->get('body_class'),
    'post'       => get_queried_object(),
];

echo $twig->render('single-post.html', $data);

Change log

Please see CHANGELOG for more information what has changed recently.

Testing

$ bin/install-wp-tests.sh
$ phpunit

Contributing

None so far

License

GPL 2. Please see License File for more information.

modern-templates's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.