wbf / mvc
Wordpress MVC 中的辅助组件
dev-master
2017-05-25 07:58 UTC
Requires
- wbf/utils: dev-master
This package is not auto-updated.
Last update: 2024-09-28 20:25:21 UTC
README
Wordpress 模板中的 MVC 范式。
此组件提供了在 Wordress 中有效地将代码与模板分离的能力。
HTMLView 的使用
HTMLView 是组件提供的默认视图类型。您可以使用它来显示 HTML 模板。
- 创建 HTMLView 的新实例
$v = new \WBF\components\mvc\HTMLView("path/to/template.php");
- 将一些变量分配给模板并显示它
$vars = [ 'title' => "Hello World" ]; $v->display();
- 在 template.php 中,您可以
<?php echo $title; ?>
视图有一些非常有趣的功能
- 您可以提供相对于当前主题或相对于插件的相对路径。模板文件将被添加到模板层次数组中,因此您可以通过在子主题中放置具有相同名称/路径的新模板来覆盖模板文件。如果路径相对于插件,则子主题或父主题中找到的任何类似模板都将覆盖原始模板。
//If you are in a twentysixteen child called "foobar"... $v = new \WBF\components\mvc\HTMLView("path/to/template.php"); /* Will looking for (in order): /wp-content/themes/foobar/path/to/template.php /wp-content/themes/twentysixteen/path/to/template.php */
- 您可以将 $vars 数组轻松缓存以提高性能。使用简单的
require
完成相同任务可能会很繁琐。 - 通过扩展视图,您可以轻松实现其他一些模板系统。
使用仪表板页面包装器
$v = new \WBF\components\mvc\HTMLView("path/to/template.php"); $v->for_dashboars()->display([ 'page_title' => "My awesome title", 'my_name' => "Maya" ]);
使用 template.php
Hello <?php echo $my_name ?>!
将显示
<div class="wrap"> <h1>My awesome title</h1> Hello Maya! </div>
使用自定义页面包装器
$v = new \WBF\components\mvc\HTMLView("path/to/template.php"); $v->display([ 'page_title' => "My awesome title", 'wrapper_class' => "my_wrap", 'wrapper_el' => "section" 'title_wrapper' => "<span>%s</span>" 'my_name' => "Maya" ]);
使用 template.php
Hello <?php echo $my_name ?>!
将显示
<section class="my_wrap"> <span>My awesome title</span> Hello Maya! </section>