Posterous theme by Cory Watilo

How to render locomotive cms pages from your own rails controllers

UPDATE: it turns out all this code only works correctly on ruby 1.9.2, I've tried to deploy this to 1.8.7 ree server and the menu item did not show up in the admin interface and also the permission settings for authors did quite work.

Few days ago I've stumbled on to nice example how to add some custom functionality to your locomotivecms site. As far as I needed to fix and improve myliufoto.lt albums and previously used refinerycms had really undfriendly interface for adding photos I've decided to remake a whole site using locomotive.

After all the admin stuff was done I needed to show the album list and the photo pages. As far as my data was not in locomotive's own content models and I needed custom routing like /portfolio/album-url/3 and so on I could not use default locomotive rendering to do it.

With the help of did (creator of locomotivecms) I've made a custom method that can render any locomotive page with my own instance variables passed to liquid. Locomotive already has all the rendering and variable setting for liquid implemented in Locomotive::Render::InstanceMethods module, so after some wandering around we've decided to base our code on that.

Here's all the code you need to do that:

Basically I've copied the 'render_locomotive_page' method and made it to accept a path for a page i.e. 'portfolio/photo' that would match page's slug in the cms.

To make it all work few small changes needed to be made:

In the render.rb file:
1. params[:path] has to be set to 'path'
2. then the locomotive_context needed to be changed to include controller instance variables so added the method 'my_context' that uses original locomotive method

In the controller you want to use it:
3. your controller should 'include Locomotive::Routing::SiteDispatcher' so that it has access to some locomotive's methods like 'current_site'

Everything else is left to the locomotive to handle so it should not brake unless the whole rendering is rewritten.

 

| Viewed
times
Filed under: