starring-jane/wordpress-blade 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

starring-jane/wordpress-blade

Composer 安装命令:

composer require starring-jane/wordpress-blade

包简介

A composer package that enables Wordpress to use the Blade templating engine

README 文档

README

Adds the Laravel Blade template engine to WordPress

Installation for WordPlate (skip for basic WordPress)

Start with adding the composer package

composer require starring-jane/wordpress-blade

Create a WordpressBlade instance in your theme functions.php

use StarringJane\WordpressBlade\WordpressBlade;

WordpressBlade::create(
    base_path('resources/views'), // Path to all blade files
    base_path('storage/views/cache'), // Path to blade cache
    base_path('public/themes/janes/components') // Path to component classes (optional, but recommended)
);

Create the following structure

/public
├── /themes
|   └── /theme-name
|       ├── /components
|       |   └── button.php
|       ├── /templates
|       |   ├── 404.php
|       |   ├── front-page.php
|       |   ├── page.php
|       |   └── single.php
|       └── functions.php
|       └── index.php
|       └── style.css
├── /resources
|   └── /views
|       └── /components
|       |   └── button.blade.php
|       └── /pages
|       |   └── page.blade.php
|       └── index.blade.php
└── /storage/cache/views

Installation for WordPress (skip for WordPlate)

Start with adding the composer package

composer require starring-jane/wordpress-blade

Create a WordpressBlade instance in functions.php

use StarringJane\WordpressBlade\WordpressBlade;

WordpressBlade::create(
    get_theme_file_path('views'), // Path to all blade files
    get_theme_file_path('cache/views'), // Path to blade cache
    get_theme_file_path('components') // Path to component classes (optional, but recommended)
);

Autoload your theme namespace in composer.json

{
    ...
    "autoload": {
        "psr-4": {
            "ThemeName\\": "wp-content/themes/theme-name"
        }
    },
    ...
}

Create the following structure

/wp-content/themes/theme-name
├── /cache/views
├── /components
|   └── button.php
├── /templates
|   ├── 404.php
|   ├── front-page.php
|   ├── page.php
|   └── single.php
├── /views
|   └── /components
|   |   └── button.blade.php
|   └── /pages
|   |   └── default.blade.php
|   └── index.blade.php
└── functions.php
└── index.php
└── style.css

Templates

Add a page template in the /templates directory with a component class and add at least a public render function

/templates/page.php

<?php

namespace ThemeName;

use StarringJane\WordpressBlade\Component;

class Page extends Component
{
    public function render()
    {
        return $this->view('pages.default', [
            'post' => get_post(),
        ]);
    }
}

Now you can access the $post variable in views/pages/default.blade.php

@extends('layouts.main')

@section('content')
    <h1>{{ $post->post_title }}</h1>
    <div>{!! $post->post_content !!}<div>
@endsection

Components

Anonymous Components

Add your components to views/components

// views/components/button.blade.php
@props([
    'type' => 'default',
])

<button
    {{ $attributes->merge(['class' => 'btn btn-'.$type]) }}
>
    {{ $slot }}
</button>

// views/pages/default.blade.php
<x-button type="primary">
    Click me
</x-button>

Class Components

<?php

namespace ThemeName\Components;

use StarringJane\WordpressBlade\Component;

class Button extends Component
{
    public $type;

    public function __construct($type)
    {
        $this->type = $type;
    }

    public function render()
    {
        return $this->view('components.button', [
            'color' => $this->color(),
        ]);
    }

    protected function color()
    {
        return 'red';
    }
}

Custom directives

The following example creates a @datetime($var) directive which formats a given $var

<?php

use StarringJane\WordpressBlade\WordpressBlade;

add_action('after_setup_theme', function () {
    WordpressBlade::directive('datetime', function ($expression) {
        return "<?php echo ($expression)->format('m/d/Y H:i'); ?>";
    });
});

Filters

Add page template directories

Add directories where wordpress should look for page templates, starting from the theme base

add_filter('wordpress-blade/template-directories', function ($directories) {
    $directories[] = 'controllers';

    return $directories;
});

Migrating from bladerunner

Follow this guide to migrate from bladerunner

Contributors

starring-jane/wordpress-blade 适用场景与选型建议

starring-jane/wordpress-blade 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5.52k 次下载、GitHub Stars 达 8, 最近一次更新时间为 2020 年 10 月 02 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 starring-jane/wordpress-blade 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 starring-jane/wordpress-blade 我们能提供哪些服务?
定制开发 / 二次开发

基于 starring-jane/wordpress-blade 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 5.52k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 8
  • 点击次数: 22
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 8
  • Watchers: 4
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-10-02