Important: This article covers features available in Caldera Metaplate 0.2.0-b2 and later only, and will not work with the original release.
In addition to allowing you to create the custom field templates we call Metplates using the Metaplate admin, and have them output automatically, you can also use Caldera Metaplate with templates loaded from a theme or plugin and load them using the function
Doing so allows you to use Handlebars.php to create an HTML file for templating, separate from your main PHP files. This way of working allows for an excellent separation of concerns. All of the same tempalting tools that are offered by Metaplates created in the admin are available. Working this way allows your templates to be saved in your plugin or theme instead of the database. Therefore they can be tracked by your version control system.
How It Works
Loading Metaplates From Files
Metaplates can be created in any file with the extension “html” or “htm”. They can then be rendered using the function
caldera_metaplate_from_file(), by passing the file path to the first argument of that function. This function’s second argument takes the ID of the post that you wish to use the data from, to render the template.
The file path passed to this function can either be in the current theme/ child theme:
<?php $file = 'template.html'; echo caldera_metaplate_from_file( $file, $post_id );
Or, you can pass a full file path:
<?php $file = dirname(__FILE__) . '/template.html'; echo caldera_metaplate_from_file( $file, $post_id );
Using Custom Data
Both of the previous examples use a post’s data to render the template. This pulls all post fields and post_meta fields. You can also, using the third argument of
caldera_metaplate_from_file() pass an array of template data:
<?php $file = 'template.html'; $template_data = array( 'hats' => 'Many Hats', 'shoes' => 'No shoes', 'shirts' => array( 'blue', 'red' ) ); echo caldera_metaplate_from_file( $file, null, $template_data );
Metaplate Without The Admin Interface
Everything discussed above will work with the plugin installed. If you wish to use the templating tools provided by Metaplate, without any of the admin interface or automatic output functionality, you may choose to only use the metaplate-core library.
This Composer package, which powers Metaplate’s templating, but does not include any of its admin-side functionality, can be included in any plugin or theme, via Composer. If you are unfamiliar with using Composer, please see this article.