View
- Quick start
- Class
- Template Engine Smarty
- Shortcut function
view()
- Assigning Variables
- Output
- Accessing Class methods and objects in smarty template
Quick start
creates View Index
in the given module Foo
php emvicy module:createView Index Foo
- If module
Foo
does not exist, it will be created as a primary if possible, otherwise as a secondary one.
Find out more about Views, for example on wikipedia, "Model–view–controller#View", 2023-12-28
Class
writing the View class
- Place the View Class inside your module's View folder (see /modules/{moduleName}/View/
- Use a Pascal Case Name (see wiki.c2.com/?PascalCase) for the Class file
- The Controller must extend
\App\View
Illustration: Module Foo
, View class Index
with method doSomething
<?php
namespace Foo\View;
use App\View;
use MVC\MVCTrait\TraitViewInit;
class Index extends View
{
use TraitViewInit;
/**
* @throws \ReflectionException
*/
protected function __construct()
{
parent::__construct();
$this->caching = false;
$this->registerForSmarty();
}
/**
* @param int $iValue
* @return void
*/
public function doSomething(int $iValue)
{
display('the value is: ' . $iValue);
}
/**
* @return void
* @throws \SmartyException
*/
protected function registerForSmarty()
{
// classes
$this->registerClass('MVC\Strings', 'MVC\Strings');
// modifiers
$this->registerPlugin('modifier','floor', 'floor');
}
}
Template Engine Smarty
- Emvicy makes use of the Template Engine
Smarty
Version 4. - All Templates you define in your module's template folder.
- Emvicy provides a standard set of template files if you create your module via
php emvicy
command (see: Creating a Module). - See the directory structure of the standard set of templates: /2.x/directory-structure#modules-moduleName-templates
Smarty
For more Information about how to code templates with powerful Smarty Template Engine please visit the official Website www.smarty.net
Shortcut function view()
instead of calling your View class complete
\Foo\View\Index::init()
you can make use of the shortcut function
view()
which is defined in modules/Foo/etc/config/Foo/config/_function.php
Assigning Variables
assign any variable to your template
assign any variable to your template (assuming module is Foo
)
view()->assign('myFrontendVariable', 'Any Content');
In your template you can access that assigned variable this way:
{$myFrontendVariable}
autoAssign variables
If you created your module via Emvicy.phar (see: Creating a Module) or you added additional context information to your route by yourself, you can easily auto assign all additional route infos:
autoAssign variables to template (assuming module is Foo
)
view()->autoAssign(
Route::getCurrent()
);
Output
render
render the template
view()->render();
controlling rendering
switch on/off rendering templates
render off
Event::run('mvc.view.render.off');
render on
Event::run('mvc.view.render.on');
controlling echo out
switch on/off to echo out the rendered result
echoOut off
Event::run('mvc.view.echoOut.off');
echoOut on
Event::run('mvc.view.echoOut.on');
Accessing Class methods and objects in smarty template
You can access any Emvicy functions and Classes as well as any of your module's functions and classes directly in the templates.
Examples
// access BasePath
{MVC\Config::get_MVC_BASE_PATH()}
// display a text
{display('this is just a test')}
// shows additional info of the current route object
{info(MVC\Route::getCurrent()->get_additional())}
Also you can access methods of assigned Objects:
Example
{$oDTRoutingAdditional->get_sContent()}