Sorry, I will not use the specific expression in English.
index.erb
<h1>Hello World.</h1>
<ul>
<li>item1</li>
<li>item2</li>
</ul>
<% capture_content :key do %>
I'm Here.
<% end %>
helpers
def capture_content(key, &block)
#content_hash = {}
#content_hash[key] = block.call # this block contains erb all
end
I just want capture_content in content
I hope expression is correct T_T
If you are looking to write yourself the Sinatra equivalent of the Reails content_for helper then you don't need to.
Because there is an extension called Sinatra::ContentFor which is part of the Sinatra::Contrib project which does what you want.
From the documentation:
Sinatra::ContentFor is a set of helpers that allows you to capture
blocks inside views to be rendered later during the request. The most
common use is to populate different parts of your layout from your
view.
Related
I don't have much experience with Ruby all I wan't to do is render a value that I declare in an .rb file in an .erb file.
In my .rb file I have this:
def abc()
begin
"aaaaa"
end
end
In my .erb file I have this:
Hello <% abc %>
When I run the app I only see:
Hello
But I expect to see:
Hello aaaa
Anybody can give me a hand, I don't really know ruby at all. Also I have no idea if this is ruby or ruby on rails so sorry if the tag below is wrong.
In Sinatra, register your method as a helper in .rb file:
helpers do
def abc
"aaaaa"
end
end
Omit parentheses if your methods don't need arguments. Also, begin/end block isn't necessary here.
You can call your helper in .erb template:
<%= abc %>
Don't forget = in the opening tag.
http://sinatrarb.com/intro.html section 'Helpers'.
It's unclear what you want to achieve. But If you just want some text in your erb you can do something like this:
erb :myerb, locals: {text: "aaaaa", saved: false}
myerb.erb
<% if saved %>
Hello <%= text %>
<% endif %>
This would also work for functions.
First of all, you need to be aware that a defined method inherently includes the functionality of a begin/end block so you don´t need to put them again. Assuming you are using sinatra, here is what I think you need:
my.rb
require 'sinatra'
def abc
"aaaa"
end
get '/' do
erb :my, locals: {variable: abc}
end
my.erb
<html>
<body>
<p>
Hello <%= variable %>
</p>
</body>
</html>
Run ruby my.rb and then open http://localhost:4567/
I'm working on a Sinatra app and want to write my own form helpers. In my erb file I want to use the rails 2.3 style syntax and pass a block to a form_helper method:
<% form_helper 'action' do |f| %>
<%= f.label 'name' %>
<%= f.field 'name' %>
<%= f.button 'name' %>
<% end %>
Then in my simplified form helper I can create a FormBuilder class and yield the methods to the erb block like so:
module ViewHelpers
class FormBuilder
def label(name)
name
end
def field(name)
name
end
def button(name)
name
end
end
def form_helper(action)
form = FormBuilder.new
yield(form)
end
end
What I don't understand is how to output the surrounding <form></form> tags. Is there a way to append text on only the first and last <%= f.___ %> tags?
Rails has had to use some tricks in order to get block helpers to work as wanted, and they changed moving from Rails 2 to Rails 3 (see the blogposts Simplifying Rails Block Helpers and Block Helpers in Rails 3 for more info).
The form_for helper in Rails 2.3 works by directly writing to the output buffer from the method, using the Rails concat method. In order to do something similar in Sinatra, you’ll need to find a way of writing to the output from your helper in the same way.
Erb works by creating Ruby code that builds up the output in a variable. It also allows you to set the name of this variable, by default it is _erbout (or _buf in Erubis). If you change this to be an instance variable rather than a local variable (i.e. provide a variable name that starts with #) you can access it from helpers. (Rails uses the name #output_buffer).
Sinatra uses Tilt for rendering templates, and Tilt provides an :outvar option for setting the variable name in Erb or Erubis templates.
Here’s an example of how this would work:
# set the name of the output variable
set :erb, :outvar => '#output_buffer'
helpers do
def form_helper
# use the new name to write directly to the output buffer
#output_buffer << "<form>\n"
# yield to the block (this is a simplified example, you'll want
# to yield your FormBuilder object here)
yield
# after the block has returned, write any closing text
#output_buffer << "</form>\n"
end
end
With this (fairly simple) example, an Erb template like this:
<% form_helper do %>
... call other methods here
<% end %>
results in the generated HTML:
<form>
... call other methods here
</form>
Sorry this is a very basic question so it should be easy to answer!
Using ruby and sinatra, I am trying to connect, via the api, to get details of my calls. The prescribed way to do this by twilio seems to be:
#client = Twilio::REST::Client.new account_sid, auth_token
# Loop over calls and print out a property for each one
#client.account.calls.list.each do |call|
puts call.sid
puts call.from
puts call.to
which works fine and "puts" the data in the terminal. I want to print the results on an HTML page, so I changed the line
#client.account.calls.list.each do |call|
to
#calls = #client.account.calls.list
and removed the last 3 lines of the code block above, ie. all the "puts"
then, attempting to print on my index page I included the following:
<% #calls.each do |call| %>
<h4 style="color: #ff0000;"><%= params['msg'] %></h4>
<ul>
<li> <%= call.from %> </li>
<li> <%= call.to %> </li>
</ul>
<% end %>
The error message says:
undefined method `each' for nil:NilClass
so I am not connecting to twilio it seems even though the code is almost exactly the same as that above which does connect and produce the required results.
Any ideas? All help gratefully received.
In Sinatra, do not use instance variables to store connection objects and similar stuff. Instead of #call, use the set method that enables a user to set such objects to different variables.
The calls.list method, as per the code, is defined in the Twilio::REST::ListResource module. This returns an array and so, the second part of your code (in the index.erb) is correct.
The problem is, when you start using instance variables for storing the connection object, it gets reset in the route and that's what's happening inside the get do .. end block.
Change the code to:
set :client, Twilio::REST::Client.new(account_sid, account_pass)
# Now, this setting can be accessed by settings.client
get '/calls' do
#calls = settings.client.account.calls
erb :index
end
# index.erb
<% #calls.each do |call| %>
...
<% end %>
This should work.
What would the erb template look like for a ruby enumerator? The answer will be a erb template.
require "erb"
# build data class
class Foo < Array
def build
b = binding
# create and run templates, filling member data variables
ERB.new(File.read('test2.erb')).result b
end
end
# setup template data
bar = Foo.new([1,2,3])
puts bar.build
I would like some way of accessing the 1,2,3 items in the erb template.
Focus on Ruby 1.9.3 compatibility.
Note: the Class is an extension of Array, and I want to access the elements of this array in its erb template.
Ok, it was as simple as reaching into the self reference.
<% self.each{|element| %> <%= element %> <% } %>
I'm new to Sinatra and I'm trying to figure out how querying a collection in templates work. In this particular example I'm trying to find out if in a specific collection (c in this example) of objects if there is an object with a certain value.
<% if c.votes #then filter by an id for example through all of the objects... %>
yes, it exists
<% else %>
nope, doesn't exist
<% end %>
Also, I'm used to django's filters, is there a comparable documentation online that outlines the various query functions for Sinatra?
Is it just a standard collection? You could use any?, which returns true if the provided block ever finds a match. You would then test each object for the value you are looking for in that block.
<% if c.votes.any? { |a| a.id == whatever } %>
...
<% else %>
...
<% end %>
It really depends on what "votes" is.
In rails you would use <% if c.votes.present? %> which is helpful because otherwise if c.votes is an empty array the condition would evaluate to true.
In Sinatra you don't have .present?, but you have a couple options: <% unless c.vote.empty? %> or <% if !c.votes.empty %>. I don't like the readability of either option, so I would recreate add the present? method to Array:
class Array
def present?
!empty?
end
end
Where you add this depends on how you have your Sinatra app setup. One option would ti added it directly to your main app file.