sinatra-erbjoiner 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/sinatra-erbjoiner.rb +79 -0
- metadata +60 -0
@@ -0,0 +1,79 @@
|
|
1
|
+
require 'sinatra'
|
2
|
+
|
3
|
+
# Gets the content of a file from its filepath.
|
4
|
+
def filecontent filepath
|
5
|
+
content=''
|
6
|
+
File.open(filepath).each_line {|line| content<<line }
|
7
|
+
content
|
8
|
+
end
|
9
|
+
|
10
|
+
# Gets the content of an erb file from its name (by string or symbol) in the views folder.
|
11
|
+
def erbcontent name
|
12
|
+
filecontent "views/#{name.to_s}.erb"
|
13
|
+
end
|
14
|
+
|
15
|
+
# Creates an HTML document from a head and a body; simplifies html doc creation.
|
16
|
+
def toHTML head='', body=''
|
17
|
+
"<HTML>\n<HEAD>\n\t#{head}\n</HEAD>\n<BODY>\n\t#{body}\n</BODY>\n</HTML>"
|
18
|
+
end
|
19
|
+
|
20
|
+
# Allows you to erb an html document created from a head and a body, erb-content allowed.
|
21
|
+
# Automatically erbs.
|
22
|
+
def erbHTML head='', body=''
|
23
|
+
erb toHTML head, body
|
24
|
+
end
|
25
|
+
|
26
|
+
# Joins *views into one erb and returns the content of that.
|
27
|
+
# Can be used in documents to simplify them.
|
28
|
+
#
|
29
|
+
# This is extremely handy, for example, in HTML:
|
30
|
+
# <p>Content</p>
|
31
|
+
# <!-- Insert Login Form here from login.erb if not logged in -->
|
32
|
+
# <%= joinERBs :login unless @user %>
|
33
|
+
# <!-- Code to insert to all -->
|
34
|
+
# <%= joinERBs :header, :content_area, :footer %>
|
35
|
+
#
|
36
|
+
# This allows for increased flexibility and less code maintainence, allowing you to
|
37
|
+
# create a re-usable live template file to use wherever you want without a lot
|
38
|
+
# of copy-pasting -- you don't need to because it's done automatically.
|
39
|
+
def joinERBs *views
|
40
|
+
joint=''
|
41
|
+
views.each {|v| joint += erbcontent v }
|
42
|
+
joint
|
43
|
+
end
|
44
|
+
|
45
|
+
# Joins *views with joinERBs and formats them with head into an html document.
|
46
|
+
# Then, displays the generated document.
|
47
|
+
# @param head - The head of the document; can call htitle("TITLE") to generate a title tag
|
48
|
+
# @param *views - The symbols or strings representing the erb views
|
49
|
+
def erbjoin head='', *views
|
50
|
+
erbHTML head, joinERBs(*views)
|
51
|
+
end
|
52
|
+
|
53
|
+
# Represents a title tag in html
|
54
|
+
# @param title - The title in the content of the tag.
|
55
|
+
def htitle title
|
56
|
+
htag :title, title
|
57
|
+
end
|
58
|
+
|
59
|
+
# Represents an html tag.\
|
60
|
+
# @param tag - The type of tag; String or Symbol.
|
61
|
+
# @param content= - The content in the tag. <TAG_TYPE>content</TAG_TYPE>
|
62
|
+
# @param *params - The parameters in the tag. <TAG_TYPE *params></TAG_TYPE>
|
63
|
+
def htag tag, content='', *params
|
64
|
+
"<#{tag.to_s} #{compact(params)}>\n\t#{content}</#{tag.to_s}>"
|
65
|
+
end
|
66
|
+
|
67
|
+
#Compacts an array into one string (non-recursively; only 1 dimensional arrays)
|
68
|
+
#each element seperated by a single space
|
69
|
+
def compact arr
|
70
|
+
str = ''
|
71
|
+
arr.each {|e| str+=e.to_s<<' '}
|
72
|
+
str
|
73
|
+
end
|
74
|
+
|
75
|
+
=begin
|
76
|
+
def fixindentation jointerb
|
77
|
+
jointerb.to_s.index /<[(html)(HTML)]>/
|
78
|
+
end
|
79
|
+
=end
|
metadata
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sinatra-erbjoiner
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Christian Tschoepe
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-06-26 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: sinatra
|
16
|
+
requirement: &16877820 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 1.4.3
|
22
|
+
- - ! '>='
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: '1.4'
|
25
|
+
type: :runtime
|
26
|
+
prerelease: false
|
27
|
+
version_requirements: *16877820
|
28
|
+
description: sinatra_erb_joiner allows you to join multiple erbs into an html document,
|
29
|
+
as well as to implement live template functionality with erbs.
|
30
|
+
email: christian.tschoepe@rackspace.com
|
31
|
+
executables: []
|
32
|
+
extensions: []
|
33
|
+
extra_rdoc_files: []
|
34
|
+
files:
|
35
|
+
- ./lib/sinatra-erbjoiner.rb
|
36
|
+
homepage: http://rubygems.org/gems/namehere
|
37
|
+
licenses: []
|
38
|
+
post_install_message:
|
39
|
+
rdoc_options: []
|
40
|
+
require_paths:
|
41
|
+
- lib
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
none: false
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 1.9.2
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
requirements: []
|
55
|
+
rubyforge_project:
|
56
|
+
rubygems_version: 1.8.11
|
57
|
+
signing_key:
|
58
|
+
specification_version: 3
|
59
|
+
summary: sinatra_erb_joiner allows you to join multiple erbs into an html document
|
60
|
+
test_files: []
|