I’ve seen a few people ask about creating dynamic pages. Usually, this
comes into play with custom extensions whereby the goal is generate
user-specific pages. That is, the page is different each time a person
visits it.
I’m relatively new to Rails/Radiant/Ruby, but I am attempting to build a
church site that presents dynamic content. I thought I’d make what I’ve
learned available:
- In the Radiant SiteController allow for sessions by commenting the
following:
class SiteController < ApplicationController
#session :off
- In your custom extension disable caching. I attempted to disable
this in the environments/development.rb file (or others) however on
successive page loads caching kicks back in. This is because this fact
is soon forgotten… I’m not sure what causes this, but by adding the
following to the “def activate” of my own custom extension, I make sure
this fact is not forgotten.
#disable caching
ResponseCache.defaults[:perform_caching] = false
- One reason I wanted to do this was to allow for flash messages (the
Rails variety) to appear on the site when the user did some invalid
action. I created a Radius “flash” tag to dump the flash message.
tag ‘flash’ do |tag|
kind = tag.attr[‘kind’] || ‘notice’
message = response.session[‘flash’][kind.to_sym]
html = []
if message
html << “
html << message
html << “
end
html.join("\n")
end
Then I thought better of this design. The problem with dynamic pages is
that usually there are just tiny bits of information that are custom
(user name, flash messages, etc.). When we make use of these custom
bits in a common page layout, we are basically resigning to sacrifice
caching if only to display these tiny customizations. Ugh.
Instead, I think it may be better to create custom pages handled by the
controllers associated with your custom extension that handle certain
url routes, avoiding Radiant altogether (I had been doing this anyway).
In this way, only the pages that need to be dynamic are dynamic and
Radiant continues to cache, to our benefit.
One issue was on the non-Radiant pages that the current username
appeared in the upper left (with a logout link) and on Radiant pages
they did not. It was inconsistent. It seemed silly to drop caching if
only to display a username in the upper left. Instead, I am now
thinking I should simply avoid displaying the username altogether, to
maintain the consistency.
Also, in cases where I want to tell the user something in a flash
message, I simply redirect_to a custom view that is designed for
fielding flash messages out of the context of a cached Radiant content
page.
You could, of course, follow the steps I mention to allow for a more
dynamic site; however, Radiant will have to do much more work rendering
pages. Obviously, you could add some intelligence to when and where
this kicks in. This is touchy and I’m not sure there’s a good way of
handling this on Radiant generated pages. Remember, when you’re logged
in, there are potentially dozens of others visiting the same pages.
Anyway, this is what I’ve learned. I’m still learning and would be glad
to hear any criticisms/tips.
~Mario