====== Creating a front-end page ====== We just learned how to [[.:tutorial#premier-plugin|write our first plugin]]. Let's see now how to create a //dynamic// page on the blog. We are going to create a ''Public example'' plugin, its files will be located in a ''/publicExample/'' directory. ===== The _define.php file ===== registerModule( /* Name */ 'Public example', /* Description */ 'A public page', /* Author */ 'Dotclear Documentation', /* Version */ '0.1', /* Permissions */ 'usage,contentadmin' ); ?> ===== The _prepend.php file ===== First, we register the URL in the ''_prepend.php'' file : url->register('publicexample','publicexample', '^publicexample(?:/(.+))?$',array('publicExampleDocument','page')); ?> The parameters of the register function are : - the URL type, used by Dotclear to identify it - the base URL of the page - the URL regular expression - a //callback// : a function name or an array containing a class name and a function to call when the URL is visited To access this URL, whatever the context is, we will use this code: $core->blog->url.$core->url->getBase('publicexample') ''publicexample'' is the URL type. ===== The _public.php file ===== ==== Creating the page ==== The "publicexample" URL that we registred in the ''_prepend.php'' files calls the ''page'' function of the ''publicExampleDocument'' class. This function takes one parameter : the URL regular expression. Here ''^publicexample(?:/(.+))?'' select text right after ''publicexample/'' if there's one. The plugin will display a different message if the "publicexample/lorem" URL is asked. class publicExampleDocument extends dcUrlHandlers { public static function page($args) { global $core; # $_ctx is the context, we can add to it everything we want $_ctx =& $GLOBALS['_ctx']; # $_ctx->publicExample will be a string # if the URL is "publicexample/lorem" if ($args == 'lorem') { $_ctx->publicExample = __('Lorem ipsum'); } # else, if the URL is "publicexample" else { $_ctx->publicExample = __('Public'); } # save the directory that contains the default template # the public_example.html file $core->tpl->setPath($core->tpl->getPath(), dirname(__FILE__).'/default-templates/'); self::serveDocument('public_example.html','text/html'); } } The ''$_ctx->publicExample'' is defined according to the ''$args'' value. It will be used later. ==== Tag declaration ==== See [[..:tpl|Creation of a tpl tag for a plugin or a theme]] for more informations. # Declaration statement of the {{tpl:Public}} tag $core->tpl->addValue('PublicExampleValue', array('publicExampleTpl','PublicExampleValue')); class publicExampleTpl { public static function PublicExampleValue() { return('publicExample); ?>'); } } ==== The complete _public.php file ==== publicExample will be a string # if the URL is "publicexample/lorem" if ($args == 'lorem') { $_ctx->publicExample = __('Lorem ipsum'); } # else, if the URL is "publicexample" else { $_ctx->publicExample = __('Public'); } # save the directory that contains the default template # the public_example.html file $core->tpl->setPath($core->tpl->getPath(), dirname(__FILE__).'/default-templates/'); self::serveDocument('public_example.html','text/html'); } } # Declaration statement of the {{tpl:Public}} tag $core->tpl->addValue('PublicExampleValue', array('publicExampleTpl','PublicExampleValue')); class publicExampleTpl { public static function PublicExampleValue() { return('publicExample); ?>'); } } ?> ===== The template file ===== The template file public_page.html is ocated in the ''default-templates'' directory. It contains the following code: {{tpl:lang Public example}} - {{tpl:BlogName encode_html="1"}} {{tpl:include src="_head.html"}}
{{tpl:include src="_top.html"}}

{{tpl:lang Public example}}

{{tpl:include src="_footer.html"}}
It uses the the {{tpl:PublicExampleValue}} tag that we defined in the _public.php file. ===== Download the example plugin ===== [[http://lab.dotclear.org/raw-attachment/wiki/plugin/publicExample/plugin-publicExample.zip|plugin-publicExample.zip]]