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.

Blogs, Twitter: They are business tools. Think: Chainsaws.


Twitter and blogs -- What are they? They are tools.

Other tools in your business are:

Nobody is going to tell you "All you need is a telephone and you have it made!" Wrong! *Perhaps that magic worked once -- *in 1920. But today, starry-eyed young technophiles will feed you same line that for a blog, or for Twitter.

No, these are all just power tools -- like a chainsaw; you must understand them, and use them wisely. If you just start one without preparation, bad things can happen.

Now let us see how your blog and your twitter account fit into your most important business presence -- your website.

Your website is your billboard and your brochure

Like a tri-fold cardstock brochure, a business website needs to quickly explain to your potential customer

Like a billboard or a business card -- You have twenty-five words. Make them count. If you cannot explain yourself and capture attention in 25 words, you have lost the sale -- because you do not even know yourself!

Then take a few pages to go into depth about your services or your products. If you can, write a page two levels down from the opeing page with as much detail as you can, without giving away trade secrets. Search engines love details, and so will the 10% of your customers who are truly curious about you.

The Long Tail: Where Google Thrives and Microsoft's Bing Fails

According to a March 25, 2010 article in E-Week, Yusuf Mehdi the, senior vice president of the Online Audience Group for Microsoft Bing, explains their small share of the search market:

"We missed the boat early on that the focus was about the long tail. We actually focused a lot on the head of the queries. ... It turned out the long tail was much more important... On any given a month, one-third of queries that show up on Bing, it's the first time we've ever seen that query. A huge chunk of those, we'll never see again. They're like gone with the wind. The challenge of being able to... understand that new flow of data and... index the right thing... is a very, very hard problem."

So if you are a peanut vendor in Phoenix, Bing does very well at finding "peanut phoenix" but probably fails at connecting you to "brickle delight scottsdale."

Your website will succeed if you write for the long tail -- and that means offering a deep level of detail so that Google will find your site when someone types an unusual search. Conquer the basics first and then pursue the long tail -- and you will improve your basic ranking for the top words too!

Tweeting without a Blog: Failure to Communicate

Unless you can say everything your customers and fans need to know within twenty words (140 characters including spaces), you need a place to show pictures and write full sentences. "SALE ON PEANUTS STOP HALF OFF SPECIALS STOP ENDS TUESDAY STOP" -- Do your customers want to relive the days tipping a nickel to the uniformed boy on his bicycle for that telegram? Or do you want to show them a picture of a hot fudge sundae with your lyric prose singing praises of your custom roasted peanuts? You need a blog, or your "tweets" will fall on deaf ears.

Short URLs: Negative information, bad for your site

Twitter nearly forces you to use a service that rewrites your actual company link -- -- to something like http://a.b/3 ... which gets you zero ranking in Google or anywhere else. "Short" links give you zero brand recognition. Worse, if the "a.b" service goes away, which some have already done, all your "short" links break.

Some of these services also display your page inside a frame, with ads... do you want someone else's advertisements cluttering and covering your page? Especially when you have no idea what they might be selling or promoting -- your competitor's products? Something unspeakable?

Moral: Do not use the "short URL" services. Link to your page directly.

Frequent, but not too frequent

Finding the right frequency to send "tweets" is a challenge, and depends on your industry and your audience. One danger is pestering your followers with a barrage of trivia, for that is far worse than writing a little too few entries.

if you are selling bulldozers you might tweet once a month, but your coffee shop might announce a morning special and then an evening special. The coffee shop's blog might be updated once a week with a schedule of live music and the weekly pastry menu. Match the frequency to your audience and they will follow you. Mismatch, or send anything that feels like "spam," and they will cut you loose.

Be prepared to spend an hour every day

Once you begin a blog or a Twitter page, you establish an expectation. If you fail to meet it, your site becomes stale and gets ignored.

Do not start if you without full intention of devoting sufficient resources, and that means time, every day and every week. Either you or a staff member can write, but fairly regular updates are key.

If you are not updating on a relatively set schedule, then delete the page. An outdated website is much worse than none at all.

Be aware of Protocol, but avoid Subconscious rudeness

Do you talk "at" your customers? That is much worse than talking "to" them. Preferably you should talk "with" them.

When your school principal "talked at" you in the auditorium, you probably did not hear a word he said, whether he was expounding on the honor of winning the state championship or cursing the evils of wacky tabacky. And you probably dreaded an appointment to "talk to" your guidance counselor. Much better to have a conversation, and "talk with" your friends.

Twitter breaks not only this subconscious relationship but also long standing Internet protocol with their use of the "@" symbol. My email address is -- I am located "at" my company. But Twitter wants you to talk "@bill" ... instead of talking with me, you are shouting "at" me, and it looks like you are sending email to "" -- which is some kind of online banking company.

Be mindful of using Twitter's "@" -- it can be a subconscious rudeness.

When to use a Hashtag

Many Twitter users use "hash tags" -- writing a comment like, "I had breakfast with #Bush and #Clinton this morning." That allegedly is a way to describe searches better -- presumably if you search for "#bush" you will miss all the posts about rose bushes and juniper bushes. But does #Rush refer to the political commentator, the band, or the fraternity enrolment week? Unless you are specifically 'tweeting' about hot items in the news, skip the hashtags and just write normally.

Optimizing Your Twitter site

Unless you have a personal business, you should probably use a variant of your business name as your Twitter name, and your logo as the thumbnail photo. If you are a very small business, consider using a professional looking personal photo as the main thumbnail, because it lends an extra degree of connection. Your Twitter biography is shown as the page description by the search engines, so make that concise and topical (again, twenty-five words!).

You can also customize the Twitter page background with a graphical identity of your company. This needs to be subtle, not distracting. A muted photograph with your logo overlaid creates a visual reminder (called "repetition of elements") that establishes the feel of the page.


On Twitter people can "follow" (subscribe to) your page, and you can follow others. You have little control over who follows you, but you should be careful especially on a business page about whom you follow. Keep your political views off your business page, for example -- unless you run a political business. Same with your favorite bands and celebrities. You should follow appropriate trade associations or awards groups in your industry.

Be prepared for Real Feedback

Once you start a Twitter page or blog, be prepared for the fact that real people have real things to say. Not everyone is a happy person. And usually only people write things when they are upset, not when they love your product.

Be polite to everyone, even in the face of rudeness. Remember, the world is watching. Be a bigger person than the blithering idiot and the world will understand. Stoop to their level and the world may not forget. Think twice before clicking OK.

Speak softly, and carry a big stick -- Theodore Roosevelt

Now, get out there, start connecting with people who are your future customers, and have a little fun.

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).