View Class

The View class is responsible for rendering the views in WP Bones

Accessing the View Class

Usually you will get the instance of the View class from the Plugin Class. Here is an example of how to get the instance of the View class:

Your Controller
$view = WPKirk()->view( 'dashboard.index' );

Methods

render()

Synopsis

  /**
   * Get the view content.
   *
   * @param bool $asHTML  Optional. Set to TRUE to get the content of view as string/html.
   * @return mixed
   */
  public function render($asHTML = false)

Usage

The render() method is used to get the view content. Here is an example of how to get the view content:

Your Controller
return WPKirk()->view( 'dashboard.index' )->render();

Usually you won’t use the render() method directly. Anyway, it could be useful if you want to get the view content as a string.

Your Controller
$html = WPKirk()->view( 'dashboard.index' )->render(true);

See also the toHTML() method.

đź’ˇ

You can get the view by using echo directly in the controller. Here is an example of how to get the view content using echo:

Your Controller
echo WPKirk()->view( 'dashboard.index' );

Fragment

Remember that you can get any view in anytime. That’s useful for the fragment concept. You can get a view and use it in another view.

Your Controller
$fragment = WPKirk()->view( 'dashboard.fragment' )->render();
return WPKirk()->view( 'dashboard.index' )
               ->with( [ 'fragment' => $fragment ] );

Or inside your view directly

resources/views/dashboard/index.php
<div class="my-class">
    <h1>Hello, the main Dashboard view</h1>
    <?php echo WPKirk()->view( 'dashboard.fragment' )->render(); ?>
</div>

toHTML()

Synopsis

/**
 * Return the content of view.
 *
 * @return string
 */
public function toHTML(): string

with()

Synopsis

/**
 * Data to pass to the view.
 *
 * @param mixed $data Array or single data.
 *
 * @return $this
 * @example     $instance->with( [ 'foo' => 'bar' ] )
 *
 * @example     $instance->with( 'foo', 'bar' )
 */
public function with($data): View

Usage

The with() method is used to pass data to the view. Here is an example of how to pass data to the view:

Your Controller
return WPKirk()->view( 'dashboard.index' )
               ->with( 'kirk', 'Class' );

A variable named $kirk will be inject and available in the view file.

resources/views/dashboard/index.php
<div class="my-class">
    <h1>Hello, the main Dashboard view</h1>
     <?php echo $kirk; /* you will see Captain */ ?>
</div>

You may also pass an array of data to the view:

Your Controller
return WPKirk()->view( 'dashboard.index' )
               ->with( [ 'kirk' => 'Captain' ] );

with multiple variables:

Your Controller
return WPKirk()->view( 'dashboard.index' )
               ->with( [ 'kirk' => 'Captain', 'spock' => 'Commander' ] );

withAdminAppsScript()

Synopsis

/**
 * Load new Javascript (React bundle) resource in admin area.
 *
 * @param string $name      Script handle the data will be attached to.
 * @param bool   $module    Optional. There is a module css.
 * @param string $variabile Optional. Name for the JavaScript object
 * @param array  $data      Optional. The data itself. The data can be either a single or multi-dimensional array.
 */
public function withAdminAppsScript($name, $module = true, $variabile = '', $data = []): View

withAdminScript()

Synopsis

/**
 * Adds extra code to a registered script.
 *
 * @param string $name      Name of the script will be attached to.
 * @param string $data      String containing the JavaScript to be added.
 * @param string $position  Optional. Whether to add the inline script before the handle
 *                          or after. Default 'after'.
 *
 * @return $this
 */
public function withInlineScript($name, $data, $position = 'after'): View

Usage

The withAdminScript() method is used to load an admin script. Here is an example of how to load an admin script:

Your Controller
return WPKirk()->view( 'dashboard.index' )
               ->withAdminScript( 'wp-kirk-common' );

The file public/js/wp-kirk-common.js will be loaded in the admin area.


withAdminStyle()

Synopsis

/**
 * Load a new css resource in admin area.
 *
 * @param string $name Name of style.
 * @param array  $deps Optional. Array of slug deps
 * @param array  $ver  Optional. Version.
 *
 * @return $this
 */
public function withAdminStyle($name, $deps = [], $ver = []): View

Usage

The withAdminStyle() method is used to load an admin style. Here is an example of how to load an admin style:

Your Controller
return WPKirk()->view( 'dashboard.index' )
               ->withAdminStyle( 'wp-kirk-common' );

The file public/css/wp-kirk-common.css will be loaded in the admin area.


withInlineScript()

Synopsis

/**
 * Adds extra code to a registered script.
 *
 * @param string $name      Name of the script will be attached to.
 * @param string $data      String containing the JavaScript to be added.
 * @param string $position  Optional. Whether to add the inline script before the handle
 *                          or after. Default 'after'.
 *
 * @return $this
 */
public function withInlineScript($name, $data, $position = 'after'): View

Usage

The withInlineScript() method is used to load an inline script. Here is an example of how to load an inline script:

Your Controller
return WPKirk()->view( 'dashboard.index' )
               ->withAdminScript( 'wp-kirk-common' )
               ->withInlineScript('wp-kirk-common', 'console.log("Hello, Captain!");');

withInlineStyle()

Synopsis

/**
 * Adds extra style to a registered style.
 *
 * @param string $name      Name of the script will be attached to.
 * @param string $data      String containing the JavaScript to be added.
 *
 * @return $this
 */
public function withInlineStyle($name, $data): View

Usage

The withInlineStyle() method is used to load an inline style. Here is an example of how to load an inline style:

Your Controller
return WPKirk()->view( 'dashboard.index' )
               ->withAdminStyle( 'wp-kirk-common' )
               ->withInlineStyle('wp-kirk-common', 'body { background-color: #f00; }');

withLocalizeScript()

Synopsis

/**
 * Adds a new Localizes a script.
 *
 * @param string $handle Script handle the data will be attached to.
 * @param string $name   Name for the JavaScript object
 * @param array  $l10n   The data itself. The data can be either a single or multi-dimensional array.
 *
 * @return $this
 */
public function withLocalizeScript($handle, $name, $l10n): View

Usage

The withLocalizeScript() method is used to pass the localized strings to the JavaScript file. Here is an example of how to pass the localized strings to the JavaScript file:

Your Controller
$localization_data = array(
    'greeting' => __('Hello, World!', 'wp-kirk'),
    'farewell' => __('Goodbye, World!', 'wp-kirk'),
    'ajax_url' => admin_url('admin-ajax.php'),
);
 
return WPKirk()->view( 'dashboard.index' )
               ->with( [ 'kirk' => 'Captain' ] )
               ->withAdminScript( 'wp-kirk-common' )
               ->withLocalizeScript('wp-kirk-common', 'wpKirkGlobal', $localization_data);
wp-kirk-common.js
(function($) {
  'use strict';
  
  alert(wpKirkGlobal.greeting);
 
})(jQuery);

withScript()

Synopsis

/**
 * Load a new css resource in theme.
 *
 * @param string $name Name of script.
 * @param array  $deps Optional. Array of slug deps
 * @param array  $ver  Optional. Version.
 *
 * @return $this
 */
public function withScript($name, $deps = [], $ver = []): View

withStyle()

Synopsis

/**
 * Load a new css resource in theme.
 *
 * @param string $name Name of style.
 * @param array  $deps Optional. Array of slug deps
 * @param array  $ver  Optional. Version.
 *
 * @return $this
 */
public function withStyle($name, $deps = [], $ver = []): View