• Stars
    star
    106
  • Rank 325,871 (Top 7 %)
  • Language
    Ruby
  • License
    Other
  • Created over 13 years ago
  • Updated over 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Just the partials helper in a gem. That is all.

Sinatra Partial

Partials for Sinatra!

Build status

Master branch: Build Status

Develop branch: Build Status for development branch

Quick note

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...

Por qué?

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.

Installation

gem install sinatra-partial

Getting started

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

Configuration options

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.

Some examples

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.

Inside a route

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?

Local variables

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

Here's one using views

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        

Collections

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.

Examples

Look in the examples directory for some very simple examples.

Thanks

Thanks to Chris Schneider and Sam Elliott for sharing their code, and for sharing further updates.

Contributing

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:

  1. Fork this.
  • 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
  • Decide on the feature you wish to add.
    • Give it a snazzy name, such as kitchen_sink.
    • git checkout -b kitchen_sink
  • Install Bundler.
    • gem install bundler -r --no-ri --no-rdoc
  • Install gems from Gemfile.
    • bundle install --binstubs --path vendor
    • Any further updates needed, just run bundle install, it'll remember the rest.
  • Write some specs.
  • Write some code. (Yes, I believe that is the correct order, and you'll never find me doing any different;)
  • Write some documentation using Yard comments - see http://rubydoc.info/docs/yard/file/docs/GettingStarted.md
    • Use real English (i.e. full stops and commas, no l33t or LOLZ). I'll accept American English even though it's ugly. Don't be surprised if I 'correct' it.
    • Code without comments won't get in, I don't have the time to work out what you've done if you're not prepared to spend some time telling me (and everyone else).
  • Run 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.
  • Run specs to make sure you've not broken anything. If it doesn't pass all the specs it doesn't get in.
    • Have a look at coverage/index.htm and see if all your code was checked. We're trying for 100% code coverage.
    • If your commit changes the way things work or adds a feature then add an example in the examples dir. The specs run off the examples to make sure the library works with a real project while keeping the examples up to date, so make sure you add something there if needs be.
  • Run bin/rake docs to generate documentation.
    • Open up docs/index.html and check your documentation has been added and is clear.
  • Add a short summary of your changes to the top of CHANGES file. Add your name and a link to your bio/website if you like too. Don't add a version number, I'll handle that.
  • Send me a pull request.
  • Wait for worldwide fame.
  • Shrug and get on with you life when it doesn't arrive, but know you helped quite a few people in their life, even in a small way - 1000 raindrops will fill a bucket!

More Repositories

1

tickle

Natural language parser for recurring events
Ruby
77
star
2

path_helper

Simple replacement for Apple's /usr/libexec/path_helper
Ruby
20
star
3

password-advice

Don't get cracked!
9
star
4

TextTube

Pass a string through filters to transform it.
Ruby
7
star
5

BBEdit-stuff

The scripts and text filters I use with BBEdit, my favourite editor
Ruby
5
star
6

bbedit-hcl

HCL Codeless Language Module for BBEdit
4
star
7

BBEdit-Dockerfile

A bit of syntax highlighting and commenting ability for Dockerfiles in BBEdit.
3
star
8

Campo

Form builder for haml
Ruby
3
star
9

Rack-GeoIPCity

rack middleware for using the maxmind geoip city databases
Ruby
3
star
10

RandomPerson

a port to Ruby of Perl's Data::RandomPerson
Ruby
3
star
11

sinatra-default_parameters

Extends the request context with a helper to give a quick and simple way to set defaults for the params helper on a per route basis.
Ruby
2
star
12

rack-jquery

jQuery CDN script tags and fallback in one neat package.
Ruby
2
star
13

Nanoc-stuff

useful bits of code for my nanoc sites
Ruby
2
star
14

TextWrangler-text-filters

The text filters I use.
Ruby
1
star
15

Captchachacha

Rack middleware for the captchator.com service
Ruby
1
star
16

NewAge

Handling year and month inputs sanely.
Ruby
1
star
17

Project_Euler

my answers for the project euler puzzles
Ruby
1
star
18

BBEdit-Awk

BBEdit codeless language module for Awk scripts
1
star
19

GeoIPalise

A sinatra server app for checking the geo info for ip's in webserver log files
Ruby
1
star
20

BBEdit-Elm-clm

A BBEdit codeless language module for Elm
1
star
21

rack-test-accepts

Helpers for rack-test for the HTTP_ACCEPT header.
Ruby
1
star
22

sinatra-extension-skeleton

Quick way to get an extension going.
Ruby
1
star
23

Mint-Docker-Compose

A (hopefully) easy way to run Mint via Docker
Dockerfile
1
star
24

Stackoverflow-question-9337273

Mark a form submission as ajax by adding parameters to POST after submit
Ruby
1
star