Skip to content

RShergold/mustachePress

Repository files navigation

MustachePress.php

A Wordpress plugin for using Mustache tempates.

This is just a very small wrapper around the excellent Mustache.php engine by Justin Hileman. Included in this repository (in /Mustache) is a complete copy of Mustache.php

It is the version from September 2014. If you would like to update to the newer version simply swap it out. However I can't be sure it will work!

Usage

index.php

<?php

echo MustachePress::render('index');

index.mustache

<h1> {{get_bloginfo name}} </h1>

Functions

You can access all the standard Wordpress functions (as above) using the {{function_name variable1 variable2}} syntax.

You can also create your own functions and call them from within your template.

###For logic

single.php

<?php

the_post();

function was_this_posted_today(){
  return ( date('Yz') == get_the_time('Yz') ) ? true : false;
}

echo MustachePress::render('single');

single.mustache

<h1> {{the_title}} </h1>

{{{the_content}}}

{{#was_this_posted_today}}
  Posted today!
{{/was_this_posted_today}}

###For output

single.php

<?php

the_post();

function author_job_title(){
  return ( get_the_author() == 'Remi' ) ? 'Admin' : 'Contributor';
}

echo MustachePress::render('single');

single.mustache

<h1> {{the_title}} </h1>

{{{the_content}}}

<p>This post was written by {{the_author}} - {{author_job_title}}</p>

Data

You can pass in data just as you would in the base Mustache.php engine.

###Array single.php

<?php

$data_to_render = array(
  'template_engine' => 'mustache.php',
  'colors' => array(
    array('color'=>'red'),
    array('color'=>'green'),
    array('color'=>'blue')
  )
);

echo MustachePress::render('single',$data_to_render);

###Object single.php

<?php

class SinglePost {
  public $template_engine = 'mustache.php';

  public function colors() {
    return array(
      array('color'=>'red'),
      array('color'=>'green'),
      array('color'=>'blue')
    );
  }
}

$data_to_render = new SinglePost;

echo MustachePress::render('single', $data_to_render);

###Example template single.mustache

<p>we are rendering with {{template_engine}}</p>

three colors
<ul>
  {{#colors}}
    <li>{{color}}</li>
  {{/colors}}
</ul>

Using The Loop

There is one reserved word posts_loop that invokes the standard Wordpress loop (The Loop).

A standard use of The Loop

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

  <h2><?php the_title() ?></h2>
  <?php the_content() ?>

<?php endwhile; else : ?>

  Sorry no posts

<?php endif; ?>

MustachePress version

{{#posts_loop}}

  <h2>{{the_title}}</h2>
  {{{the_content}}}

{{/posts_loop}}
{{^posts_loop}}

  Sorry no posts

{{/posts_loop}}

Template location

By default mustachePress will look for all templates (including partials) in:

wordpress_template_directory/views

if that does not exist it will revert to

wordpress_template_directory

You can change this in settings.

Settings

You can change any of the settings for the base Mustache.php engine like so

functions.php

MustachePress::settings(array(
  'loader' => new Mustache_Loader_FilesystemLoader( '/my/custom/path' ),
  'partials_loader' => new Mustache_Loader_FilesystemLoader( '/my/custom/path' )
));

For a complete list of options look at the Constructor options for Mustache.php

###Note MustachePress has the BLOCKS pragma enabled by default. This is because it is a convenient way of creating a site-wide layout template. this can of course be turned off by changing the pragmas setting.






About

A Wordpress plugin for using Mustache tempates.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages