Giter VIP home page Giter VIP logo

view's Introduction

Usages

Install dependency into your project.

composer require wp-strap/view

This exposes some classes which are responsible loading template files inside plugins (but can also be used for themes).

The classes follow PSR practices with interfaces, so it can be included trough OOP with dependency injection and IoC containers. It also provides a Facade class that allows you to use static methods instead to call the methods everywhere you like.

Example with using the facade:

use WPStrap\View\View;

// Resolves instance and registers project configurations
View::register([
    'dir' => plugin_dir_path(__FILE__), // or get_stylesheet_directory() for themes
]);

echo View::render('my-component')->args([
    'my-argument' => 'my-value'
])

Example with using the instance

use WPStrap\View\View;
use WPStrap\View\ViewService;

// Instantiates the Asset service and registers project configurations
$views = new ViewService();

$views->register([
    'dir' => plugin_dir_path(__FILE__), // or get_stylesheet_directory() for themes
]);

// Renders template with arguments
echo $views->render('my-component')->args([
    'my-argument' => 'my-value'
])

// You can also use the facade based on this instance.
View::setFacade($views);
View::render('my-second-component');

Example with using instance as function

use WPStrap\View\ViewInterface;
use WPStrap\View\ViewService;

function views(): ViewsInterface {
     static $views;
     
     if(!isset($views)) {
        $views = (new ViewService())->register([
            'dir' => plugin_dir_path(__FILE__), 
        ]);
     }
     
     return $views;
}

echo views()->render('my-component')->args([
    'my-argument' => 'my-value'
])

Example with using the League Container

use League\Container\Container;
use WPStrap\View\View;
use WPStrap\View\ViewInterface;
use WPStrap\View\ViewService;

$container = new Container();
$container->add(ViewsInterface::class)->setConcrete(ViewService::class)->addMethodCall('register', [
    'dir' => plugin_dir_path(__FILE__), 
]);

$views = $container->get(ViewsInterface::class);

echo $views->render('my-component')->args([
    'my-argument' => 'my-value'
])

// You can also set a PSR container as facade accessor
View::setFacadeAccessor($container);
View::render('my-second-component');

Base settings

It locates the template files from the "views" folder by default (eg: your-plugin-folder/views ).

$views->register([
    // Determines the root dir of your project
    'dir' => plugin_dir_path(__FILE__), 
    
    // Will change templates path to "your-plugin-folder/path-to-views/views"
    'path' => 'path-to-views', 
    
    // Changes "your-plugin-folder/views" to "your-plugin-folder/templates"
    'folder' => 'templates', 
    
    // Will override templates from theme/child-themes if templates exist in the
    // "clients-theme/my-plugin-name" directory, this is turned off by default
    // to remove the performance load since it won't be needed for most plugins
    'locate' => 'my-plugin-name', 
    
    // By default it uses the plugin folder name as hook prefix for filters inside the
    // classes (eg: a filter for "my-plugin-folder" becomes "my_plugin_folder_view_args")
    // With this setting you can change the prefix
    'hook' => 'my_plugin_hook', 
]);

Domain folder

If you have a domain folder structure like this

my-custom-plugin/
├── src/                  
│   ├── Blocks/
│   │    └── Static/     
│   │         ├── css/  
│   │         ├── js/  
│   │         ├── views/  
│   │         │    ├── example-block.php  
│   │         │    └── another-example-block.php
│   │         └── images/  
│   ├── Admin/             
│   │    ├── Admin.php
│   │    └── Static/
│   │         ├── css/  
│   │         ├── js/  
│   │         ├── views/  
│   │         │    ├── admin-page.php  
│   │         │    └── another-admin-page.php
│   │         └── images/  
│   ├── Main/        
│   │    ├── Main.php     
│   │    └── Static/
│   │         ├── css/  
│   │         ├── js/  
│   │         └── images/  

You can use the first param of the render() method to point to the domain folder

$views->register([
    'dir' => plugin_dir_path(__FILE__), 
    'path' => 'src'
 ]);
 
echo $views->render('Blocks', 'my-example-block')->args([
    'my-argument' => 'my-value'
]);

If you use another name for "Static" you can use the "entry" setting to change it to something else

$views->register([
    'dir' => plugin_dir_path(__FILE__), 
    'path' => 'src',
    'entry' => 'templates'
 ]);

view's People

Watchers

Brandon Kramer avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.