wunderbar icon indicating copy to clipboard operation
wunderbar copied to clipboard

Getting Rails support to work

Open fkchang opened this issue 4 years ago • 7 comments

I'm using 1.4.2 which still has Rails support, but I get this error when a wunderbar _html template

uninitialized constant Wunderbar::HtmlMarkup

I did include it in the Gemfile as

gem 'wunderbar', '1.4.2', require: 'wunderbar/rails'

Is there another step?

fkchang avatar Feb 21 '21 02:02 fkchang

It looks like there is two problems. First, wunderbar/rails doesn't require wunderbar, which is why you are getting that error. Second, it looks like the Rails API has changed and default_format is now expected to be a symbol, not a mime type.

For now, change your gemfile to read:

gem 'wunderbar', '~ 1.4.5'

And drop the following file into your config/initializers directory, perhaps with the filename of wunderbar.rb:

module Wunderbar
  module Rails
    class HtmlHandler
      cattr_accessor :default_format
      self.default_format = :html

      def self.call(template, source=nil)
        %{
          compiled = Proc.new {#{template.source}}
          x = Wunderbar::HtmlMarkup.new(self);
          instance_variables.each do |var|
            x.instance_variable_set var, instance_variable_get(var)
          end
          x.instance_eval(&compiled)
          x._.target!
        }.strip # take care to preserve line numbers in original source
      end
    end

    class JsonHandler
      cattr_accessor :default_format
      self.default_format = :json

      def self.call(template, source=nil)
        %{
          compiled = Proc.new {#{template.source}}
          x = Wunderbar::JsonBuilder.new(self);
          instance_variables.each do |var|
            x.instance_variable_set var, instance_variable_get(var)
          end
          x.instance_eval(&compiled)
          x.target!
        }.strip # take care to preserve line numbers in original source
      end
    end

    ActionView::Template.register_template_handler :_html, HtmlHandler
    ActionView::Template.register_template_handler :_json, JsonHandler
  end
end

With that, you should be able to serve files with an _html extension. For example:

rails generate controller hello test rm app/views/hello/test.html.erb

create a file named app/views/test._html with the following contents:

_h1 "wunderbar"
_p 'it worked'

rubys avatar Feb 21 '21 15:02 rubys

That did the trick, thanks

fkchang avatar Feb 22 '21 20:02 fkchang

Can I render a haml partial from my _html template? render 'partial_name' isn't working

fkchang avatar Feb 22 '21 22:02 fkchang

I see I can't really reuse the normal render or helpers, they must output elsewhere and not to a string

fkchang avatar Feb 23 '21 00:02 fkchang

I'm pretty sure that one can have haml partials, so it must be possible. I'll try to investigate in a bit.

rubys avatar Feb 23 '21 17:02 rubys

An example of rendering a partial from with a wunderbar view. For illustrative purposes, 'bin/rails generate scaffold book title author'. Now you will have a directory named app/views/books with a file named new.html.erb. Rename that file to new._html, and replace its contents with:

_h1 'New Book'
  
_ { render 'form', book: @book }

_ { link_to 'Back', books_path }

A modification to the class initializer described above will allow wunderbar partials to be included from other sources (erb, haml, wunderbar, whatever).

In class HtmlHandler, modify

x._.target!

to read:

x._.target!.html_safe

rubys avatar Feb 24 '21 02:02 rubys

That's working for me, thanks

fkchang avatar Feb 25 '21 20:02 fkchang