something = "0"
get "/" do
erb :index, :locals => something
end
When I do this and go to localhost, it says undefined method `keys' for "0":String. I am using sinatra. How do I pass a variable from server to index?
You're half way there, you just need to make something an object.
get "/" do
erb :index, :locals => {:something => 0}
end
and then just use it in the your index:
Something: <%= something %>
Related
I'd like to make some modifications on variable in erb templates:
e.g. decode base64 and split string
It tried
<%= Base64.decode64(#my_variable).rpartition(':').last %>
and
<%= Base64.decode64(<%= #my_variable %>).rpartition(':').last %>
and via scope
<%= Base64.decode64(scope['my_class::my_variable']).rpartition(':').last %>
but it is nil.
Filepath: /usr/lib/ruby/1.9.1/base64.rb
Line: 58
Detail: undefined method `unpack' for nil:NilClass
puppet version 3.8.2
The first example that you tried is the correct ERB notation.
That error would be occurring because you passed the nil object to Base64.decode64():
[1] pry(main)> require 'base64'
=> true
[2] pry(main)> Base64.decode64(nil).rpartition(':').last
NoMethodError: undefined method `unpack' for nil:NilClass
from /Users/alexharvey/.rvm/rubies/ruby-2.1.4/lib/ruby/2.1.0/base64.rb:58:in `decode64'
(Admittedly, I don't have your Ruby 1.9.1 and I wasn't able to easily install it, but it's unlikely this changed.)
That means that #my_variable is set to nil, and that would happen if you failed to actually set the Puppet variable $my_variable in your manifest.
So you would need to call the template with a block something like this:
$my_variable = 'foo'
file { '/tmp/foo':
ensure => file,
content => template('test/mytemplate.erb')
}
I'm using Padrino, and I want to take parameters out of URL and use them in an .erb template.
In my app setup I have:
get '/testpage/:id' do
userID = params[:id]
render 'test/index'
end
In my test/ folder I have index.html.erb which is successfully rendered, for a url like http://localhost:9000/testpage/hello123.
However, I've tried printing the params[:userID] on the page with:
<%= #userID %>
The rest of the page renders fine but hello123 isn't anywhere to be found. When I try <%= userID %> I get undefined local variable or method `userID' for #<stuff>
What am I missing here?
Just a guess, because I've never used Padrino, but if it works like Rails this may help you:
get '/testpage/:id' do
#userID = params[:id]
render 'test/index'
end
In sinatra, it's just like this (see "Views/Templates" section):
get '/testpage/:id' do |id|
erb :index, :locals => {:id => id}
end
The template is located in views/index.erb by default. It could be change.
I'd like to be able to create template functions in Sinatra HAML templates that themselves contain haml. Is there any way to do this or something similar? It'd be cool if it could work with markdown too.
foo.haml
def foo(x)
%h2 something
%p something about #{x}
%h1 Herp de derp
= foo("mary")
= foo("us")
Cheers!
Actually, you can do something like this:
# app.rb
require 'sinatra'
require 'haml'
helpers do
def foo(name)
haml = <<-HAML
#hello_block
Hello, #{name}
HAML
engine = Haml::Engine.new(haml)
engine.render
end
end
get '/' do
haml :index
end
# index.haml
= foo 'World'
Function is close, what you really need is what's known as a partial. These are predefined templates that you can place inside other views. For instance, you may have a comment partial to display a comment's author, timestamp, content, etc. You can then render this partial for each of the comments on a particular post.
Essentially, you'll end up with the following
# _foo.haml.erb
%h2 somthing
%p= x
# index.haml.erb
%h1 Herp de derp
= render :partial => "foo", :locals => { :x => "mary" }
= render :partial => "foo", :locals => { :x => "us" }
I'm trying to pass parameters to an erb view using Ruby and Sinatra.
For example, I can do:
get '/hello/:name' do
"Hello #{params[:name]}!"
end
How do I pass :name to the view?
get '/hello/:name' do
erb :hello
end
And how do I read the parameters inside view/hello.erb?
Thanks!
just pass the :locals to the erb() in your routes:
get '/hello/:name' do
erb :hello, :locals => {:name => params[:name]}
end
and then just use it in the views/hello.erb:
Hello <%= name %>
(tested on sinatra 1.2.6)
Not sure if this is the best way, but it worked:
get '/hello/:name' do
#name = params[:name]
erb :hello
end
Then, I can access :name in hello.erb using the variable #name
get '/hello/:name' do
"Hello #{params[:name]}!"
end
You cannot do this in routes.
You want to set the params in the controller.
app/controllers/some_controller.rb
def index
params[:name] = "Codeglot"
params[:name] = "iPhone"
params[:name] = "Mac Book"
end
app/views/index.html.erb
<%= params[:name] %>
<%= params[:phone] %>
<%= params[:computer] %>
I have come across a issue when writing some sinatra code I have the follow block of code
begin
# do stuff here
rescue SomeException::Class => ex
flash.now[:err] = "some error " + ex.message
ensure
erb :content, :layout => :mainlayout
end
The problem I have is the erb output is only partially rendered, mainlayout.erb is rendered, however, content.erb doesn't get included. I have used the same erb line in other parts of the application and they work fine.
The following actually works and is a work around that I am currently using
begin
# do stuff here
erb :content, :layout => :mainlayout
rescue SomeException::Class => ex
flash.now[:err] = "some error " + ex.message
erb :content, :layout => :mainlayout
end
Any ideas to why this isn't completing when under ensure? I would like to use it as its more elegant.
try to use return erb :content, :layout => :mainlayout read more about ensure here - http://blog.leshill.org/blog/2009/11/17/ensure-with-explicit-return.html