1Mojolicious::Plugin::JSUOsNeCronCfoingt(r3i)buted Perl DMoocjuomleinctiaotuiso:n:Plugin::JSONConfig(3)
2
3
4
6 Mojolicious::Plugin::JSONConfig - JSON configuration plugin
7
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
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 These configuration values are currently reserved:
51
52 "config_override"
53 If this configuration value has been set in "config" in Mojolicious
54 when this plugin is loaded, it will not do anything besides loading
55 deployment specific plugins.
56
57 "plugins"
58 "plugins": [{"SetUserGroup": {"user": "sri", "group": "staff"}}]
59
60 One or more deployment specific plugins that should be loaded right
61 after this plugin has been loaded.
62
63 The code of this plugin is a good example for learning to build new
64 plugins, you're welcome to fork it.
65
66 See "PLUGINS" in Mojolicious::Plugins for a list of plugins that are
67 available by default.
68
70 Mojolicious::Plugin::JSONConfig inherits all options from
71 Mojolicious::Plugin::Config and supports the following new ones.
72
73 template
74 # Mojolicious::Lite
75 plugin JSONConfig => {template => {line_start => '.'}};
76
77 Attribute values passed to Mojo::Template object used to preprocess
78 configuration files.
79
81 Mojolicious::Plugin::JSONConfig inherits all methods from
82 Mojolicious::Plugin::Config and implements the following new ones.
83
84 parse
85 $plugin->parse($content, $file, $conf, $app);
86
87 Process content with "render" and parse it with Mojo::JSON.
88
89 sub parse ($self, $content, $file, $conf, $app) {
90 ...
91 $content = $self->render($content, $file, $conf, $app);
92 ...
93 return $hash;
94 }
95
96 register
97 my $config = $plugin->register(Mojolicious->new);
98 my $config = $plugin->register(Mojolicious->new, {file => '/etc/foo.conf'});
99
100 Register plugin in Mojolicious application and merge configuration.
101
102 render
103 $plugin->render($content, $file, $conf, $app);
104
105 Process configuration file with Mojo::Template.
106
107 sub render ($self, $content, $file, $conf, $app) {
108 ...
109 return $content;
110 }
111
113 Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
114
115
116
117perl v5.38.0 2023-09-11Mojolicious::Plugin::JSONConfig(3)