MightyPHP Views are built around the Twig templating engine. There are however, some custom functions that are built into MightyPHP View.
Returning views from controllers are easy. Simply use the view() function.
namespace Application\Controllers;​use Application\Controllers\Controller;use MightyCore\Routing\Request;​class FooController extends Controller{public function bar(){return view('foobar');}}
The above example will return a view from the Views/foobar.html
file. It is also possible to shove your view files into directories, for the purpose of better structure.
// This will return view from Views/foo/foobar.htmlreturn view('foo/foobar');
The view() function accepts a second parameter as array, which will be used for data rendering.
return view('foobar', ['name' => 'Bob','age' => 21]);
You can then access the data from your view similar to how Twig templating does it.
In order to get the accurate relative path, it is best if it is injected by the view component. Hardcoding of URL's within the view is dangerous as it renders the application inflexible. All the URLs may not be relatively true when the application environment changes.
In order to bind a URL to your public assets, simply use the {{ asset(url) }}
syntax. For example:
<img src="{{ asset('img/avatar.jpg') }}" />
There may be times where you wish to change the path of asset()
, you can do so within the Helpers asset
function. The file is located within /Utilities/Helpers/asset.php
.
/Utilities/Helpers/asset.phpfunction asset($url){return ROOT_PATH.$url;}
The default path is prefixed with the ROOT_PATH
, of which you can add on with any as you see fit.
Translation files are within the Utilities/Lang
folder. The folder structure is further broken down to respective languages.
<span>{{ trans('foo.hello') }}</span>
Within your Lang folder:
<?php​// Utilities/Lang/en/foo.php​return ["hello" => "Hello World"];
​