Mojolicious::Plugin::TagHelpers::ContentBlock - Mojolicious Plugin for Content Blocks
use Mojolicious::Lite;
plugin 'TagHelpers::ContentBlock';
# Add snippets to a named content block, e.g. from a plugin
app->content_block(
admin => {
inline => "<%= link_to 'Edit' => '/edit' %>"
}
);
# ... or in a controller:
get '/' => sub {
my $c = shift;
$c->content_block(
admin => {
inline => "<%= link_to 'Logout' => '/logout' %>",
position => 20
}
);
$c->render(template => 'home');
};
app->start;
__DATA__
@@ home.html.ep
%# and call it in a template
%= content_block 'admin'
Mojolicious::Plugin::TagHelpers::ContentBlock is a Mojolicious plugin to create pluggable content blocks for page views.
Mojolicious::Plugin::TagHelpers::ContentBlock inherits all methods from Mojolicious::Plugin and implements the following new one.
# Mojolicious
$app->plugin('TagHelpers::ContentBlock');
# Mojolicious::Lite
plugin 'TagHelpers::ContentBlock';
Called when registering the plugin. Accepts an optional hash containing information on content blocks to be registered on startup.
# Mojolicious
$app->plugin(
'TagHelpers::ContentBlock' => {
admin => [
{
inline => '<%= link_to "Edit" => "/edit" %>',
position => 10
},
{
inline => '<%= link_to "Logout" => "/logout" %>',
position => 15
}
],
footer => {
inline => '<%= link_to "Privacy" => "/privacy" %>',
position => 5
}
}
);
Content blocks are defined by their name followed by either a hash of content block information or an array of content block information hashes. See content_block for further information.
The content block hash can be set as part of the configuration file with the key TagHelpers-ContentBlock
or on registration (that will be merged with the configuration).
# In a plugin
$app->content_block(
admin => {
inline => '<%= link_to 'Edit' => '/edit' %>'
}
);
# From a controller
$c->content_block(
admin => {
inline => '<%= link_to 'Edit' => '/edit' %>',
position => 40
}
);
# From a template
% content_block 'admin', { position => 9 }, begin
<%= link_to 'Edit' => '/edit' %>
% end
# Calling the content block
%= content_block 'admin'
Add content to a named content block (like with content_for) or call the contents from a template.
In difference to content_for, content of the content block can be defined in a global cache during startup or as part of the applications configuration.
Supported content block parameters, passed as a hash, are template
or inline
. Additionally a numeric position
value can be passed, defining the order of elements in the content block. If position
is omitted, the default position is 0
. Position values may be positive or negative.
When calling the content blocks, an additional list parameter separator can define a string to be placed between all blocks.
# Calling the content block
%= content_block 'admin', separator => '<hr />'
# In a template
% if (content_block_ok('admin')) {
<ul>
%= content_block 'admin'
</ul>
% };
Check if a content_block
contains elements.
https://github.com/Akron/Mojolicious-Plugin-TagHelpers-ContentBlock
Copyright (C) 2015-2021, Nils Diewald.
This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0.