1Mojolicious::Plugin::JSUOsNeCronCfoingt(r3i)buted Perl DMoocjuomleinctiaotuiso:n:Plugin::JSONConfig(3)
2
3
4

NAME

6       Mojolicious::Plugin::JSONConfig - JSON configuration plugin
7

SYNOPSIS

9         # myapp.json (it's just JSON with embedded Perl)
10         {
11           %# Just a value
12           "foo": "bar",
13
14           %# Nested data structures are fine too
15           "baz": ["♥"],
16
17           %# You have full access to the application
18           "music_dir": "<%= app->home->child('music') %>"
19         }
20
21         # Mojolicious
22         my $config = $app->plugin('JSONConfig');
23         say $config->{foo};
24
25         # Mojolicious::Lite
26         my $config = plugin 'JSONConfig';
27         say $config->{foo};
28
29         # foo.html.ep
30         %= config->{foo}
31
32         # The configuration is available application-wide
33         my $config = app->config;
34         say $config->{foo};
35
36         # Everything can be customized with options
37         my $config = plugin JSONConfig => {file => '/etc/myapp.conf'};
38

DESCRIPTION

40       Mojolicious::Plugin::JSONConfig is a JSON configuration plugin that
41       preprocesses its input with Mojo::Template.
42
43       The application object can be accessed via $app or the "app" function.
44       A default configuration filename in the application home directory will
45       be generated from the value of "moniker" in Mojolicious
46       ("$moniker.json"). You can extend the normal configuration file
47       "$moniker.json" with "mode" specific ones like "$moniker.$mode.json",
48       which will be detected automatically.
49
50       If the configuration value "config_override" has been set in "config"
51       in Mojolicious when this plugin is loaded, it will not do anything.
52
53       The code of this plugin is a good example for learning to build new
54       plugins, you're welcome to fork it.
55
56       See "PLUGINS" in Mojolicious::Plugins for a list of plugins that are
57       available by default.
58

OPTIONS

60       Mojolicious::Plugin::JSONConfig inherits all options from
61       Mojolicious::Plugin::Config and supports the following new ones.
62
63   template
64         # Mojolicious::Lite
65         plugin JSONConfig => {template => {line_start => '.'}};
66
67       Attribute values passed to Mojo::Template object used to preprocess
68       configuration files.
69

METHODS

71       Mojolicious::Plugin::JSONConfig inherits all methods from
72       Mojolicious::Plugin::Config and implements the following new ones.
73
74   parse
75         $plugin->parse($content, $file, $conf, $app);
76
77       Process content with "render" and parse it with Mojo::JSON.
78
79         sub parse {
80           my ($self, $content, $file, $conf, $app) = @_;
81           ...
82           $content = $self->render($content, $file, $conf, $app);
83           ...
84           return $hash;
85         }
86
87   register
88         my $config = $plugin->register(Mojolicious->new);
89         my $config = $plugin->register(Mojolicious->new, {file => '/etc/foo.conf'});
90
91       Register plugin in Mojolicious application and merge configuration.
92
93   render
94         $plugin->render($content, $file, $conf, $app);
95
96       Process configuration file with Mojo::Template.
97
98         sub render {
99           my ($self, $content, $file, $conf, $app) = @_;
100           ...
101           return $content;
102         }
103

SEE ALSO

105       Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
106
107
108
109perl v5.32.0                      2020-07-28Mojolicious::Plugin::JSONConfig(3)
Impressum