Skip to content

Commit

Permalink
Add example for JstPages.
Browse files Browse the repository at this point in the history
  • Loading branch information
rstacruz committed Sep 11, 2011
1 parent e680245 commit 969dc92
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 3 deletions.
8 changes: 6 additions & 2 deletions README.md
Expand Up @@ -21,10 +21,14 @@ Contents
__Sinatra-backbone__ is comprised of two Sinatra plugins:

* `Sinatra::JstPages` – Provides support for JavaScript server templates (JST)
for use in Backbone views.
for use in Backbone views. See [JstPages example][jstx] for a full example
application.

* `Sinatra::RestAPI` – Provides restful API for your models for use in Backbone
models.
models. See [RestAPI example][restx] for a full example application.

[jstx]: https://github.com/rstacruz/sinatra-backbone/examples/jstpages
[restx]: https://github.com/rstacruz/sinatra-backbone/examples/restapi

For usage and API reference, please see http://ricostacruz.com/sinatra-backbone. [](#api_reference)

Expand Down
21 changes: 21 additions & 0 deletions examples/jstpages/app.rb
@@ -0,0 +1,21 @@
$:.unshift File.expand_path('../../../lib', __FILE__)

require 'sinatra/base'
require 'sequel'
require 'sinatra/backbone'

class App < Sinatra::Base
enable :raise_errors, :logging
enable :show_exceptions if development?

register Sinatra::JstPages
serve_jst '/jst.js'

set :root, File.expand_path('../', __FILE__)
set :views, File.expand_path('../views', __FILE__)
set :public, File.expand_path('../public', __FILE__)

get '/' do
erb :home
end
end
3 changes: 3 additions & 0 deletions examples/jstpages/config.ru
@@ -0,0 +1,3 @@
require './app'
App.set :run, false
run App
20 changes: 20 additions & 0 deletions examples/jstpages/public/app.js
@@ -0,0 +1,20 @@
// Here is our Backbone model!
$(function() {
do_test();
});

function do_test() {
echo("<h3>Rendering from template:</h3>");
echo(JST['hello']({name: "Julie Kitzinger", age: "33"}));
echo("<h3>Success!</h3>");
}

// Helper functions
function echo(html) {
$("#messages").append(html);
};

function onerror() {
echo("<p class='error'>Oops... an error occured.</p>");
};

6 changes: 6 additions & 0 deletions examples/jstpages/views/hello.jst.tpl
@@ -0,0 +1,6 @@
<p>Hello, this is me:</p>

<dl>
<dt>Name:</dt><dd><%= name %></dd>
<dt>Age:</dt><dd><%= age %></dd>
</dl>
25 changes: 25 additions & 0 deletions examples/jstpages/views/home.erb
@@ -0,0 +1,25 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title></title>
<script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/1.6.2/jquery.min.js'></script>
<script src='http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.1.7/underscore-min.js'></script>
<script src='http://cdnjs.cloudflare.com/ajax/libs/backbone.js/0.5.3/backbone-min.js'></script>
<style>
body { font-family: sans-serif; font-size: 13px; line-height: 1.5; background: #d0d0da; }
#messages { width: 400px; margin: 20px auto; background: white; padding: 20px; border: solid 10px #c0c0ca; }
h3 { border-top: dotted 1px #ccc; padding: 20px 20px 0 20px; margin: 20px -20px 0 -20px; color: #46a; }
h3:first-child { border-top: 0; margin-top: 0; padding-top: 0; }
dl { overflow: hidden; }
dt { float: left; width: 120px; margin-right: 10px; text-align: right; color: #aaa; }
h3 { font-family: palatino; font-size: 1.3em; }
</style>
</head>
<body>
<div id="messages">
</div>
<script src='jst.js'></script>
<script src='app.js'></script>
</body>
</html>
2 changes: 1 addition & 1 deletion examples/restapi/app.rb
@@ -1,4 +1,4 @@
$:.unshift File.expand_path('../../lib', __FILE__)
$:.unshift File.expand_path('../../../lib', __FILE__)

require 'sinatra/base'
require 'sequel'
Expand Down
2 changes: 2 additions & 0 deletions lib/sinatra/jstpages.rb
@@ -1,5 +1,6 @@
# ## Sinatra::JstPages [module]
# A Sinatra plugin that adds support for JST (JavaScript Server Templates).
# See [JstPages example][jstx] for a full example application.
#
# #### Basic usage
# Register the `Sinatra::JstPages` plugin in your application, and use
Expand Down Expand Up @@ -67,6 +68,7 @@
# [haml]: https://github.com/creationix/haml-js
# [eco]: https://github.com/sstephenson/eco
# [cs]: http://coffeescript.org
# [jstx]: https://github.com/rstacruz/sinatra-backbone/examples/jstpages
#
module Sinatra
module JstPages
Expand Down

0 comments on commit 969dc92

Please sign in to comment.