Leveraging content negotiation
At this point, our blog is really coming together. We are able to create, fetch, update, and delete articles. We also have some pretty solid abstractions in place to aid future growth. As mentioned earlier in the chapter, having the controller actions directly return an object can help with handling multiple response formats. For example, say we wanted to augment our application by allowing it to return an article as HTML as well as JSON, depending on the accept
header of the request.
In order to handle the generation of the HTML, we could make use of Crystal's built-in Embedded Crystal (ECR) feature, which is essentially like compile-time templating. However, it could help to have something a bit more flexible, much akin to PHP's Twig, Python's Jinja, or Embedded Ruby (ERB). There is actually a Crystal port of Jinja, called Crinja that we can leverage. So first up, add the following as a dependency to your shard.yml
, being sure to run...