inc2734 / wp-view-controller
WordPress主题视图控制器的库。
12.4.6
2024-06-01 04:18 UTC
Requires
- php: >=7.4
Requires (Dev)
- dev-master
- 12.4.6
- 12.4.5
- 12.4.4
- 12.4.3
- 12.4.2
- 12.4.1
- 12.4.0
- 12.3.1
- 12.2.4
- 12.2.3
- 12.2.2
- 12.2.1
- 12.2.0
- 12.1.7
- 12.1.6
- 12.1.5
- 12.1.4
- 12.1.3
- 12.1.2
- 12.1.1
- 12.1.0
- 12.0.5
- 12.0.3
- 12.0.2
- 12.0.1
- 11.2.2
- 11.2.1
- 11.2.0
- 11.1.0
- 11.0.1
- 11.0.0
- 10.1.2
- 10.1.1
- 10.1.0
- 10.0.0
- 9.0.0
- 8.2.0
- 8.1.2
- 8.1.1
- 8.1.0
- 8.0.5
- 8.0.3
- 8.0.2
- 8.0.0
- 7.5.0
- 7.4.1
- 7.4.0
- 7.3.1
- 7.3.0
- 7.2.1
- 7.2.0
- 7.1.2
- 7.1.1
- 7.1.0
- 7.0.4
- 7.0.3
- 7.0.2
- 7.0.1
- 7.0.0
- 6.0.2
- 6.0.1
- 6.0.0
- 5.0.6
- 5.0.5
- 5.0.4
- 5.0.3
- 5.0.2
- 5.0.1
- 5.0.0
- 4.0.6
- 4.0.5
- 4.0.4
- 4.0.3
- 4.0.2
- 4.0.1
- 4.0.0
- 3.0.0
- 2.2.0
- 2.1.4
- 2.1.3
- 2.1.2
- 2.1.1
- 2.1.0
- 2.0.0
- 1.0.0
- 0.1.4
- 0.1.3
- 0.1.2
- 0.1.1
- 0.1.0
- dev-develop
This package is auto-updated.
Last update: 2024-08-31 05:03:43 UTC
README
安装
$ composer require inc2734/wp-view-controller
如何使用
<?php
// in functions.php
new Inc2734\WP_View_Controller\Bootstrap();
<?php
// in page template
use Inc2734\WP_View_Controller\Bootstrap;
Bootstrap::layout( 'right-sidebar' );
Bootstrap::render( 'front-page' );
目录结构
themes/your-theme-name/
├─ vendor # → Composer packages (never edit)
├─ page-template # → Custom page templates directory
├─ templates # → Base templates directory
│ ├─ layout
│ │ ├─ wrapper # → Layout templates
│ │ ├─ header # → Header templates
│ │ ├─ sidebar # → Sidebar templates
│ │ └─ footer # → Footer templates
│ ├─ view # → View templates
│ └─ static # → Static view templates
└─ index.php, page.php ect...
布局模板
布局模板需要 <?php $_view_controller->view(); ?>
。
视图模板
在单页中
加载视图模板 /templates/view/content-{post-type}.php
。当 /templates/view/content-{post-type}.php
不存在时,加载 /templates/view/content.php
。
在存档页中
加载视图模板 /templates/view/archive-{post-type}.php
。当 /templates/view/archive-{post-type}.php
不存在时,加载 /templates/view/archive.php
。
静态视图模板
尝试根据URL加载视图模板。例如,当URL是 http://example.com/foo/bar 时,尝试从 /templates/static/foo/bar.php
或 /templates/static/foo/bar/index.php
加载。
使用视图控制器
use Inc2734\WP_View_Controller\Bootstrap;
Bootstrap::layout( 'right-sidebar' );
Bootstrap::render( 'content', 'news' );
模板标签
\Inc2734\WP_View_Controller\Helper::get_template_part()
此方法是 get_template_part()
的扩展。
过滤器钩子
inc2734_wp_view_controller_config_path
/**
* Change config path
*
* @param string $path
* @return string $path
*/
add_filter(
'inc2734_wp_view_controller_config_path',
function( $path ) {
return $path;
}
);
inc2734_wp_view_controller_pre_template_part_render
/**
* Define and return the content before loading the template.
* If a string is returned, the template will not load.
*
* @param null $html
* @param string $slug
* @param string $name
* @param array $vars
* @return string
*/
add_filter(
'inc2734_wp_view_controller_pre_template_part_render',
function( $html, $slug, $name, $vars ) {
return $html;
},
10,
4
);
inc2734_wp_view_controller_template_part_render
/**
* Customize the rendered HTML
*
* @param string $html
* @param string $slug
* @param string $name
* @param array $vars
* @return string
*/
add_filter(
'inc2734_wp_view_controller_template_part_render',
function( $html, $slug, $name, $vars ) {
return $html;
},
10,
4
);
inc2734_wp_view_controller_layout
/**
* Change layout file
*
* @param string $layout The layout file template slug
* @return string
*/
add_filter(
'inc2734_wp_view_controller_layout',
function( $layout ) {
return $layout;
}
);
inc2734_wp_view_controller_view
/**
* Change view file
*
* @param string $view The view file template slug
* @return string
*/
add_filter(
'inc2734_wp_view_controller_view',
function( $view ) {
return $view;
}
);
inc2734_wp_view_controller_config
/**
* Customize config values
*
* @param array $values
* @return array
*/
add_filter(
'inc2734_wp_view_controller_config',
function( $values ) {
return $values;
}
);
inc2734_wp_view_controller_controller
/**
* Change controller
*
* @param string $template
* @return string
*/
add_filter(
'inc2734_wp_view_controller_controller',
function( $template ) {
return $template;
}
);
inc2734_wp_view_controller_get_template_part_args
/**
* Customize template part args
*
* @param array $args
* @var string $slug
* @var string $name
* @var array $vars
* @return array
*/
add_filter(
'inc2734_wp_view_controller_get_template_part_args',
function( $args ) {
return $args;
}
);
inc2734_wp_view_controller_template_part_root_hierarchy
/**
* Customize root hierarchy
*
* @param array $hierarchy
* @param string $slug
* @param string $name
* @param array $vars
* @return array
*/
add_filter(
'inc2734_wp_view_controller_template_part_root_hierarchy',
function( $hierarchy, $slug, $name, $vars ) {
return $hierarchy;
},
10,
4
);
inc2734_wp_view_controller_located_template_slug_fallback
/**
* You can specify a template for fallback
*
* @param string|null $fallback_slug
* @param array $relative_dir_paths
* @param string $slug
* @param string $name
* @return string|null
*/
add_filter(
'inc2734_wp_view_controller_located_template_slug_fallback',
function( $fallback_slug, $relative_dir_paths, $slug, $name ) {
return $fallback_slug;
},
10,
4
);
inc2734_wp_view_controller_render_type
/**
* Change rendered type.
*
* loop ... Loading in the WordPress loop
* direct ... Simply loading
*
* @param string $render_type loop or direct
* @return string
*/
add_filter(
'inc2734_wp_view_controller_render_type',
function( $render_type ) {
return $render_type;
}
);
动作钩子
inc2734_wp_view_controller_get_template_part_<slug>-<name>
/**
* Define template
*
* @deprecated
*
* @param array $vars
* @return void
*/
add_action(
'inc2734_wp_view_controller_get_template_part_<slug>-<name>',
function( $vars ) {
?>
HTML
<?php
}
);
inc2734_wp_view_controller_get_template_part_<slug>
/**
* Define template
*
* @deprecated
*
* @param string $name
* @param array $vars
* @return void
*/
add_action(
'inc2734_wp_view_controller_get_template_part_<slug>',
function( $name, $vars ) {
?>
HTML
<?php
},
10,
2
);
inc2734_wp_view_controller_get_template_part_pre_render
/**
* Fire before template rendering
*
* @param array $args
*/
add_action(
'inc2734_wp_view_controller_get_template_part_pre_render',
function( $args ) {
// Do something
}
);
inc2734_wp_view_controller_get_template_part_post_render
/**
* Fire after template rendering
*
* @param array $args
*/
add_action(
'inc2734_wp_view_controller_get_template_part_post_render',
function( $args ) {
// Do something
}
);
inc2734_wp_view_controller_expand_get_template_part
/**
* If return false, do not expand get_template_part().
* The various hooks that extend get_template_part added by this library will no longer be available.
*
* @param boolean $expand
* @param array $args
* @return boolean
*/
add_filter(
'inc2734_wp_view_controller_expand_get_template_part',
'__return_false'
);