Giter Club home page Giter Club logo

sinatra-content-for2's Introduction

ContentFor2

Small extension for the Sinatra web framework that allows you to use the following helpers in your views:

<% content_for :some_key do %>
  <chunk of="html">...</chunk>
<% end %>

<% yield_content :some_key %>

This allows you to capture blocks inside views to be rendered later in this request. For example, to populate different parts of your layout from your view.

When using this with the Haml rendering engine, you should do the following:

- content_for :some_key do
  %chunk{ :of => "html" } ...

= yield_content :some_key

Note that with ERB yield_content is called without an ‘=’ block (<%= %>), but with Haml it uses = yield_content.

Using an ‘=’ block in ERB will output the content twice for each block, so if you have problems with that, make sure to check for this.

Note Use <%= yield_content ... %> in Erubis.

Also we provide content_for? method that checks that block is defined.

Usage

If you’re writing “classic” style apps, then requring sinatra/content_for2 should be enough. If you’re writing “classy” apps, then you also need to call helpers Sinatra::ContentFor2 in your app definition.

And how is this useful?

For example, some of your views might need a few javascript tags and stylesheets, but you don’t want to force this files in all your pages. Then you can put <% yield_content :scripts_and_styles %> on your layout, inside the <head> tag, and each view can call content_for setting the appropriate set of tags that should be added to the layout.

Credits

Code by foca, inspired on the Ruby on Rails helpers with the same name. Haml support by mattly. Erubis support by hiroyuki. Bundler/etc. unicode compatibility by arnabc and akzhan. content_for? method by BrunoGrasselli.

sinatra-content-for2's People

Contributors

akzhan avatar arnabc avatar brunograsselli avatar foca avatar mattly avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

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

Forkers

akemrir

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.