wbf/mvc

An helper component for MVC in Wordpress

dev-master 2017-05-25 07:58 UTC

This package is not auto-updated.

Last update: 2024-09-28 20:25:21 UTC


README

MVC paradigm for Wordpress templates.

This component provide the ability to effectively split code from templates in Wordpress.

Usage of HTMLView

HTMLView is the default view type provided by component. You can use is to display HTML templates.

  • Create a new instance of HTMLView:
$v = new \WBF\components\mvc\HTMLView("path/to/template.php");
  • Assign some vars to the template and display it
$vars = [
    'title' => "Hello World"
];
$v->display();
  • In template.php you can:
<?php echo $title; ?>

The View has some really interesting features:

  • You can provide a path relative to the current theme or relative to a plugin. The template file will be enqueued in a template hierarchy array, so you can override the template file by placing a new template with the same name/path in a child theme. If the path is relative to a plugin, any similiar template found in child theme or in parent theme will override the original template.
    //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
    */
  • You can easily cache the $vars array to boost performances. The same task can be tedious with a simple require.
  • You can easily implement some other templating systems by extending View.

Use dashboard page wrapper

$v = new \WBF\components\mvc\HTMLView("path/to/template.php");
$v->for_dashboars()->display([
    'page_title' => "My awesome title",
    'my_name' => "Maya"
]);

With template.php:

Hello <?php echo $my_name ?>!

Will display:

<div class="wrap">
    <h1>My awesome title</h1>
    Hello Maya!
</div>

Use custom page wrapper

$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"
]);

With template.php:

Hello <?php echo $my_name ?>!

Will display:

<section class="my_wrap">
    <span>My awesome title</span>
    Hello Maya!
</section>