<!--toc-->
|
|
CppCMS 1.2 introduces a new plugin API together with new improved support of dynamic templates
|
CppCMS 1.2 introduces a new [plugin API](https://github.com/artyom-beilis/cppcms/blob/master/cppcms/plugin.h) together with new improved support of dynamic templates
|
|
## Writing Plugins
|
|
Plugins are shared object that can register their functions within `cppcms::plugin::manager` singleton. For example:
|
Plugins are shared object that can register their functions within [`cppcms::plugin::manager`](https://github.com/artyom-beilis/cppcms/blob/master/cppcms/plugin.h#L134) singleton. For example:
|
|
For example you write some class that implements a certain API:
|
|
namespace myplugin {
|
class my_class : public plugin_api {
|
public:
|
statuc my_class *create(std::string const ¶meter)
|
{
|
return new my_class(parameter);
|
}
|
...
|
};
|
}
|
|
Than you can register it within the `plugin::manager` using special macro.
|
|
CPPCMS_PLUGIN_ENTRY(myplugin,my_class::create,plugin_api *(std::string const &))
|
|
Note: the 3rd parameter that defines the signature of the entry for typesafe plugin access.
|
|
Than you can access the required plugins from the main code calling
|
|
// callback object
|
booster::callback<plugin_api *(std::string const &)> cb;
|
// get it from plugin
|
cb = cppcms::manager::instance().entry<plugin_api *(std::string const &)>("myplugin","my_class::create");
|
// call it
|
plugin_api *ptr = cb("foo");
|
|
## Loading plugins
|
|
`cppcms::service` allows to load the plugins [from the configuration](/wikipp/en/page/cppcms_1x_config#plugin)
|
Plugin lifetime is managed by [`cppcms::plugin::scope`](https://github.com/artyom-beilis/cppcms/blob/master/cppcms/plugin.h#L56) class. `cppcms::service` provides an instance of one and allows to load the plugins [from the configuration](/wikipp/en/page/cppcms_1x_config#plugin)
|
|
You can also use `plugin::scope` independently so you can have separate lifetime of `cppcms::service` and your plugins.
|
|
You can also link the views with the same plugin and they will be loaded automatically.
|
|
## Template System Support
|
|
In order to create dynamically loaded templates from plugins, there are several improvements in template systems:
|
|
You can create [abstract and inline views](/wikipp/en/page/cppcms_1x_templates_block#view) so plugins can easily extend them.
|
|
Using new [view helpers](/wikipp/en/page/cppcms_1x_templates_comm#Views.Helpers) you can call specific functions from templates by their names
|
|
<% using master_api as m from a_skin, "api" %>
|
<% include foo() from m %>
|
<% end using %>
|
|
You can use specific `gettext` [domain](/wikipp/en/page/cppcms_1x_templates_comm#Selecting.localization.domain) for each of the views separately so plugin can run withing its own domain transparently from its caller. |