Posts Tagged ‘erb’

Preview (0.5.5): The New Rendering Engine

Tuesday, May 20th, 2008

In the latest version of Mack the rendering engine has been completely re-written from the ground up. With this comes some new features, some incompatibility, and most importantly, extensibility. Let’s jump on in and see what we can expect with this release.

Incompatibility

  • Gone is <%= @content_for_layout %> in layouts. In is <%= yield_to :view %>.
  • Gone is render(options_hash) in controllers/views. In is render(type, value, options_hash)
    Examples:
    render(:action => :new) is now render(:action, :new)
    render(:url => “http://www.mackframework.com”, :parameters => {:message => “hi”}) is now render(:url, “http://www.mackframework.com”, :parameters => {:message => “hi”})
  • Gone is *.xml.erb. In is *.xml.builder
Let’s quickly talk about how these incompatibilities have come about. First there were several bugs that needed to be addressed with the rendering engine. For example, if you set an instance variable in a view, it wasn’t available in the layout. That’s a pain if you want to do things like programatically set the page title. There were also ‘hacks’ used to do things like render xml using the Builder::XmlMarkup library. It wasn’t clean, but it worked. Finally, the rendering engine itself wasn’t that extensible. All of that has now changed.

Render Me Softly

In the new rendering engine there are two parts to the system, Mack::Rendering::Type::* objects and Mack::Rendering::Engine::* objects. Let me explain the difference.

Mack::Rendering::Type::*

A type is something like :action, :text, :inline, :url, etc… That is the type of thing you want to do. I want to render an action. I want to render a url, etc… There are classes for each of these types, and you can easily add your own. These types do all sorts of work before they pass it off to an engine, if need be. For example, in the case of Mack::Rendering::Type::Partial the render method does the work of inserting an ‘_’ in the appropriate place, so the file can found.

<%= render(:partial, "users/form") %> # => "users/_form"

Once that happens it tries to find an engine to process the partial.

Mack::Rendering::Engine::*

An engine does the actual work of rendering the io, with the binding of the Mack::Rendering::ViewTemplate object, it’s been given by the results of the render method in the Mack::Rendering::Type::* object. Engine examples would be, Erubis (ERB), Markaby, Haml, and Builder::XmlMarkup, all of which are included with Mack in this release. New engines can easily be plugged in and registered with the system.

Coming soon a tutorial on adding PDF::Writer support using the new system.

Release 0.5.0

Tuesday, May 6th, 2008

I know I’ve been talking about this release for a while now, and here it finally is. There’s some good bug fixes in here, a few new rake tasks, and some other little things like that.

The biggest thing, however, is what’s been causing a stir online now for the past few days, I’ve split out the ORM support into separate gems. I’m not going to go over it again, you can find all the details here: http://www.mackframework.com/2008/05/04/orms-and-mack/

Needless to say this is a good release to get, if not for the bug fixes alone, but for the great db:create rake tasks that are now in there. Thanks, again, to Darsono Sutedja for his great contributions.

Changelog

  • Added rake db:create and db:create:all rake tasks.
  • Refactored out constants, such as MACK_ROOT and MACK_ENV and made them into Mack::Configuration.env, Mack::Configuration.root, etc…
  • Added test:stats and test:coverage Rake tasks.
  • Removed support for ActiveRecord and DataMapper and moved them into their own gems.
  • Fixed a bug where yields in ERB weren’t giving the desired results.
  • DataMapper database.yml file can now be nested, and is generated as such.
  • Cleaned up some tests, and refactored the HTML generation stuff.
  • Fixed a bug with the scaffold generating ‘bad’ methods instead of ‘post’
  • Made error_messages_for more compatible with DataMapper.
  • Fixed a bug loading url.rb and uploaded_file.rb found by Phil Darnowsky
  • [dsutedja] Render method (in controller_base) now accepts response status code.
  • gem: mack_ruby_core_extensions 0.1.23
  • gem: genosaurus 1.1.4
  • gem: datamapper 0.3.2
  • gem: rcov 0.8.1.2.0
  • gem: mack-data_mapper 0.5.0
  • gem: erubis 2.6.0

Release 0.4.6

Friday, April 18th, 2008

This is a pretty cool release. First off Thin has been updated to 0.8.0, a rather nice, stable release. ERB has been replaced with Erubis to give a significant improvement in speed, which, after all is one of Mack’s most important traits. 

There’s been a general overhaul of the generators included with Mack. Mostly, cleaning them up. With that said things like the scaffold, model, and migration generators have been updated to do what a lot of people have been asking for, that is take a ‘cols’ parameter and build out the generated code corrrectly. This really makes life, very, very simple.

There will be a re-written ‘blog’ tutorial coming out soon to make use of this, as well as migration support that’s now part of Mack.

Very exciting stuff! Enjoy!

Changelog:

  • Mack now uses Erubis, http://www.kuwata-lab.com/erubis/, for it’s rendering engine instead of ERB. This makes Mack even faster now! Yippie!
  • Added rake generate:model name=<model_name> (optional: cols=<col_1>:<col_1_type>,<col_2>:<col_2_type>) This will also create a migration for you.
  • Updated rake generate:migration name=<model_name> (optional: cols=<col_1>:<col_1_type>,<col_2>:<col_2_type>) This will create the proper table migration for you.
  • Updated rake generate:scaffold to use the ModelGenerator now.
  • ScaffoldGenerator now create input fields based on the type of columns, if any, passed in.
  • Overall general refactoring of the generator classes.
  • Fixed a bug with request logging not, well, logging.
  • gem: erubis 2.5.0
  • gem: thin 0.8.0

Release 0.3.0

Wednesday, March 19th, 2008

I’ve been holding back this release so I could get distributed routing into it, but it appears that there’s still a little more work that needs to be done before it’s ready to go. I’m hoping to get it out by the beginning of next week, but don’t quote me on that.

Instead of focusing on what didn’t make it in, let’s talk about what did make it in! There’s some cool stuff in this release.

Format Driven Content

Mack now allows you to drive different content based on the format requested. For example:

/posts – will render app/views/posts/index.html.erb
/posts.html – will also render app/views/posts/index.html.erb
/posts.xml – will render app/views/posts/index.xml.erb – A special note *.xml.erb files, despite their name, do NOT get run through ERB, instead they use the XML Builder library
/posts.js – will render app/views/posts/index.js.erb
etc…

Alternatively, in your action you can now define ‘want’ blocks, to run specific code based on the format. Example:

class PostsController
  def index
    # find all the posts in the system
     @posts = Post.find(:all)
    wants(:html) do
      # this will only be run if html is requested.
      # we need a username for a 'welcome message in the view'
      @username = @user.username
    end
    wants(:xml) do
      # this will only be run if html is requested.
      # find the last published date
      @last_pub_date = Rss.find_last_by_date_by_object(:posts)
    end
  end
end

XML Builder Support

I’m not going to go into this, there is another nice post coming shortly that will explain how to use this library to add RSS to our blog demo. Here’s the post.

Built-in Encryption

In every app I’ve ever built I found the need to use encryption. Whether it’s for encrypting something into a cookie, a password in the database, or a file on disk, we all need encryption, so I’ve baked the crypt gem into Mack.

At the very simple level you can easily do this in your code:

@my_encrypted_value = _encrypt("hello world")

and you’ll be returned a nice pieced of garbled data using the Crypt/Rijndael library. Decrypting is just as easy:

_decrypt(@my_encrypted_value) # => "hello world"

See, I told you it couldn’t be easier. It gets even better you can even define your own ‘worker’ to implement other encryption schemes. It’s as simple as this:

class Mack::Utils::Crypt::HorribleWorker
  def encrypt(value)
    value.reverse
  end
  def decrypt(value)
    value.reverse
  end
end

_encrypt("hello", :horrible) # => "olleh"
_decrypt("decrypt", :horrible) # => "hello"

See how easy that was? You can also do:

@my_encrypted_value = "Hello".encrypt
@my_encrypted_value.decrypt #=> "Hello"

Either way it’s now easy to handle encryption in your funky cool Mack app.

Changelog:

  • Ticket: #8 Xml Builder Support
  • Ticket: #7 Ability to drive certain content based on ‘format’
  • Ticket: #9 Added a global encryption system to make encrypting/decrypting of strings easy to use
  • gem: builder 2.1.2
  • gem: crypt 1.1.4