Created
July 4, 2010 17:23
-
-
Save ghickman/463598 to your computer and use it in GitHub Desktop.
Jekyll Contact Form with Sinatra
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'sinatra' | |
require 'pony' | |
require 'haml' | |
set :haml, {:format => :html5} | |
set :public, File.dirname(__FILE__) | |
set :views, File.dirname(__FILE__) | |
# Create the page class and give it a title of Contact for the layout | |
class Page | |
def title | |
'Contact' | |
end | |
end | |
def contact | |
# create the variables that the layout will expect | |
page = Page.new | |
content = haml :contact | |
# render the contact page using jekyll's layout and with our mock jekyll vars | |
haml :contact, :layout=>:'_layouts/default', :locals=>{:page=>page, :content=>content} | |
end | |
get '/contact' do | |
@errors={} | |
contact | |
end | |
post '/contact' do | |
@errors={} | |
@errors[:name] = 'No Anon allowed here.' if params[:name].nil? || params[:name].empty? | |
@errors[:email] = 'Sinatra needs an email to send your message from!' if params[:email].nil? || params[:email].empty? | |
@errors[:message] = 'No message?! Sounds like heavy breathing on the phone to me.' if params[:message].nil? || params[:message].empty? | |
if @errors.empty? | |
Pony.mail(:to=>'[email protected]', :from=>"#{params[:email]}", :subject=>"Contact Message", :body=>"#{params[:message]}") | |
redirect 'http://localhost:4000/index.html' | |
else | |
contact | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'source/_contact' | |
set :run, false | |
set :environment, :production | |
FileUtils.mkdir_p 'log' unless File.exists?('log') | |
log = File.new("log/sinatra.log", "a") | |
$stdout.reopen(log) | |
$stderr.reopen(log) | |
run Sinatra::Application |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#contact | |
%form{:method=>"post"} | |
%fieldset | |
.input | |
%label{:for=>"name"} Name | |
%input{:name=>"name", :type=>"text", :id=>"name", :class=>('error' if !@errors[:name].nil?), :required=>{}} | |
- if !@errors[:name].nil? | |
.error | |
%p=@errors[:name] | |
.input | |
%label{:for=>"email"} Email Address | |
%input{:name=>"email", :type=>"email", :id=>"email", :class=>('error' if !@errors[:email].nil?), :required=>{}} | |
- if !@errors[:email].nil? | |
.error | |
%p=@errors[:email] | |
.input | |
%label{:for=>"message"} Message | |
%textarea{:name=>"message", :id=>"message", :rows=>'5', :class=>('error' if !@errors[:message].nil?), :required=>{}} | |
- if !@errors[:message].nil? | |
.error | |
%p=@errors[:message] | |
#submit.input | |
%input{:type=>"submit", :value=>"Send"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
!!! | |
%html | |
%head | |
%title Jekyll's Contact Form | |
%meta{:"http-equiv" => "Content-type", :content => "text/html; charset=utf-8"} | |
%link{:rel =>'shortcut icon', :href=>'/favicon.ico', :type=>'image/x-icon'} | |
%link{:rel=>'stylesheet', :type=>'text/css', :href=>'/stylesheets/master.css', :media=>'screen'} | |
%body | |
%header | |
%h1 Jekyll's Contact From brought to you by Sinatra | |
%nav | |
%ul | |
%li | |
%a{:href=>'/', :title=>'Home'} Blog | |
%li | |
%a{:href=>'http://localhost:4567/contact', :title=>'Contact'} Contact | |
%section#content== #{content} | |
%footer | |
%div | |
%section#jekyll | |
Powered by | |
%a{:href=>"http://github.com/richguk/jekyll", :title=>"Jekyll"} Jekyll | |
%section#from | |
A | |
%a{:href=>"http://ghickman.co.uk", :title=>"GHickman Blog"} GHickman | |
project |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
config.ru | |
site/ | |
source/ | |
_contact.rb | |
_layouts/ | |
default.haml | |
_posts/ | |
contact.haml | |
images/ | |
index.haml | |
javascript/ | |
stylesheets/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment