The module allows to load and process template files (tmpl). In its turn, tmpl files contain html markup and macros which are replaced by content during files parsing.
tmpl-files load order is the following:
- folder - wp-content / uploads / theme_name (plugin_name) / template-name.tmpl
- folder in the theme - wp-content / themes / theme_name / templates / theme_name (plugin_name) / template_name.tmpl
- folder in the plugin - wp-content / plugins / plugin_name / templates / theme_name (plugin_name) / template_name.tmpl
tmpl-file can include any HTML markup and macros of two types.
PHP code in tmpl-files will not work.
Macros types:
%%MACRO_NAME%%
- macro which calls the callback for processing$$MACRO_NAME$$
- macro which receives variable value from variable attribute of the callbacks class.
Example:
<button type="submit" class="search-submit btn btn-primary">
%%ICON%%
%%SUBMIT_TEXT%%
</button>
<span class="screen-reader-text">$$READER_TEXT$$</span>
<input type="search" class="search-field" placeholder="$$PLACEHOLDER$$" value="" name="s">
template_dir
- string - path to template format. By default:templates/%1$s/%2$s.tmpl
slug
- string - product slug (set automatically). By default:''
upload_dir
- string - path to directory with uploads (set automatically). By default:''
macros_callback
- string - regular expression for callback macro which is passed in the callbacks class. By default:/%%.+?%%/
macros_variable
- string - variable macro regular expression which is passed in the callbacks class. By default:/\$\$.+?\$\$/
The method loads tmpl-files and parses callbacks and variables macros, replacing them with their results. The method returns HTML.
template_name
- string - name of the loaded template (without *.tmpl extension). Example: search-form. By default: falseclass
- string | stdClass - class name or sample. If class name is passed as string, method parsed_template tries to get its sample. If sample is passed, methods starts to work with it right away. All methods for callback macros should be public. Data for variables macro should be stored as an array inside $variables array of the passed class. The property should also be public. By default: falsemacros_callback
- string - regular expression for callback macro, which is passed in callback class. the given argument is set individually and will be of the top priority, compared to the module macros_callback argument. By default: falsemacros_variable
- string - regular expression for variable macros, which is passed in callback class. The given argument is set individually and will be of the top priority, compared to the module macros_variable argument. By default: false
Example:
$template_manager = Cherry_Template_Manager::get_instance();
$template_manager->parser->parsed_template(
'your-template-name',
your_callback()
);
You can rewrite or add new Callbacks and variables values. For that you can use 2 filters, one for variables and the second for callbacks.
They have the following formats:
- Variable -
{$product_slug}_set_variable_{$macro_name}
. In lower case. Example: cherry-search_set_variable_placeholder. The macro in tmpl-file looks like that -$$PLACEHOLDER$$
- Callback -
{product_slug}_set_callback_{$macro_name}
. In lower case. Example: cherry-search_set_callback_submit_text. The macro in tmpl-file looks like that -%%SUBMIT_TEXT%%
The method loads tmpl-file content. Search result is returned with a string. If the template is not found, returns false value.
name
- string - name of the loaded template (without *.tmpl extension). Example: search-form-input. By default:false
Example:
$template_manager = Cherry_Template_Manager::get_instance();
$template_manager->loader->get_template_by_name( 'your-template-name' );