Class: Yuzu::Core::TemplateMethods

Inherits:
Object
  • Object
show all
Defined in:
lib/yuzu/core/template.rb

Overview

The root object that contains functions that can be called from inside the Haml template.

Instance Method Summary collapse

Constructor Details

#initialize(siteroot) ⇒ TemplateMethods

Returns a new instance of TemplateMethods.



91
92
93
# File 'lib/yuzu/core/template.rb', line 91

def initialize(siteroot)
  @siteroot = siteroot
end

Instance Method Details

#format_date(raw_date) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/yuzu/core/template.rb', line 124

def format_date(raw_date)
  if not raw_date.is_a?(String) and not raw_date.is_a?(Time)
    return raw_date
  end

  if raw_date.is_a?(String)
    raw_date = Time.parse(raw_date)
  end

  date = raw_date.strftime("%Y-%m-%d")

  months = {
    "01" => "January",
    "02" => "February",
    "03" => "March",
    "04" => "April", 
    "05" => "May",
    "06" => "June",
    "07" => "July",
    "08" => "August", 
    "09" => "September",
    "10" => "October",
    "11" => "November",
    "12" => "December"
  }

  year, month, day = date.split("-")[0..2]
  month = months[month]

  "#{year} #{month} #{day}"
end

#insert_contents(filename, method = :raw_contents) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/yuzu/core/template.rb', line 103

def insert_contents(filename, method=:raw_contents)
  path = Helpers::Path.new(filename)
  return (Html::Comment.new << "File #{filename} not found.") if not path.exists?

  website_file = @siteroot.find_file_by_path(path)

  if not website_file.nil?
    website_file.send(method)

  else

    f = File.open(filename, "r")
    contents = f.read
    f.close

    return contents if method == :raw_contents
    Yuzu::Translators::Translator.translate(contents, path.extension)

  end
end

#insert_raw_file(filename) ⇒ Object



95
96
97
# File 'lib/yuzu/core/template.rb', line 95

def insert_raw_file(filename)
  insert_contents(filename, :raw_contents)
end

#insert_rendered_contents(filename) ⇒ Object



99
100
101
# File 'lib/yuzu/core/template.rb', line 99

def insert_rendered_contents(filename)
  insert_contents(filename, :rendered_contents)
end