Giter VIP home page Giter VIP logo

erector's Introduction

Erector

DESCRIPTION

Erector is a view framework. That is, it helps you generate HTML mixing in dynamic content (like erb, slim or haml). Unlike erb, slim, or haml, views are objects, not template files, which allows the full power of object-oriented programming (inheritance, modular decomposition, encapsulation) in views. See the rdoc for the Erector::Widget class to learn how to make your own widgets, and visit the project site at http://erector.github.io/erector for more documentation.

No, seriously, we've got hella docs at http://erector.github.io/erector -- go check it out.

SYNOPSIS

    require 'erector'

    class Hello < Erector::Widget
      def content
        html do
          head do
            title "Hello"
          end
          body do
            text "Hello, "
            b @target, :class => 'big'
            text "!"
          end
        end
      end
    end

    Hello.new(:target => 'world').to_html
    => "<html><head><title>Hello</title></head><body>Hello, <b class=\"big\">world</b>!</body></html>"

    include Erector::Mixin
    erector { div "love", :class => "big" }
    => "<div class=\"big\">love</div>"

REQUIREMENTS

The gem depends on rake and treetop, although this is just for using the command-line tool, so deployed applications won't need these. The Rails-dependent code is now separated so you can use Erector cleanly in a non-Rails app.

INSTALL

To install as a gem:

  • sudo gem install erector

Then add "require 'erector'" to any files which need erector.

To install as a Rails plugin:

  • Copy the erector source to vendor/plugins/erector in your Rails directory.

When installing this way, erector is automatically available to your Rails code (no require directive is needed).

TESTS

Three spec rake tasks are provided: spec:core (core functionality), spec:erect (the erector command line tool), and spec:rails (rails integration).

rake spec will run the complete set of specs.

CONTRIBUTING

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

See web site docs for more details.

CREDITS

Core Team:

  • Alex Chaffee
  • Jim Kingdon

Special Thanks To:

  • Abby (Chaffee's muse & Best friend)
  • Brian Takita
  • Jeff Dean
  • John Firebaugh
  • Nathan Sobo
  • Nick Kallen
  • Alon Salant
  • Andy Peterson

VERSION HISTORY

see History.txt

LICENSE: MIT

see LICENSE.txt

SEE ALSO

erector's People

Contributors

aeon avatar ageweke avatar ahto avatar alexch avatar btakita avatar dsander avatar jasonbarnabe avatar jkingdon avatar mikelewis avatar nakajima avatar ndp avatar needfeed avatar nestegg avatar thewoolleyman avatar zelig avatar zilkey avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

erector's Issues

Weirdness with rendering Erector::Widgets from Views in Rails 3.1

I'm using Rails 3.1 and am getting stuck on using Rails helpers (link_to, routes, etc). I get this error:

class Something < Erector::Widget
  def content
    link_to 'joe'
  end
end

# Used in Haml views like:
= Something.new.to_html

undefined method `link_to' for nil:NilClass
/bundler/ruby/1.9.1/bundler/gems/erector-cb01e061f9ed/lib/erector/rails3.rb:51:in `link_to'

ruby 1.9 #to_html

When I try to call to_html in 0.8 in ruby 1.9.1 or 1.9.2 I get a weirdly escaped output. I was trying to find out what is going on but with no luck. Any ideas?

>> require 'erector'
=> true
>> Erector::InlineWidget.new { span 'asd' }.to_html
=> "[\"<span>\", \"asd\", \"</span>\"]"

rubygems?

Hi,

Can we get the new version pushed to rubygems.org?

Nested capture craziness

Hey all,

Banging my head against the desk. Can someone tell me why this doesn't work?

x = capture_content {
  form_tag("/posts") do
    div { submit_tag 'Save' }
  end
}

rawtext(x)

renders

<div><input name=\"commit\" type=\"submit\" value=\"Save\" /></div> before <form accept-charset=\"UTF-8\" action=\"/posts\" method=\"post\">, instead of inside the form block.

Sinatra helpers in Erector template

Background: Sinatra 1.4.5, Erector 0.10.0, Ruby 2.1.2

I'm using Erector in a Sinatra app. Rather than creating a Tilt registration for Erector, I decided to just declare some Widgets in other ruby files:

require 'rubygems'
require 'erector'

module MySinatraApp
  class FooWidget < Erector::Widget
    def content
      # bla bla
    end
  end
end

Then I require the widget in my main sinatra application, and when I want to render a FooWidget, I make an instance of it and to_html it:

require 'rubygems'
require 'sinatra'
require_relative 'views/foo_widget.rb'

module MySinatraApp
  class App < Sinatra::Base
    get '/' do
      FooWidget.new.to_html
    end
  end
end

Now what I want to do is invoke Sinatra helpers in the body of the FooWidget. For example, I might have some CSS file in my FooWidget, being linked to via some path /css/app.css. But this might fail if my app was actually hosted at example.com/myapp. I want to use the to helper instead. So somehow, I need to get the to helper into the widget.

The heart of the problem (at least it looks that way to me) is that the to helper has to be evaluated in the context of the sinatra app instance, so my current solution is to wrap the helper in a Proc and pass it to the widget like this:

# in the Sinatra app
get '/' do
  FooWidget.new(:to => Proc.new { |url| to url }).to_html
end

# in FooWidget
def content
  link :rel => stylesheet, :href => @to.call('/css/app.css')
end

This seems to work. I'd definitely call it an ugly hack though. Is there a better or clever-er way? I'm willing to restructure the widget and where it fits into my application, if that enables a better solution.

simple_form compatibility

# config/initializers/erector.rb
Erector::Rails.def_simple_rails_helper(:simple_form_for)


# view/listings/edit.rb
class Views::Listings::Edit < Views::Layouts::Main

  def main
    simple_form_for(@listing) do |f|
      f.input :title
      f.input :body
      f.input :contact_name
      f.input :contact_email
    end
  end
end

This only renders the last input field. To make it work as expected, i have to change the view to:

simple_form_for(@listing) do |f|
  f.input(:title) +
  f.input(:body) +
  f.input(:contact_name) +
  f.input(:contact_email) 
end

I'm at a complete loss for where to start, so any help is much appreciated.

stop supporting Rails 2

If you're using Rails 2, freeze on the current version (0.9 or 0.10) since I doubt it will be supported much longer.

RSpec-rails + Erector

Hey all,

Trying to get erector views loaded into a view spec similarly to the way haml works with rspec-rails. Doesn't seem to be working:

describe "welcome/sign_up.rb" do
  it "renders the html5 doctype" do
    render
    rendered.should be_starts_with('<!DOCTYPE html>')
  end
end

The error we get is:

NameError: undefined local variable or method `render' for #<RSpec::Core::ExampleGroup::Nested_1:0x007fe5ea5a6c70>
/Users/abhik/.rvm/gems/ruby-1.9.2-p290@cbt/gems/rspec-expectations-2.7.0/lib/rspec/matchers/method_missing.rb:9:in `method_missing'

Is there a standard way to test Erector views? Should we be using rspec-rails?

Abhik

erector support for fields_for_with_nested_attributes

We have a model which declares accepts_nested_attributes_for on one of its has_many associations. When we try to call fields_for for that association, we get a TypeError: "can't convert nil into String". This is coming from ActionView's FormHelper class:

actionpack-2.3.2/lib/action_view/helpers/form_helper.rb:1023:in 'fields_for'
actionpack-2.3.2/lib/action_view/helpers/form_helper.rb:1021:in 'fields_for_nested_model'
actionpack-2.3.2/lib/action_view/helpers/form_helper.rb:1010:in 'fields_for_with_nested_attributes'
actionpack-2.3.2/lib/action_view/helpers/form_helper.rb:1009:in 'map'
actionpack-2.3.2/lib/action_view/helpers/form_helper.rb:1009:in 'fields_for_with_nested_attributes'
actionpack-2.3.2/lib/action_view/helpers/form_helper.rb:950:in 'fields_for'
erector-0.8.1/lib/erector/rails/rails_form_builder.rb:12:in 'send'
erector-0.8.1/lib/erector/rails/rails_form_builder.rb:12:in 'method_missing'

Have you seen this problem before? We're considering extending Erector::RailsFormBuilder to implement fields_for_with_nested_attributes in a manner similar to the way you've done form_for and fields_for. Would you recommend this approach, or something else?

Thanks from the Grockit team.

HTML::raw calls to_s on widget

Erector::Widget#to_s is deprecated. Please use #to_html instead. Called from erector/lib/erector/html.rb:142:in `raw'

command line tool needs to escape single quotes in raw text

Just tried to convert an HTML file with a Google Analytics script; got this:

  script :type => 'text/javascript' do
    text '//'
    rawtext '<![CDATA[
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-12345-1']);

when it should have been this:

  script :type => 'text/javascript' do
    text '//'
    rawtext '<![CDATA[
  var _gaq = _gaq || [];
  _gaq.push([\'_setAccount\', \'UA-12345-1\']);

Can't run tests on ruby 2.0

I tried to take a whack at the circular require problem but I can't even get started.

10004 % bundle install
Using rake (10.1.0) 
Using abstract (1.0.0) 
Using activesupport (3.0.20) 
Using builder (2.1.2) 
Using i18n (0.5.2) 
Using activemodel (3.0.20) 
Using erubis (2.6.6) 
Using rack (1.2.8) 
Using rack-mount (0.6.14) 
Using rack-test (0.5.7) 
Using tzinfo (0.3.38) 
Using actionpack (3.0.20) 
Using mime-types (1.25.1) 
Using polyglot (0.3.3) 
Using treetop (1.4.15) 
Using mail (2.2.20) 
Using actionmailer (3.0.20) 
Using arel (2.0.10) 
Using activerecord (3.0.20) 
Using activeresource (3.0.20) 
Using addressable (2.3.5) 
Using diff-lcs (1.2.5) 
Using multipart-post (1.2.0) 
Using faraday (0.8.8) 
Using git (1.2.6) 
Using hashie (2.0.5) 
Using multi_json (1.8.2) 
Using nokogiri (1.5.10) 
Using httpauth (0.2.0) 
Using jwt (0.1.8) 
Using multi_xml (0.5.5) 
Using oauth2 (0.9.2) 
Using github_api (0.10.1) 
Using tilt (2.0.0) 
Using haml (4.0.4) 
Using highline (1.6.20) 
Using bundler (1.3.5) 
Using json (1.8.1) 
Using rdoc (3.12.2) 
Using jeweler (1.8.8) 
Using json_pure (1.8.1) 
Using predicated (0.2.6) 
Using thor (0.14.6) 
Using railties (3.0.20) 
Using rails (3.0.20) 
Using rr (1.1.2) 
Using rspec-core (2.14.7) 
Using rspec-expectations (2.14.4) 
Using rspec-mocks (2.14.4) 
Using rspec (2.14.1) 
Using sexp_processor (4.4.0) 
Using ruby_parser (3.2.2) 
Using ruby2ruby (2.0.6) 
Using rubyforge (2.0.4) 
Using sass (3.2.12) 
Using wrong (0.7.1) 
Your bundle is complete!
Use `bundle show [gemname]` to see where a bundled gem is installed.
10005 % rake
RUBY_VERSION=2.0.0
Releasing gems to Rubyforge is deprecated. See details at http://wiki.github.com/technicalpickles/jeweler/migrating-from-releasing-gems-to-rubyforge
BUNDLE_GEMFILE='/Users/ryan/Work/git/erector/Gemfile-rails' bundle exec rake spec:core spec:erect spec:integration_rails3
Could not find rake-10.0.1 in any of the sources
Run `bundle install` to install missing gems.
rake aborted!

This project needs some love for contributors to be able to contribute. I'm not the one to give it.

Should InlineWidget override capture?

Hi, I've just made some changes in Tilt to support erector templates. I struggled a bit with using yield from the template, using #capture from an InlineWidget passing a block defined outside the widget would evaluate the block in the context where it was originally declared, maybe InliteWidget should override capture evaluating the block with instance_eval instead of yield.

Here's my commit to tilt, I overrode capture:

maca/tilt@a5776e5

"respond_to?" With one parameter is deprecated

def respond_to?(name)
super || helpers.respond_to?(name)
end

 def respond_to?(name, include_private = false) 
   super || helpers.respond_to?(name, include_private) 
 end 

Avoids using deprecated syntax (you are supposed to be explicit now)

<your method call> uses the deprecated method signature, which takes one parameter

Not sure what you want the default to be, but not adding a default could be a breaking change for some applications.

#depends_on raises error when called with more than one dependency

According to doc #depends_on can be called like this

depends_on :js, '1.js', '2.js'

but it raises error. There are no test for it so it can be only misleading doc. But I thing following patch solves this issue.

diff --git a/lib/erector/externals.rb b/lib/erector/externals.rb
index 8b2ee8b..fba01b0 100644
--- a/lib/erector/externals.rb
+++ b/lib/erector/externals.rb
@@ -21,7 +21,7 @@ module Erector
       #   depends_on :js => ["foo.js", "bar.js"], other_option=>:blah
       def depends_on(*args)
         x = interpret_args(*args)
-        my_dependencies.push(x)
+        my_dependencies.push(*x)
       end

       # deprecated in favor of #depends_on

Early return prevents tags from being closed properly

Consider this code:

require 'rubygems'
require 'erector'

class Example < Erector::Widget
  def content
    div do
      return if some_early_condition_guard?

      span "Some additional stuff"
    end
  end

  def some_early_condition_guard?
    true
  end
end

puts Example.new.to_html

This example prints <div>. The div is not closed properly because of the return.

This seems like a pretty significant bug to me. In my mind, one of the big benefits of erector is that it's block syntax guarantees that all tags will be closed properly...or so I thought.

Rails 3.x and erector

Hi I'm using rails 3.1.3 and erector git (master) in Gemfile and seem to be running into 'uninitialized constant ActionView::CompiledTemplates::Views' if erector is used. Is there documentation of some sort on how to use erector with rails 3?

circular require blows up on ruby 2.0

I'm not sure what the status of erector is atm, but I found this error in the rendering stage of my project matrix:

warning: loading in progress, circular require considered harmful - tmp/gems/erector-0.9.0/lib/erector/output.rb
    from bin/seattlerb_dashboard:194:in `<main>'
    from tmp/gems/erector-0.9.0/lib/erector.rb:11:in `<top (required)>'
    from tmp/gems/erector-0.9.0/lib/erector/output.rb:1:in `<top (required)>'
    from tmp/gems/erector-0.9.0/lib/erector/abstract_widget.rb:6:in `<top (required)>'

(heavily cleaned up for readability)

Dependencies not emitted in Rails

When using Erector with Rails, the dependencies are not emitted at all in the head tag.

I'm using Rails 2.3.10 with Erector 0.8.1 on Ruby 1.9.2.

I tracked the problem down to the fact that in Rails context, the widgets are rendered with the output which uses Rails' output buffer, which is a string. Since the buffer is a string, the output.placeholder implementation doesn't work (it works if the buffer is an Array).

I tried initializing Rails view buffer to an empty array and patching Rails TextHelper::concat to work with Arrays, but it seems that Rails internals really don't like anything other than strings for output buffer.

'javascript' command fails to pretty-print output

When using the javascript command to contain some javascript the output script tags and contents are not indented correctly i.e:

html :xmlns => 'http://www.w3.org/1999/xhtml' do
  head do
    javascript "Cufon.replace('#content');"
    javascript '$(document).ready(function(){  $(document).pngFix(); });'
  end
  body do
  end
end

produces:

<html xmlns="http://www.w3.org/1999/xhtml"> 
  <head> 
    <script type="text/javascript"> 
// <![CDATA[
Cufon.replace('#content');
// ]]>
</script> 


    <script type="text/javascript"> 
// <![CDATA[
$(document).ready(function(){  $(document).pngFix(); });
// ]]>
</script> 

</head> 
  <body></body> 
</html> 

Erector should not turn helpers which return into helpers which emit

When used with rails, erector currently wraps helper methods so that methods which return HTML, like link_to, will emit output instead.

This is not what rails developers expect, but it also creates problems which do not have easy solutions.

For example, consider the following HAML code to output some tabs with a vertical bar between the tabs.

- tabs = [link_to("File")]
- tabs << link_to("Edit") unless params[:readonly]
- tabs << link_to("Admin") if params[:admin]
= tabs.join(" | ").html_safe

This generates something like "File | Edit | Admin" (with the exact menu items depending on the parameters).

Translating to Erector looks like this:

tabs = [link_to("File")]
tabs << link_to("Edit") unless params[:readonly]
tabs << link_to("Admin") if params[:admin]
text tabs.join(" | ").html_safe

and the result is something like "FileEditAdmin |" because link_to emits rather than returning.

The rails helper to_sentence also suffers from this problems.

With the enclosed patch, the above erector code works.

diff --git a/lib/erector/rails3.rb b/lib/erector/rails3.rb
index 39a770b..ab9972b 100644
--- a/lib/erector/rails3.rb
+++ b/lib/erector/rails3.rb
@@ -48,7 +48,7 @@ module Erector
       def def_simple_rails_helper(method_name)
         module_eval <<-METHOD_DEF, __FILE__, __LINE__+1
           def #{method_name}(*args, &block)
-            text helpers.#{method_name}(*args, &block)
+            helpers.#{method_name}(*args, &block)
           end
         METHOD_DEF
       end

Is the project not maintained?

Is the project developed/maintained? If so, where is the typical discussion channel. I am very interested in the idea. Never knew something like this existed already! Let me know how I can contact the developers? I would love to contribute,

Assigns not working with render_to_text

If I try the following:

render_to_string( :widget => Views::Posts::MyPost, :assigns => { :foo => "bar"})

foo is never getting set within the widget. It also doesn't work if I try this:

render_to_string( :widget => Views::Posts::MyPost, :foo => "bar")

In looking at the Erector code in rails/extensions/action_controller.rb, it looks like the @assigns on line 20 is never getting set:

def render_with_erector_widget(*options, &block)
if options.first.is_a?(Hash) && widget = options.first.delete(:widget)
render_widget widget, @assigns, &block
else
render_without_erector_widget *options, &block
end
end

If I replace the above @assigns with options.first[:assigns] then the first example above works. So, I'm either missing something in my render_to_text usage, or there's an error in the code.

wrong number of arguments (given 4, expected 5) in FormBuilder

wrong number of arguments (given 4, expected 5)
/home/web/.rvm/gems/ruby-2.3.0@squash/bundler/gems/erector-f99aecdad3cd/lib/erector/rails/form_builder.rb:16:in `initialize'
actionview (4.2.5.2) lib/action_view/helpers/form_helper.rb:1210:in `new'
actionview (4.2.5.2) lib/action_view/helpers/form_helper.rb:1210:in `instantiate_builder'
actionview (4.2.5.2) lib/action_view/helpers/form_helper.rb:443:in `form_for'
/home/web/.rvm/gems/ruby-2.3.0@squash/bundler/gems/erector-f99aecdad3cd/lib/erector/rails3.rb:61:in `form_for'

Passing a block to FormBuilder#new was removed in Rails 4.1.

Mixing erector with views

it would be really nice to be able to do something like this from inside views:

class ContentSectionWidget < Erector::Widget
  def content
    div :class => 'content' do
      # magic here
    end
  end

  def header header_text
    tab do
      h1 header_text
    end
  end

  def tab &block
    span :class => 'tab', &block
  end

  def body
    div :class => 'body', &block
  end
end


# Haml

= ContentSectionWidget.new do |w|
  = w.header "Blah blah"
  %p Some haml here
  = w.body do 
    %p Some more haml body here

We are slowly moving from haml to erector, but we don't want to make a new erector class for every single view in the system yet. Being able to do something like the above where we can interoperate with erector from inside haml (or erb) woud be useful.

I've tried to come up with a simple way to do this, but haven't been able to yet. Thoughts?

depends_on not working

Hello,

I'm using

depends_on :css,  '/stylesheets/items/show.css.scss'

But it does not get included in the head portion.

I'm using rails 3.2.1.

Any Idea on what's going on ?

Erector and html5 attributes

Haml has a html5 mode where

= %input{:type => 'text', :required => true}

=>

But erector doesn't seems to:

input :type => 'text', :required => true

=>

Is this feature on the roadmap?

I could help a bit if I find the time

Erector renders rails link_to in wrong order

Here is my code

text "The #{a.class.name.titleize}: #{link_to(a.name , album_path(a))} has "

here's the desired output:

The Studio Album: <a href="/albums/360">Yeezus</a> has 10 songs on it. It was released on

here's the actual output:

<a href="/albums/360">Yeezus</a>The Studio Album:  has 10 songs on it. It was released on

Some how the link_to gets shuffled to the front. :(

rvm info

  ruby:
    interpreter:  "ruby"
    version:      "1.9.3p286"
    date:         "2012-10-12"
    platform:     "i686-linux"
    patchlevel:   "2012-10-12 revision 37165"
    full_version: "ruby 1.9.3p286 (2012-10-12 revision 37165) [i686-linux]"

Rails 3.2.5 warns on disable_with

When I upgrade my app from rails 3.2.2 to rails 3.2.5, I get the following warning:

DEPRECATION WARNING: :disable_with option is deprecated and will be removed from Rails 4.0. Use 'data-disable-with' instead. (called from button_to at /Users/dev/.rvm/gems/ruby-1.9.2-p180@banana/bundler/gems/erector-8e54c16f092c/lib/erector/rails3.rb:51)

This is https://github.com/jkingdon/erector.git, ref 8e54c16 . Don't see anything which changed in trunk since then, although I didn't try any other erector versions yet.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.