Skip to content

Latest commit

 

History

History
81 lines (50 loc) · 4.17 KB

05. cherry-template-manager.md

File metadata and controls

81 lines (50 loc) · 4.17 KB

Cherry Template Manager

General Description

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:

  1. folder - wp-contentuploads / theme_name (plugin_name) / template-name.tmpl
  2. folder in the theme - wp-contentthemestheme_name / templatestheme_name (plugin_name) / template_name.tmpl
  3. folder in the plugin - wp-content / plugins / plugin_nametemplatestheme_name (plugin_name)template_name.tmpl

Files structure

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">

Module arguments

  • template_dirstring - path to template format. By default: templates/%1$s/%2$s.tmpl
  • slugstring - product slug (set automatically). By default: ''
  • upload_dirstring - path to directory with uploads (set automatically). By default: ''
  • macros_callbackstring - 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: /\$\$.+?\$\$/

Module methods

parsed_template

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: false
  • class - 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: false
  • macros_callbackstring - 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: false
  • macros_variablestring - 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()
);
Callback & variables filters

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%%

get_template_by_name

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' );