![]() scrambled extension and will run that source file through our Engine by calling our compile/2 function defined in our Engine module. At compile time Phoenix will match any templates with the. You can see this is the same as I did in my app to configure phoenix_markdown. last ( word ) end end endĪnd to use this in our Phoenix app, we need to configure the file extension we want to invoke scrambling for. join ( " " ) end defp shuffle ( word ) do if String. join ( " \n " ) end defp shuffle_words_in_sentence ( sentence ) do sentence |> String. map ( & shuffle_words_in_sentence / 1 ) |> Enum. Engine, file: path, line: 1 ) end defp scramble_words ( content ) do content |> String. ![]() read! () |> scramble_words () |> Earmark. Engine def compile ( path, _name ) do path |> File. This is what I came up with for our use case:ĭefmodule RealWorldPhoenix. The behaviour requires us to implement compile/2. We’ll define our engine in lib/engine/scrambled.ex. If you want to follow along you can create a fresh phoenix project and put the files in there to test this out yourself. This however doesn’t hold us back from using the concept to build our awesome scramble engine! So let’s get cakrcnig! Our scramble Engine Here’s a nice article from a Cambridge Professor about this. Fun fact, this so-called research was actually never done at Cambridge University! Somehow this internet meme got morphed into being a Cambridge research subject, but was actually never the case. So scramble all letters in words but keep the first and last letters intact. We’ll use the Cambridge University Scrambled Text concept. So how does one go about and create a template engine? Well, the best way to find out is to see if we can create one for ourselves, right? So let’s create a template engine that scrambles all the text that gets rendered by the engine. Great stuff! How do we create a Template Engine? Does that name sound familiar? Oh, that’s because boyd was also the creator of the awesome Scenic Library. This is exactly what Boyd Multerer has created in his library phoenix_markdown. It is fairly straightforward to create your own Template Engine by implementing the behaviour. The way that eex and exs are implemented is as Phoenix Template Engines. ![]() Ofcourse, Phoenix has this all figured out. Ooohhh what is that….? It’s a Template Engine! So, without further ado, let’s crack this baby open and take a look inside. Now I can just use this and be done with it (and as you are reading this… I am actually already using it ) ), but my curious mind wants to know more! I’d like to know how this mechanism works so I have a better understanding of how rendering works in Phoenix. In fact there is even a library that handles this for us: Phoenix Markdown. We can make sure that our Phoenix app can actually render markdown files stored in our template directly to html, just like it render’s eex to html. We could of course convert our markdown in our phoenix controller directly using Earmark and output that in our view, but we can do even better. ![]() It is used by ex_doc and is a nice and well maintained library. The go-to way to convert markdown to html in Elixir land is the great library written by Dave Thomas, Earmark. So let’s find out how we can write markdown, put it under version control (just because that is nice to have) and then make sure it gets rendered as html in our Phoenix app. So most people use markdown to write blogposts and I’m no exception. Writing content for a blog.Īs most of you would probably agree, writing long-form content using html is a bit of a pain. Then I can also do some nice things like make it a bit interactive or even let people sign-up and comment without using a third-party tool for that. But as I am writing about using Phoenix in the Real World, I figured why not just create a Phoenix application to host my blog. I could of course take an off the shelf blogging platform or just use some kind of static site generator. Recently, I acquired the domain in order to continue my blogging adventure on a separate blog dedicated to writing exclusive content on Phoenix and Elixir (and some related subjects occassionally).
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |