Using Mojolicious templates with JSON


Although templates are primarily used with HTML when writing programs in Mojolicious, it is entirely possible and quite simple to use them to respond to any of the MIME types that Mojolicious::Types defines. You merely use the appropriate one-word type name along with .ep, like this Mojolicious::Lite example:

@@ index.json.ep
% use Mojo::JSON qw(to_json);
%== to_json { foo => 3 };

Here, we permit the /index route to respond with JSON by using Mojo's convenient module. We use the double-equal percent-print form so that quote characters and other HTML-ish bits are output as-is, not ampersand-encoded.

It is important to not have empty lines above or below the content, as the newlines would appear in the output.

Also note the use of to_json (which returns characters) rather than encode_json (which returns bytes) to avoid double-encoding UTF8 characters.

With that in mind, you should be able to produce any file format you desire.

What's a blog?


Blogging is where you write an article or two every day, and it really does have to be at least one a day, about an issue.

Usually blogs attempt to develop a community of readers, all of whom get to discuss each post on the blog site.

Blogs are mostly used to discuss political and social issues. The comments are wide-ranging and often become shouting matches.

If you want to push an issue, to get people excited about something, a blog is great.

If you're trying to sell something, you'll either wind up with silence (which is bad) or people shouting and complaining (worse).