Module: PrestoView::Config

Includes:
PrestoCore::Utils, Utils
Included in:
ClassAPI, Slice
Defined in:
lib/presto/view/config.rb

Overview

Note:

configs here are available for both controllers and slices

Note:

methods here used to setup the View API. to have an consistent setup, it should be write-able only at class definition. any later updates should be prohibited. to accomplish this, any updates for mounted controllers are silently dropped.

Constant Summary

Constant Summary

Constants included from PrestoCore::Utils

PrestoCore::Utils::PATH_MODIFIERS, PrestoCore::Utils::STATUS__NOT_FOUND, PrestoCore::Utils::STATUS__OK, PrestoCore::Utils::STATUS__PERMANENT_REDIRECT, PrestoCore::Utils::STATUS__REDIRECT, PrestoCore::Utils::STATUS__RESTRICTED, PrestoCore::Utils::STATUS__SERVER_ERROR

Instance Method Summary (collapse)

Methods included from Utils

#engine_supported?, #guess_extension, #guess_layout, #guess_path, #guess_scope_and_context

Methods included from PrestoCore::Utils

build_path, #extract_controllers, is_controller?, normalize_path, rootify_url

Instance Method Details

- (Object) compile(*actions, &proc)

Note:

compiler will work only if block given

for most apps, most expensive operations are fs operations and template compilation. it is possible to avoid these operations by storing compiled templates in memory and just render them later.

this method allow to enable compiler for all or just some actions. actions should be passed one by one as arguments. if no args passed, all actions will use compiler.

compiler behavior are determined by given block.

  • if block returns any positive value, compiled template will be used.

  • if block returns :update [Symbol], template for current action will be recompiled.

  • if block returns :purge or :truncate [Symbol], templates for all actions on current controller will be recompiled.

Examples:

compile templates for all actions

view.compile { true }

compile templates only for #summary and #content

view.compile(:summary, :content) { true }

update cache as needed

view.compile do
  :update if http.params['update-compiler']
end

view.compile do |action|
  :update if action == :content && http.params['update-content']
end

Parameters:

  • *actions (Array)
  • &proc (Proc)


219
220
221
222
223
224
225
226
227
228
229
# File 'lib/presto/view/config.rb', line 219

def compile *actions, &proc

  if proc && configurable?
    actions = [:*] if actions.size == 0
    actions.each { |a| @compile[a] = proc }
  end

  action = actions.first || :*
  @setup['Compiler "%s"' % action] ||= @compile[action] ||
      (@controller.ctrl.slice.view.compile(action) if @controller)
end

- (Object) compiler_pool(pool = nil)

by default, compiled templates kept in memory. this option allow to use custom pool.

Examples:

using MongoDB pool for compiled templates

db = Mongo::Connection.new.db('compiler-pool')
view.compiler_pool ::PrestoCache::MongoDB.new(db)


237
238
239
240
241
242
# File 'lib/presto/view/config.rb', line 237

def compiler_pool pool = nil
  @compiler_pool = pool if pool && configurable?
  @setup[:compiler_pool] ||= @compiler_pool ||
      (@controller.ctrl.slice.view.compiler_pool if @controller) ||
      ::PrestoCache::Memory.new
end

- (Object) engine(engine = nil, *args)

set engine to be used.

Examples:

http.engine :Haml

engine with extension

http.engine :Haml

engine with extension and opts

http.engine :Haml, default_encoding: 'UTF-8'

engine with opts

http.engine :Haml, :some_engine_argument, some_engine_option: 'some value'

Parameters:

  • engine (Symbol) (defaults to: nil)

    accepts any of Tilt supported engine

  • *args (String)

    any args to be passed to engine at initialization



40
41
42
43
44
45
46
47
48
49
# File 'lib/presto/view/config.rb', line 40

def engine engine = nil, *args
  if engine && configurable?
    @engine = engine_supported?(engine)
    @engine_args = args
    @ext = guess_extension(@engine)
  end
  @setup[:engine] ||= @engine ||
      (@controller.ctrl.slice.view.engine if @controller) ||
      ::Presto.setup.view.engine
end

- (Object) engine_args(*args)

alternative way to define arguments to be passed to engine at initialization

Examples:

http.engine_args default_encoding: 'UTF-8'


55
56
57
58
59
60
61
# File 'lib/presto/view/config.rb', line 55

def engine_args *args
  @engine_args = args if args.size > 0 && configurable?
  @setup[:engine_args] ||= @engine_args ||
      (@controller.ctrl.slice.view.engine_args if @controller) ||
      ::Presto.setup.view.engine_args ||
      []
end

- (Object) engine_opts

convert engine arguments into options suitable for Tilt::Template#initialize



64
65
66
# File 'lib/presto/view/config.rb', line 64

def engine_opts
  @setup[:engine_opts] ||= engine_args.inject({}) { |opts, a| a.is_a?(Hash) ? opts.update(a) : opts.update(a => true) }
end

- (Object) ext(ext = nil)

set the extension used by templates



69
70
71
72
73
74
# File 'lib/presto/view/config.rb', line 69

def ext ext = nil
  @ext = normalize_path(ext.to_s) if ext && configurable?
  @setup[:ext] ||= @ext ||
      (@controller.ctrl.slice.view.ext if @controller) ||
      ::Presto.setup.view.ext
end

- (Config) initialize(*args)

A new instance of Config

Returns:

  • (Config)

    a new instance of Config



17
18
19
20
# File 'lib/presto/view/config.rb', line 17

def initialize *args
  @setup = {}
  @compile = {}
end

- (Object) layout(layout = nil, *actions) Also known as: layouts

Note:

by default no layout will be rendered. if you need layout, use view.layout to set it.

set the layout to be used by some or all actions.

Examples:

set :master layout for :index and :register. other actions will use default layout


class Example
  # basic setup
  view.layout :master, :index, :register
end

Parameters:

  • layout (String) (defaults to: nil)
  • *actions (Array)

    list of actions to use layout. if ignored, all actions will use given layout.



124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/presto/view/config.rb', line 124

def layout layout = nil, *actions
  if (layout || layout == false) && configurable?
    @layouts ||= Hash.new
    layout = layout.to_s unless layout == false
    if actions.size == 0
      actions = [:*]
      @master_layout = layout
    end
    actions.each { |a| @layouts[a] = layout }
  end
  @setup[:layouts] ||= @layouts ||
      (@controller.ctrl.slice.view.layout if @controller) || {}
end

- (Object) layouts_path(path = nil)

Note:

should be relative to root, either root is set by app or manually.

set custom path for layouts.



102
103
104
105
106
# File 'lib/presto/view/config.rb', line 102

def layouts_path path = nil
  @layouts_path = normalize_path(path) if path && configurable?
  @setup[:layouts_path] ||= @layouts_path ||
      (@controller.ctrl.slice.view.layouts_path if @controller)
end

- (Object) master_layout

returns the layout that was set by `view.layout(:name)` other ones, defined by `view.layout(:name, :action1, :action2, :etc)`, are not master



142
143
144
145
# File 'lib/presto/view/config.rb', line 142

def master_layout
  @setup[:master_layout] ||= @master_layout ||
      (@controller.ctrl.slice.view.master_layout if @controller)
end

- (Object) path(path = nil)

Note:

should be relative to app root.

set custom path for templates. use it only when root path are guessed automatically. when root are defined manually, path is automatically set to an empty string.



93
94
95
96
97
98
# File 'lib/presto/view/config.rb', line 93

def path path = nil
  @path = normalize_path(path) if path && configurable?
  @setup[:path] ||= @path ||
      (@controller.ctrl.slice.view.path if @controller) ||
      'view/'
end

- (Object) root(path = nil)

set custom root for templates.



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/presto/view/config.rb', line 77

def root path = nil
  if path && configurable?
    @root = normalize_path(path)
    # resetting path to an empty string cause root should be an absolute path to templates.
    @path, @setup[:path] = nil
    path ''
  end
  @setup[:root] ||= @root ||
      (@controller.ctrl.slice.view.root if @controller) ||
      ::PrestoCore::App.root
end

- (Object) scope(scope = nil, &proc)

set the custom scope for rendered templates. if no scope set, it will use the scope where the #view.render called.

if scope is a proc, proc will be executed inside controller instance and returned object will serve as scope.

Examples:

# app.rb
class Sandbox
  def initialize params
    @params = params
  end
end

class HelloWorld
  include Presto
  http.map

  view.scope do
    Sandbox.new http.params
  end

  def index
    view.render_partial
  end
end

# view/index.rhtml
Hello <%= @params['var'] %>

# /index/?var=World will return Hello World


179
180
181
182
183
# File 'lib/presto/view/config.rb', line 179

def scope scope = nil, &proc
  @scope = (proc || scope) if (scope || proc) && configurable?
  @setup[:scope] ||= @scope ||
      (@controller.ctrl.slice.view.scope if @controller)
end