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.

Defined Under Namespace

Classes: AssignBindings

Instance Method Summary collapse

Instance Method Details

#view(filename) ⇒ Object

Procuce content and append to widget.



102
103
104
# File 'lib/cuca/generator/view.rb', line 102

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

#view_p(template) ⇒ Object

Equivaltent to view but take template as a string.



113
114
115
# File 'lib/cuca/generator/view.rb', line 113

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

#viewtext(filename = nil) ⇒ Object

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



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/cuca/generator/view.rb', line 86

def viewtext(filename=nil)
  view_dir = $cuca_path + '/' + App::config['view_directory']
  begin
     f = File.open(view_dir + "/#{filename}", 'r')
  rescue => e
     return "Error opening template: #{e}"
  end
 
  template = f.read
  f.close

  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.



108
109
110
# File 'lib/cuca/generator/view.rb', line 108

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