Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
C

clearwater

> 编程语言
Open source

Component-based Ruby front-end framework

596 stars0 likes1 views
WebsiteGitHub

About

Component-based Ruby front-end framework

clearwater

Clearwater is a rich front-end framework for building fast, reasonable, and easily composable browser applications in Ruby. It renders to a virtual DOM and applies the virtual DOM to the browser's actual DOM to update only what has changed on the page.

Installing

Add these lines to your application's Gemfile:

ruby
gem 'clearwater'
gem 'opal-rails' # Only if you're using Rails

Using

This is a minimum-viable Clearwater app:

ruby
require 'opal'
require 'clearwater'

class HelloWorld
  include Clearwater::Component
  
  def render
    h1('Hello, world!')
  end
end

app = Clearwater::Application.new(component: HelloWorld.new)
app.call

Clearwater has three distinct parts:

  1. The component: the presenter and template engine
  2. The router (optional): the dispatcher and control
  3. The application: the "Go" button

The Component

ruby
class Blog
  # All components need a set of behavior, but don't worry it's not a massive list.
  include Clearwater::Component

  # This method needs to return a virtual-DOM element using the element DSL.
  # The DSL is provided by the Clearwater::Component mixin.
  def render
    div([
      Articles.new,
      Biography.new,
    ])
  end
end

While we use two components in this example, you can use all of these as well:

ruby
# 

def render
  div({ id: 'foo' }, [
    h1('Heading'),
    article('hello!'),
  ])
end

# 

def render
  div('Hello, world!')
end

# 

def render
  div(123)
end

# 

def render
  div
end

The Router

ruby
router = Clearwater::Router.new do
  # A route with a block contains subordinate routes
  route 'blog' => Blog.new do # /blog
    route 'new_article' => NewArticle.new # /blog/new_article

    # This path contains a dynamic segment. Inside this component, you can use
    # router.params[:article_id] to return the value for this segment of the
    # URL. So for "/articles/123", router.params[:article_id] would be "123".
    route ':article_id' => ArticleReader.new # /blog/123
  end
end

Using with Rails

You can also use Clearwater as part of the Rails asset pipeline. First create your Clearwater application (replace app/assets/javascripts/application.js with this file):

app/assets/javascripts/application.rb

ruby
require 'opal' # Not necessary if you load Opal from a CDN
require 'clearwater'

class Layout
  include Clearwater::Component

  def render
    h1('Hello, world!')
  end
end

app = Clearwater::Application.new(component: Layout.new)
app.call

app/views/layouts/application.html.erb

erb

  

  
    
    
  

Then you need to get Rails to render a blank page, so add these two routes:

config/routes.rb

ruby
root 'home#index'
get '*all' => 'home#index'

You can omit the second line if your Clearwater app doesn't use routing. It just tells Rails to let your Clearwater app handle all routes.

app/controllers/home_controller.rb

ruby
class HomeController < ApplicationController
  def index
  end
end

app/views/home/index.html.erb

xml

You can use the Rails generators to generate the controller and view (rails g controller home index), but it won't set up the root and catch-all routes, so you'll still need to do that manually.

Once you've added those files, refresh the page. You should see "Hello, world!" in big, bold letters. Congrats! You've built your first Clearwater app on Rails!

Using with Roda

If you're using Roda, you'll want to use the roda-opal_assets gem to get an asset-pipeline-style workflow for compiling your Clearwater app into JavaScript.

Getting Started

Use the clearwater-roda gem to generate a starter Clearwater app that demonstrates many components working together, routing, and even state management (via grand_central).

Experiment!

You can experiment with Clearwater using the Clearwater Playground. You can also explore other saved playground experiments.

Contributing

This project is governed by a Code of Conduct

  1. Fork it
  2. Branch it
  3. Hack it
  4. Save it
  5. Commit it
  6. Push it
  7. Pull-request it

License

Copyright (c) 2014-2018 Jamie Gaskins

MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Rubyclearwaterfront-endruby

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言