ahadinyoto / refinerycms-hooks

Enable markup for RefineryCMS Page content.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Hooks extension for Refinery CMS

A "Refinery CMS":http://refinerycms.com/ extension to make Page a bit more dynamic.

In Refinery CMS, a Page is where you put your content. The Page, however, is static. Occasionally, having dynamic behavior in the content is needed. For instance, if I were to use the Page to display a product with a price. It'd be nice if I can display the price dynamically based on the latest update in my back-end database.

The "hook" will do just that. It allows you to put a simple markup with a tag - {{tag}} - in a Page "Body" and "Side Body", and based on the code that you write in the hook, the markup will be rendered accordingly.

Therefore, in my Page "Body, I can put something like:

Product "A", price: {{price|productA}}

The "price" hook code will be called, and its result will be printed. How to write hook is documented below. It's really easy.

This hook is simply to add a bit of intelligent and makes it more dynamic to an otherwise static Page content.

Installation

In your Refinery CMS application Gemfile, add:

gem 'refinerycms-hooks', :git => 'git://github.com/ahadinyoto/refinerycms-hooks.git'

Then run

bundle install
rails g refinery:hooks
bundle exec rake db:migrate

Writing your hook

In your Refinery CMS application root directory, run generator to create the hook.

rails generate refinery:hooks:scaffold Test1Hook test1

Two files will be created:

app/hooks/test1.rb
app/hooks/views/test1.html.erb

The app/hooks/test1.rb is to write the logic - I call this Hook Controller, and the other is to write the views similar to Rails' Views.

Any instance variables defined in Hook Controller will be automatically available in the view (in this case: test1.html.erb). Again, just like Rails' Views.

If you'd rather not using the view but render a text directly from Hook Controller, you can use:

def hook(args)
  # ... your codes
  render_hook("<b>Hello</b>")
end

Passing arguments

You can pass arguments to your hook for greater flexibility:

In your Page Body, say, you add ("one,two" after "|" are arguments):

{{test1|one,two}}

Then in your Hook Controller, you can simply access those arguments with:

def hook(args)
  # args = ["one", "two"]
  render_hook("#{args[0]} and #{args[1]}")
end

Changing the hooks path

By default, the hooks are generated in the application's app/hooks. You can change them at config/initializers/refinery/hooks.rb.

Disabling the hook

You can go to Refinery CMS Admin page to disable a hook (or even put an alternate message for the disabled hook).

If you really need to totally remove the hook, you can run:

rails destroy refinery:hooks:scaffold Test1Hook test1

Removing the files alone is not sufficient as the hook will still be registered with Refinery CMS.

About

Enable markup for RefineryCMS Page content.

License:MIT License


Languages

Language:Ruby 100.0%