Module: Cuca::Generator::View

Included in:
BaseList
Defined in:
lib/cuca/generator/view.rb

Overview

View Generator

The view generator allows you to define content using an ERB template - similar to Ruby on Rails.

Example use within a Controller:

require 'cuca/generator/view'
class IndexController
  include Cuca::Generator::View

  def run
    @some_variable = "Stuff"
    @page_title = "Hello World"
    view('template.rhtml')
  end
end

And the template (template.rhtml)

<html>
 <head>
  <title><%= @page_title %></title>
 </head>
 <body>
   <% (1..10).each do |idx|  %>  <!-- Embedded Ruby Code ->
     <%= idx.to_s %> - <b>Some variable: <%= @stuff %></b><br/>
   <% end %>
   An external Widget: <%= Link('/to/somewhere') { b { "Click me" }} %>
 </body>
</html>

For more information about ERB templates visit it’s website.

Instance Method Summary collapse

Instance Method Details

#view(filename) ⇒ Object

Procuce content and append to widget.



63
64
65
# File 'lib/cuca/generator/view.rb', line 63

def view(filename)
   @_content << viewtext(filename)  
end

#view_p(template) ⇒ Object

Equivaltent to view but take template as a string.



74
75
76
# File 'lib/cuca/generator/view.rb', line 74

def view_p(template)
  @_content << viewtext_p(template)
end

#viewtext(filename = nil) ⇒ Object

Produce content by a template file. This will return the generated markup as a string



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/cuca/generator/view.rb', line 45

def viewtext(filename=nil)
  view_dir = $cuca_path + '/' + App::config['view_directory']

  begin
     template = File.read(view_dir + "/#{filename}")
  rescue => e
     return "Error reading template: #{e}"
  end

  if RUBY_VERSION > '1.8' && Cuca::App.config['view_encoding'] then
    template.force_encoding(Cuca::App.config['view_encoding'])
  end
 
  viewtext_p(template)
end

#viewtext_p(template) ⇒ Object

Normally you have your view (the template) within a separate file. Nevertheless you can passing as a string to this function.



69
70
71
# File 'lib/cuca/generator/view.rb', line 69

def viewtext_p(template)
  ERB.new(template).result(GeneratorContext.new(get_assigns, self).get_bindings)
end