I'm just a new user of Mephisto, and I find that its a mature blogging platform (that still has a lot to improve though). I'm liking it already, but I feel that there aren't much resources available for the Mephisto users. I had a hard time finding a theme that suits my taste, and so I decided to use my own

As I was writing my own layout, I had to look at liquid markup and quickly breeze through it so that I could go on with fixing my blog. To my surprise, even the Google Group for Liquid were not so kind enough to post answers to those questions related to "using helper methods in liquid layouts/template".

Its sad that I had to work on it on my own. And now that I finally learned how to do it, I''m sharing it with you.

Liquid is used for templating. If you have an application that requires for you to offer them the power to change the skin/template of the whole application but not sacrifing functionality, this is where its best used. Well, not to mention that your application should be in Rails to begin with (I think you already know that, that's why you're here). If you're familiar with the PHP Smarty templating engine, Liquid is kinda its counterpart.

These templating engines don't directly interact with the whole application via the usual ways of defining what commands can be put in the View aspect of the application. They use sections that are manually defined and registered before use. In Liquid, its not called helper, they are called drops. Let's make a quick run through of stuffs that are very important in using Liquid. (You must know ruby in order to use Liquid)

  • the tag <%= %> will be replaced by {{ }}
  • the tag <% %> will be replaced by { }
  • not all methods are available in liquid templates, see this wiki for more info
  • you can use some of the methods available, and here is an example on how the parameters get used:

 

def multiply(value1, value2)
  value1 * value2
end

And in your liquid template, {{ 3 | multiply: 2} It gets translated to: multiply(3,2). The first parameter is consumed by the method/function that comes after the pipe.

Now that you know the basics, let's move to using your own helper methods in the liquid template. If you'd take a look at your directory structure, you would find the following:

Mephisto Application Directory

Using your favorite texteditor (mine is gedit), open app/drops/site-drop.rb. You can add your helpers here. :) And so, let's say that I have this helper:

def my_name_in_uppercase
  "Maricris".upcase
end

then I can call this from my liquid template as such: {{ site.my_name_in_uppercase }}

And that's it! Remember to put all requires inside the methods you are using. You can use a separate drop file too if you want. You can do this for all helpers that you have available in application_helper.rb. But if there are specific helpers (per model or controller), it is best to write them in a separate drop file.

Happy coding!