Views
Views are PHP files whose content is merely HTML. PHP tags should only be used to echo values from variables passed by controllers or any caller.
Views are considered assets within the framework; can be seen as front-end templates as well.
Storage path
Views are stored at[path to project]/assets/views/
.
Ayuco Command
php ayuco create view:{view-key}
{view-key} | The key (or ID) that identifies the view to create. |
Keys
View keys are strings that indicate the path and filename of a view; for example, the view key shortcodes.hello-word
will point to the file ../assets/views/shortcodes/hello-world.php
.
Content
The view should consist only of front-end code; The majority of the times, the code inside the view should only be HTML with PHP tags to display dynamic data, there are special instances where you might want to include CSS and JS code as well. Here is a basic sample:
<div class="book book-<?php echo $book->ID ?>"> <h1><?php echo $book->title ?></h1> <ul> <li>Year: <?php echo $book->year ?></li> <li>Publisher: <?php echo $book->publisher ?></li> </ul> <p><?php echo $book->description ?></p> </div>
Keep business logic out
NOTE: There is no PHP business logic or algorithms in the view sample, it is a clean HTML template. It is recommended to keep logic and algorithms in controllers to respect MVC design pattern and to facilitate the job to designers.Usage
There are multiple ways of calling views.
Within a controller
Every controller can access the view
property, from which the methods get()
and show()
can be used to retrieve or print a view.
class MyController extends Controller { public function my_function() { // View is returned and assigned to variable $view $view = $this->view->get('view.key'); // View is prrinted // Array of parameters ar passed to the view $this->view->show('view.key-2', [ 'param1' => true, 'model' => MyModel::find() ]); } }
Within the Main Class
The Main Class can access the mvc
property, from which the methods view->get()
and view->show()
can be used to retrieve or print a view.
class Main extends Bridge { /** * Following example will return the view with key "view.key" */ public function return_view() { return $this->mvc->view->get( 'view.key' ); } }
Using the get_bridge global function
The function get_bridge()
, which is used to return the project's Main Class instance, has the public methods get_view()
and show()
can be used to retrieve or print a view.
// Print a view get_bridge( 'MyNamespace' )->view( 'view.key' ); // Get a view $view = get_bridge( 'MyNamespace' )->get_view( 'view.key' );
Using the theme_view global function
If the project is a theme. The global function theme_view()
is available to print a view.
<div class="sample"> <h1>Sample using theme_view()</h1> <div class="view-key"> <?php theme_view( 'view.key', $parameters ) ?> <!-- NOTE: theme_view() is only available for themes. Plugins should rely on using global variable. --> </div> </div>
Parameters
Every method or function that calls to a view will allow custom parameters to be passed to the template. Parameters are passed within an array and should include a string array key. Parameters are always sent as the second parameter after the view key.
// Parameters are passed as array to view "view.key" get_bridge( 'theme' )view( 'view.key', [ 'parameter_name_1' => 1, 'my_model' => MyModel::find( 1 ), 'db_option' => get_option( 'db_option' ), ] );
Array keys are converted by the framework into PHP variables that can be used in the view.
<div class="view.key sample"> <strong>Param 1:<strong> <?php echo $parameter_name_1 ?> <?php if ( $my_model ) : ?> <h1><?php echo $my_model->title ?></h1> <?php endif ?> <pre><?php print_r( $db_option ) ?></pre> </div>
In the example above, the array parameters defined when calling the view are converted to individual PHP variables.
Customization
Plugin views can be customized in any theme or child-theme, allowing end-user customization. Views must be pasted in the theme following the same structure of folders from which they were copied.
For example, if a view file is located at:
{plugin root}/assets/views/shortcodes/hello-word.php
The copy should be placed at:
{theme root}/assets/views/shortcodes/hello-word.php
The framework will prioritize the copies found on themes, allowing them to be customized without being overwritten or affected by plugin updates.
Customize theme folder name
By default, the framework will use the relative path {theme root}/assets/views/
to look up for customized views.
The setting paths.theme_path
, in the configuration file, will replace the default relative path.
For example, if the configuration file has the setting:
return [ // ...other settings 'paths' => [ 'base' => __DIR__ . '/../', 'controllers' => __DIR__ . '/../Controllers/', 'views' => __DIR__ . '/../../assets/views/', 'log' => WP_CONTENT_DIR . '/wpmvc/log', // Custom theme path 'theme_path' => '/my-app/', ], // ...other settings ];
The framework will look for the customized view in:
{theme root}/my-app/shortcodes/hello-word.php