module Roda::RodaPlugins::PrecompileTemplates::ClassMethods
def freeze_template_caches!
up rendering by freezing the template caches, so that a mutex is not needed to access
In addition to ensuring that no templates are compiled at runtime, this also speeds
application startup, if you don't want to allow templates to be cached at runtime.
Freeze the template caches. Should be called after precompiling all templates during
def freeze_template_caches! _freeze_layout_method opts[:render] = render_opts.merge( :cache=>render_opts[:cache].freeze, :template_method_cache=>render_opts[:template_method_cache].freeze, ).freeze self::RodaCompiledTemplates.freeze nil end
def precompile_templates(pattern, opts=OPTS)
to +precompile_templates+:
To compile inline templates, just pass a single hash containing an :inline
should pass the same options when precompiling the template.
are passing any of those options to render/view for the template, you
including +:cache_key+, +:template_class+, and +:template_opts+. If you
You can specify other render options when calling +precompile_templates+,
precompile_templates 'views/users/_*.erb', locals: [:user]
variables to precompile, which should be an array of symbols:
If the templates use local variables, you need to specify which local
any subdirectory.
That will precompile all erb template files in the views directory or
precompile_templates "views/**/*.erb"
like to precompile:
You can call +precompile_templates+ with the pattern of templates you would
there are still cases where makes sense to use it.
handle optimized template methods supported in newer versions of Roda, but
Precompile the templates using the given options. Note that this doesn't
def precompile_templates(pattern, opts=OPTS) if pattern.is_a?(Hash) opts = pattern.merge(opts) end if locals = opts[:locals] locals.sort! else locals = EMPTY_ARRAY end compile_opts = if pattern.is_a?(Hash) [opts] else Dir[pattern].map{|file| opts.merge(:path=>File.expand_path(file, nil))} end instance = allocate compile_opts.each do |compile_opt| template = instance.send(:retrieve_template, compile_opt) begin Render.tilt_template_compiled_method(template, locals, self) rescue NotImplementedError # When freezing template caches, you may want to precompile a template for a # template type that doesn't support template precompilation, just to populate # the cache. Tilt rescues NotImplementedError in this case, which we can ignore. nil end end nil end
def precompile_views(views, locals=EMPTY_ARRAY)
def precompile_views(views, locals=EMPTY_ARRAY) instance = allocate views = Array(views) if locals.empty? opts = OPTS else locals_hash = {} locals.each{|k| locals_hash[k] = nil} opts = {:locals=>locals_hash} end views.each do |view| instance.send(:retrieve_template, instance.send(:render_template_opts, view, opts)) end if locals_hash views.each do |view| instance.send(:_optimized_render_method_for_locals, view, locals_hash) end end nil end