qtgye/wp-template-wrapper

该包最新版本(0.1.2)没有可用的许可信息。

组织WordPress模板的更好方式。

0.1.2 2017-05-29 02:46 UTC

This package is not auto-updated.

Last update: 2024-09-21 00:20:52 UTC


README

开发WordPress模板的更好方式。

功能

组织模板文件

所有模板文件都位于templates文件夹中。
主题根文件夹中的任何模板文件将无法工作

模板别名

模板包装器将WordPress默认模板文件命名转换为提供更好的文件组织。
“别名化”的模板文件,即single-{post_type|post_name|post_id}等,将按该类型命名子目录分组。

示例

  • templates/single/post.php将替换single-post.php
  • templates/page/about-us.php将替换page-about-us.php
  • templates/archive/4.php将替换archive-4.php
  • 模板类型的文件将按原样使用;例如,templates/single.phptemplates/page.php等。

通常,文件按{type}/{slug|id|custom-name}.php格式组织。

自定义页面模板

自定义页面模板应放置在templates文件夹中。

模板包装器仅用于在管理中注册自定义页面模板文件。
也就是说,自定义页面模板文件将不会用作“视图”。
您必须在templates/page文件夹中创建一个同名文件。

示例

要使用custom-donate-page.php自定义“捐赠页面模板”,
page-templates文件夹中创建一个作为注册,仅包含模板名称。
templates/page文件夹中再创建一个作为“视图”。这将用于渲染视图。

基于模板的路由

模板包装器允许通过template_routes钩子注册路由。
路由放置在以下方式src/routes.php

// src/routes.php

add_filter( 'template_routes', function () {

	return array(
		$template_slug => $callable,
	);

});

$template_slug (字符串)

模板别名的“路由”。这与WordPress的模板层次结构类似,但已转换为基于目录的。

$callable (函数或对象/类方法)

“控制器”。这将处理传递给模板的数据。这可以是函数或对象/类方法,类似于MVC方法。

示例

	// Matches is_single(), using a function name
	'single' => 'post_data',
	// Matches 'project' post type single, Using a static class method
	'single/project' => [ 'MyControllerClass', 'project_single' ],
	// Matches single for a post_id of 12
	'single/12' => [ 'MyControllerClass', 'project_single' ],
	// Will match about page, using a namespaced class method
	'page/about' => [ '\\MyNamespace\\AnotherController', 'about' ],
	// Will match a custom page template registered in `page-templates/custom-page.php`
	'page/custom-page' => [ 'MyControllerClass', 'custom_page_data' ],
 	// Will match any page or single which is not registered in route
	'page' => 'any_page_data',
	'single' => 'any_single_data',

注意
“别名化”的路由不需要模板对应项。就像WordPress的模板层次结构一样,它将使用该别名的可用模板。也就是说,如果templates/page/about.php不可用,则page/about路由将使用templates/page.php

模板包装器

这受到了scribu主题包装器的启发,该包装器也实现了在Sage启动主题中。
它允许您在wrapper.php中拥有主布局,主模板将自动用其包装。它防止了使用get_header()get_footer()在每个主要模板中引起的代码重复。

模板包含

模板包装器提供了通过Wrapper类的include_template方法包含模板和通过数据的方法。

// single.php

use QtGye\TemplateWrapper\Wrapper;

<h1>This is an awesome post!</h1>

<?php
	Wrapper::include_template( 'modules/content', $content_data );
?>

为了避免在模板中声明Wrapper的命名空间,建议将其包装在全局函数中,如下所示

// functions.php

function include_template ( $template_slug ='', $data = [] ) {
	QtGye\TemplateWrapper\Wrapper::include_template( $template_slug, $data );
}

$template_slug (字符串)

相对于模板文件夹的模板文件别名。在示例中,它将加载templates/modules/content.php

$data (关联数组)

传递给包含模板的数据。注意,用户定义的全局变量在包含的模板中也是可用的,所以如果有共同的变量名,模板数据将覆盖它。

模板数据

路由调用处理传递给模板的数据。通过 反射,调用函数可能将用户定义的全局变量作为参数接收。

示例

 class MyControllerClass {
	
	// Using the $post global
	static function about_page ( $post ) {

		$data = [];

		$data['id'] = $post->ID;
		$data['slug'] = $post->name;

		// The variables $id and $slug are now available in the template
		return $data;
 
	}
 }

全局数据

用户定义的全局数据通过 src/global.php 中的 template_global_data 钩子定义。这些全局变量在模板中可用。

示例

 // src/global.php

 add_filter('template_global_data', function ( $predefined_globals )
 {
	// For some reason, use $wp_roles global
	global $wp_roles;
	
	// $wp_roles variable is now ready to be used in the templates
	return compact('wp_roles');
});

模板包装器提供了预定义的全局变量,用户定义的全局变量将合并到其中。
$wp, $wpdb, $wp_query, $post, $authordata, $page;

AJAX路由

模板包装器提供了一种使用动作处理ajax路由的方法。

通过 ajax_routes 钩子注册ajax路由。

   // src/routes.php

   add_action( 'ajax_routes', function () {

   	return array(
   		// Matches ?action=items
   		'items' => [ "MyController", "ajax" ],
   	);

   });

在ajax回调函数中,GET参数和用户定义的全局变量通过 反射 传递。

class MyController {
   
   // Handling ?action=items&filter=all&page=3
   // The argument names should match the GET parameter names. Hence, order is irrelevant.
   static function ajax ( $page, $filter, $post ) {
   	
   	// $filter === 'all'
   	// $page === '3'
   	$response = [
   		'items' => get_filtered_posts( $filter, $page),
   		'current_id' => $post->ID,
   	];
   	
   	// Returned data is sent as json-encoded response.
   	return $response;

   }
}

待办事项

  • [] 多个包装器