WordPressで「Blade」テンプレートエンジンを使おう!

WordPressで「Blade」テンプレートエンジンを使おう!

王



WordPressPHPHTML

Blade
PHPLaravel
BladeWordPress使Mikael MattssonWordPress使


WordPress  Blade « WordPress Plugins
https://wordpress.org/plugins/blade/


Blade





{{$foo}}


<?php echo $foo ?>

if()

@if(has_post_thumbnail())
    {{the_post_thumbnail() }}
@else 
    <img src="{{bloginfo( 'template_url' )}}/images/thumbnail-default.jpg" />
@endif


<?php if(has_post_thumbnail()) : ?>
    <?php the_post_thumbnail() ?>
<?php else: ?>
    <img src="<?php bloginfo( 'template_url' ) ?>/images/thumbnail-default.jpg" />
<?php endif; ?>

@wpposts
    <a href="{{the_permalink()}}">{{the_title()}}</a><br>
@wpempty
    <p>404</p>
@wpend


<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <a href="<?php the_permalink() ?>"><?php the_title() ?></a><br>
<?php endwhile; else: ?>
    <p>404</p>
<?php endif; ?>

wordpress query

<ul>
@wpquery(array('post_type' => 'post'))
    <li><a href="{{the_permalink()}}">{{the_title()}}</a></li>
@wpempty
    <li>{{ __('Sorry, no posts matched your criteria.') }}</li>
@wpend
</ul>


<ul>
<?php $query = new WP_Query( array('post_type' => 'post') ); ?>
<?php if ( $query->have_posts() ) : ?>
    <?php while ( $query->have_posts() ) : $query->the_post(); ?>
        <li><a href="<?php the_permalink() ?>"> <?php the_title() ?> </a></li>
    <?php endwhile; ?>
<?php else : ?>
    <li><?php _e('Sorry, no posts matched your criteria.') ?></li>
<?php endif; wp_reset_postdata(); ?>
</ul>

Advanced Custom Fields

<ul>
    @acfrepeater('images')
        <li>{{ get_sub_field( 'image' ) }}</li>
    @acfend
</ul>


<ul>
    <?php if( get_field( 'images' ) ): ?>
        <?php while( has_sub_field( 'images' ) ): ?>
            <li><img src="<?php the_sub_field( 'image' ) ?>" /></li>
        <?php endwhile; ?>
    <?php endif; ?>
</ul>

@include('header')

layout & section


Blade

使

page.php
@layout()使/layout/master.php@layout('layout.master')

@section()  @endsection

page.php:
@layout('layout.master')

@section('content')
    <p>Lorem ipsum</p>
@endsection

master.php@yield()

master.php:
<html>
    <div class="content">
        @yield('content')
    </div>
</html>


<html>
    <div class="content">
        <p>Lorem ipsum</p>
    </div>
</html>


使Blade


Templates  Laravel  The PHP Framework For Web Artisans
http://laravel.com/docs/4.2/templates


使


使

使


使
BladeWordPresspage.phpsingle.php使



WordPressBladewp-content/plugins/blade/storage

BladePHP使

PhpStormBlade使


PhpStormBlade.blade.php

使 
.blade*.blade.phpGulp

`gulpfile.js``default`
(function() {
  var BLADE_TEMPLATES, File, duplicate, fs, globby, gulp, is_expired, path;

  fs = require('fs');

  path = require('path');

  gulp = require('gulp');

  globby = require('globby');

  BLADE_TEMPLATES = 'wp-content/themes/**/*.blade.php';

  File = function(file_path) {
    this.name = path.basename(file_path, '.blade.php');
    this.dir = path.dirname(file_path);
  };

  is_expired = function(template, compiled) {
    var compiled_mtime, template_mtime;
    if (fs.existsSync(compiled)) {
      template_mtime = fs.statSync(template).mtime.getTime();
      compiled_mtime = fs.statSync(compiled).mtime.getTime();
      if (template_mtime > compiled_mtime) {
        return true;
      } else {
        return false;
      }
    }
    return true;
  };

  duplicate = function(file_path) {
    var file, new_file_path, reader, writer;
    file = new File(file_path);
    new_file_path = "" + file.dir + "/" + file.name + ".php";
    if (!is_expired(file_path, new_file_path)) {
      return;
    }
    reader = fs.createReadStream(file_path, {
      encoding: 'utf8'
    });
    writer = fs.createWriteStream(new_file_path);
    reader.pipe(writer);
    return writer.on('finish', function() {
      return console.log("Duplicated: " + new_file_path);
    });
  };

  gulp.task('blade', function() {
    return gulp.watch(BLADE_TEMPLATES, function(e) {
      var file_path;
      file_path = e.path;
      return duplicate(file_path);
    });
  });

  gulp.task('compile_all_blade_template', function() {
    return globby(BLADE_TEMPLATES, function(err, paths) {
      return paths.forEach(function(file_path) {
        return duplicate(file_path);
      });
    });
  });

  gulp.task('default', ['compile_all_blade_template', 'blade']);

}).call(this);

ViewModel


使ViewModel


structure


functions.php


functions.phpfunctionsPHPfunctions.php
<?php
/*
 * functionsフォルダにあるファイルをすべて読み込む
*/

foreach ( glob( TEMPLATEPATH . "/functions/*.php" ) as $file ) {
  require_once $file;
}

functions/route.php

<?php

require_once dirname(dirname( __FILE__ )).'/app/Route.php';
use \App\Route;

Route::init();

app/Route.php


$viewsis_page()
<?php namespace App;
require_once 'Model.php';
use Closure;

class Route {
  static public $views = array(
    'home',
    'page',
  );

  /**
   * $viewsにあった「ページ」にアクセスしたら、必要なデータを取得するようにする
   */
  static public function init() {
    foreach ( static::$views as $view ) {
      static::listen( $view, function ( $wp ) use ( $view ) {
        global $d;
        $d = new Model( $view );
      } );
    }
  }

  /**
   * 該当のページと該当のコールバックを関連付ける
   *
   * @param $view
   * @param callable $callback
   *
   * @return void
   */
  static public function listen( $view, Closure $callback ) {
    add_action( 'wp', function ( $wp ) use ( $view, $callback ) {
      if ( in_array( $view, static::$views ) AND static::is_page( $view ) ) {
        $callback( $wp );
      }
    } );
  }

  /**
   * 該当のページかどうかを判断する
   *
   * @param string $view
   *
   * @return bool
   */
  static public function is_page( $view ) {
    if ( is_admin() ) {
      return false;
    }
    switch ( $view ) {
      case 'home':
        return is_home();
      case 'page':
        return is_page();
    }

    return false;
  }
}

BaseModel.php & Model.php


BaseModel.php
<?php
namespace App;

abstract class BaseModel {
  public $posts = array();
  public $extra, $common;

  function __construct( $view ) {
    $this->extra  = (object) array();
    $this->common = (object) array();
    $method       = "set_{$view}";
    $this->set_common();
    if ( method_exists( $this, $method ) ) {
      $this->$method();
    }
  }

  /**
   * 共通データの設定を行う
   */
  abstract protected function set_common();

  /**
   * ヘルパーメソッド。渡されたクロージャをThe-Loopの中で呼ぶ。
   *
   * @param callable $callback         実引数にidを渡す
   * @param \WP_Query $wp_query_object WP_Queryのインスタンスを受け付ける
   */
  protected function each( \Closure $callback, \WP_Query $wp_query_object = null ) {
    // WP_Query Loop
    if ( $wp_query_object instanceof \WP_Query ) {
      if ( $wp_query_object->have_posts() ) {
        while ( $wp_query_object->have_posts() ) {
          $wp_query_object->the_post();
          $callback( get_the_ID() );
        }
      }
      wp_reset_postdata();

      return;
    }

    // Regular Loop
    if ( have_posts() ) {
      while ( have_posts() ) {
        the_post();
        $callback( get_the_ID() );
      }
    }
  }
}

Model.php
<?php
namespace App;
require_once 'BaseModel.php';

class Model extends BaseModel {
  // index.phpにアクセスしてきたときにやりたいこと。
  protected function set_home() {
    $this->extra->foo = 'foo';
  }

  // 「固定ページ」にアクセスしてきたときにやりたいこと。
  protected function set_page() {
    $this->extra->foo = 'bar';
  }

  /**
   * 共通データの設定を行う
   */
  protected function set_common() {
    $this->each( function () {
      $post_obj = array(
        'title'     => get_the_title(),
        'permalink' => get_the_permalink()
      );

      $this->posts[] = (object) $post_obj;
    } );
  }
}



Route`set_`homeset_home

layout/master.php



WordPressget_header()get_footer()使1header.php

Blade1@yield() 

index.php & page.php


index.php
@layout('layout.master')

@section('css')
<style>
  h1 {
    color: #002a80;
  }
</style>
@endsection

@section('js')
<script>alert('index.php')</script>
@endsection

@section('body')
<h1>{{ $d->extra->foo }}</h1>
<ul>
  @foreach($d->posts as $p)
  <li><a href="{{ $p->permalink }}">{{ $p->title }}</a></li>
  @endforeach
</ul>
@endsection

page.php
@layout('layout.master')

@section('body')
  @foreach($d->posts as $p)
    <h1>{{ $p->title }}</h1>
  @endforeach
@endsection

$d

@section使CSSJavaScriptmaster.phpheadJSCSS





1

WordPressBlade

 



 WordPressWidget

 Retina便Compassmixin

 console.log

 RxJS  JavaScript

LIGWeb
Webサイト制作の実績・料金を見る

この記事のシェア数


LIGKING
このメンバーの記事をもっと読む
デザイン力×グローバルな開発体制でDXをトータル支援
お問い合わせ 会社概要DL