feat: extend view with initialize and content_for
This commit is contained in:
1 parent
a171d9d333
commit
6b7850d756
2 files changed
+38
-4
No files matched your search
+34
-4
@@ -2,17 +2,45 @@ module Prism
|
||||
class View
|
||||
class TemplateNotFound < StandardError; end
|
||||
|
||||
def initialize(roots)
|
||||
def initialize(roots, helpers: nil)
|
||||
@roots = Array(roots)
|
||||
@helpers = helpers
|
||||
@namespace_stack = []
|
||||
end
|
||||
|
||||
def render(name, locals: {}, layout: nil)
|
||||
@content_store = {}
|
||||
content = render_template(name.to_s, locals)
|
||||
return content unless layout
|
||||
render_template("layouts/#{layout}", locals.merge('content' => content))
|
||||
end
|
||||
|
||||
# content_for(:slot) { ... } inside the content template accumulates
|
||||
# here; the layout template rendered right after reads it back with a
|
||||
# plain content_for(:slot) call. Reset once per top-level #render so
|
||||
# slots don't leak between unrelated requests that reuse this View.
|
||||
def content_for(name)
|
||||
(@content_store ||= {})[name.to_sym].to_s
|
||||
end
|
||||
|
||||
def append_content_for(name, text)
|
||||
store = (@content_store ||= {})
|
||||
store[name.to_sym] = "#{store[name.to_sym]}#{text}"
|
||||
end
|
||||
|
||||
def content_for?(name)
|
||||
!(@content_store ||= {})[name.to_sym].to_s.empty?
|
||||
end
|
||||
|
||||
# Turns a fragment-cache key argument into a single cache key string.
|
||||
# Arrays let callers mix static scope names with model instances, e.g.
|
||||
# `cache [:sidebar, current_user, post]`; anything responding to
|
||||
# #cache_key (Orm::Model) contributes its own key, everything else is
|
||||
# stringified as-is.
|
||||
def expand_cache_key(key)
|
||||
Array(key).map { |part| part.respond_to?(:cache_key) ? part.cache_key : part.to_s }.join('/')
|
||||
end
|
||||
|
||||
def render_partial(name, locals: {})
|
||||
render_template(partial_name(qualify(name)), locals)
|
||||
end
|
||||
@@ -35,9 +63,11 @@ module Prism
|
||||
private
|
||||
|
||||
def render_template(name, locals)
|
||||
source = File.read(resolve(name))
|
||||
@namespace_stack.push(File.dirname(name))
|
||||
Template.render(source, locals, self)
|
||||
Instrumentation.instrument('render_template.prism', identifier: name) do
|
||||
source = File.read(resolve(name))
|
||||
@namespace_stack.push(File.dirname(name))
|
||||
Template.render(source, locals, self, @helpers)
|
||||
end
|
||||
ensure
|
||||
@namespace_stack.pop
|
||||
end
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
require_relative 'websocket/frame'
|
||||
require_relative 'websocket/handshake'
|
||||
require_relative 'websocket/connection'
|
||||
require_relative 'websocket/hub'
|
||||
Reference in new issue
Block a user