Project Name | Stars | Downloads | Repos Using This | Packages Using This | Most Recent Commit | Total Releases | Latest Release | Open Issues | License | Language |
---|---|---|---|---|---|---|---|---|---|---|
Hamlit | 976 | 1,804 | 14 | 4 months ago | 117 | February 03, 2022 | other | Ruby | ||
High Performance Haml Implementation | ||||||||||
Sinatra Partial | 105 | 754 | 21 | 6 years ago | 11 | July 28, 2017 | 3 | other | Ruby | |
Just the partials helper in a gem. That is all. | ||||||||||
Sinatra Prawn | 46 | 2 | 12 years ago | 3 | August 10, 2014 | 5 | mit | Ruby | ||
Sinatra extension to add support for pdf rendering with Prawn templates. | ||||||||||
React Sinatra | 37 | 5 years ago | 2 | January 19, 2017 | 1 | mit | Ruby | |||
React on Sinatra Integration, Server Side Rendering | ||||||||||
Tilt Jbuilder | 22 | 462 | 6 | 6 years ago | 12 | January 30, 2016 | 3 | mit | Ruby | |
Support for rendering Jbuilder templates in Tilt | ||||||||||
Sinatra Markaby | 9 | 14 years ago | 3 | August 10, 2014 | 1 | mit | Ruby | |||
Sinatra plugin to enable markaby (.mab) template rendering | ||||||||||
Cells Sinatra | 9 | 13 years ago | Ruby | |||||||
View Components for Sinatra. | ||||||||||
Ruby Oc | 5 | 3 | 1 | 7 years ago | 5 | November 10, 2015 | 2 | mit | Ruby | |
OpenComponents for Ruby | ||||||||||
Tumblr Dashboard Rss Sinatra App | 5 | 12 years ago | Ruby | |||||||
Sinatra application for rendering tumblr dashboard rss | ||||||||||
Chuck Renderer | 4 | 6 years ago | 2 | gpl-3.0 | CSS | |||||
Docker image for ChucK rendering as a service |
Partials for Sinatra!
If you do decide to use this gem, please let me know if it isn't working for you - make a contribution! Github makes it so simple..! See the Contribution section for more.
Back to our previously scheduled programming...
You may say "why is this needed?".
Go on then.
huff "Why is this needed?"
Because I was forever copying the code I took from http://www.sinatrarb.com/faq.html#partials into each and every project. It may well be that this is included in some other gem full of useful helpers, but I haven't found it yet, and besides this is what I really want. The whole point of Sinatra is not to get a lot of stuff you didn't really need anyway.
So here it is, partials, and that's it.
gem install sinatra-partial
At the top of your app.rb:
require 'sinatra/partial'
For a classic app, that's all you need to do. For a modular app you should register it too:
class Blah < Sinatra::Base
register Sinatra::Partial
The default templating engine is haml. If you wish to use something else, you can set in the config options:
set :partial_template_engine, :erb
If you wish, you can also pass this in with the options hash to partial (if you do, it will override the above setting for that call):
partial(:"meta/news", :template_engine => :erb)
If you like the Rails convention of adding an underscore to the beginning of a partial, set it here:
enable :partial_underscores
Otherwise, the default is for no underscore (if you like Rails you know where to get it;)
Note:
If you're using Slim then there are examples in the examples directory, but the output is slightly different for some reason and the project maintainers don't wish to be helpful. I don't use Slim, if you do then feel free to contribute and find out how to get the specs to pass, but I won't be pursuing this.
The docs are good to look at (big thanks to Sam Elliot for improving them a lot), just follow the docs link from this page if you can't find them:
https://rubygems.org/gems/sinatra-partial
or use yard/rdoc to generate them.
get "/" do
output = ""
output << partial( :top )
output << partial( :middle )
output << partial( :bottom )
output
end
-# top.haml
%h2
The is the top
-# middle.haml
%p
Can you guess what I am yet?
-# bottom.haml
%p
Is it worse to be at the bottom or the foot?
get "/" do
output = ""
@title = "My contrived example"
username = current_user.username
output << partial( :left_col )
output << partial( :middle, :locals => { username: username} )
output << partial( :right )
output
end
-# middle.haml
%p
Wow, here is that #{username} you just passed me!
:-o
Remember that since this is a helper method it can be called inside routes and views - use it where you need it!
-# welcome_message.haml
%h2
Welcome back #{username}
-# content.haml
Blah Blah Blah
-# footer.haml
You've reached the bottom of the page!
-# layout.haml
%html
%head
%body
#header
= partial :welcome_message, locals: {username: "Iain" }
#main
= partial :content
= yield
#footer
= partial :footer
Here's how to use a collection, in this case to render a menu:
# app.rb
before do
@menu = [
["home", uri("/")],
["login", uri("/login")],
["contact_us", uri("/contact-us")],
["about", uri("/about")],
]
end
-# menu.haml
#nav
%ul
= partial :menu_item, collection: menu
-# menu_item.haml
- display_name, url = menu_item
- atts ||= {}
-# set the class of the list item for the current page to 'active'
- (atts[:class] = atts[:class].nil? ? "active" : "#{atts[:class]} active") if request.url == url
%li{ atts }
%a{ class: "nav", href: menu_item.last }
= menu_item.first
-# layout.haml
%html
%head
%title= @title
%body
= yield
= partial :menu, locals: { menu: @menu }
You'll get a menu built for you.
Look in the examples directory for some very simple examples.
Thanks to Chris Schneider and Sam Elliott for sharing their code, and for sharing further updates.
Most of all, remember that any contribution you can make will be valuable, whether that is putting in a ticket for a feature request (or a bug, but they don't happen here;), cleaning up some grammar, writing some documentation (or even a blog post, let me know!) or a full blooded piece of code - it's all welcome and encouraged.
To contribute some code:
git clone [email protected]:YOUR_USERNAME/Sinatra-Partial.git
git remote add upstream git://github.com/yb66/Sinatra-Partial.git
git fetch upstream
git checkout develop
git checkout -b kitchen_sink
gem install bundler -r --no-ri --no-rdoc
bundle install --binstubs --path vendor
bundle install
, it'll remember the rest.reek PATH_TO_FILE_WITH_YOUR_CHANGES
and see if it gives you any good advice. You don't have to do what it says, just consider it.bin/rake docs
to generate documentation.