View Engines
In order to use one of the supported templating systems, you need to edit app/Core/View in your plugin or theme project and extend the relevant base class. The code for each of the options is below.
This is done for you automatically when you select a view engine using forme codegen to generate your initial boilerplate - we're outlining the code below if you need to do this manually.
Platine
// app/Core/View.php
<?php
declare(strict_types=1);
namespace VendorName\NameSpace\Core;
use Forme\Framework\View\PlatineView;
final class View extends PlatineView
{
}Platine is Forme's default - it's a fork of Plates 4 alpha, which was abandoned. The maintainer's development is currently focused on Plates 3 below.
Why is Platine the default? This is partly a historical choice - we use it ourselves for most client projects and will therefore continue to support it - we find its slightly terser syntax is a marked improvement over Plates v3.
We maintain Platine as a hard fork, so it won't be going away any time soon. We have been adding new features, and will fix any critical bugs that crop up.
Validation
We've added some template error exceptions to enforce proper template syntax style, specifically:
- No one line if statements
- No multi-line php statements
- No more than one statement per line
- No use of echo
- No variable assignment
- Short/alternative syntax only for control structures
These are quite useful to ensure old school WordPress patterns don't creep in, but YMMV. You can switch them off by setting the global constant FORME_PLATINE_VALIDATION to false in your wp-config.php.
phtml support
If for any reason you don't want to use files with the .php extension, you can also use .phtml to differentiate them and Forme will pick these up.
Further documentation
We be looking at porting over the Plates 4 documentation to Platine in due course, but in the meantime check the project readme
Plates
// app/Core/View.php
<?php
declare(strict_types=1);
namespace VendorName\NameSpace\Core;
use Forme\Framework\View\PlatesView;
final class View extends PlatesView
{
}This is the currently maintained version. The main advantage of Plates over other options is that it's essentially just php, so there is no syntax as such to learn, although there are helper methods. It's more about following a set of conventions.
Blade
// app/Core/View.php
<?php
declare(strict_types=1);
namespace VendorName\NameSpace\Core;
use Forme\Framework\View\BladeView;
final class View extends BladeView
{
}This is of course Laravel's view engine, which is in fact originally based on .net's Razor.
Forme uses jenssegers/blade under the hood.
Caching
You will find the cache folder at FORME_PRIVATE_ROOT/view-cache in case you need to purge/delete it. We'll be adding a codegen command for this at some point, but you can rm -r it for now.
Directives
All the directives from Sage Directives are available in Blade templates. We haven't tested all of them - but they should mostly work so long as they don't use any Sage specific functionality. You can call arbitrary php functions within blade in any case, so these are mostly for convenience/syntactic sugar.
We'll look at providing a way of adding custom blade directives from within forme projects in a future release.
Twig
// app/Core/View.php
<?php
declare(strict_types=1);
namespace VendorName\NameSpace\Core;
use Forme\Framework\View\TwigView;
final class View extends TwigView
{
}Twig is Symfony's view engine. It's a little more verbose than the other options, and doesn't allow any arbitrary php.
Caching
The cache will be on if WP_ENV is production, off if it's development. You can find the cache folder at FORME_PRIVATE_ROOT/view-cache in case you need to purge/delete it.
Functions
In Twig you can't call arbitrary php functions like you can in Blade or Plates files. Forme allows this capability by wrapping them in function or the shortcut fn.
{# single.twig #}
<html>
<head>
<!-- Add whatever you need in the head, and then...-->
{{ function('wp_head') }}
</head>
<!-- etc... -->
<footer>
Copyright © {{ "now"|date('Y') }}
</footer>
{{ fn('wp_footer') }}
</body>
</html>You can also pass arguments into the function as follows.
{# single.twig #}
<div class="admin-tools">
{{ function('edit_post_link', 'Edit', '<span class="edit-link">', '</span>') }}
</div>