Public Instance methods
Freeze the template caches. Should be called after precompiling all templates during application startup, if you don’t want to allow templates to be cached at runtime. In addition to ensuring that no templates are compiled at runtime, this also speeds up rendering by freezing the template caches, so that a mutex is not needed to access them.
# File lib/roda/plugins/precompile_templates.rb 55 def freeze_template_caches! 56 _freeze_layout_method 57 58 opts[:render] = render_opts.merge( 59 :cache=>render_opts[:cache].freeze, 60 :template_method_cache=>render_opts[:template_method_cache].freeze, 61 ).freeze 62 self::RodaCompiledTemplates.freeze 63 64 nil 65 end
Precompile the templates using the given options. Note that this doesn’t handle optimized template methods supported in newer versions of Roda
, but there are still cases where makes sense to use it.
You can call precompile_templates
with the pattern of templates you would like to precompile:
precompile_templates "views/**/*.erb"
That will precompile all erb template files in the views directory or any subdirectory.
If the templates use local variables, you need to specify which local variables to precompile, which should be an array of symbols:
precompile_templates 'views/users/_*.erb', locals: [:user]
You can specify other render options when calling precompile_templates
, including :cache_key
, :template_class
, and :template_opts
. If you are passing any of those options to render/view for the template, you should pass the same options when precompiling the template.
To compile inline templates, just pass a single hash containing an :inline to precompile_templates
:
precompile_templates inline: some_template_string
# File lib/roda/plugins/precompile_templates.rb 93 def precompile_templates(pattern, opts=OPTS) 94 if pattern.is_a?(Hash) 95 opts = pattern.merge(opts) 96 end 97 98 if locals = opts[:locals] 99 locals.sort! 100 else 101 locals = EMPTY_ARRAY 102 end 103 104 compile_opts = if pattern.is_a?(Hash) 105 [opts] 106 else 107 Dir[pattern].map{|file| opts.merge(:path=>File.expand_path(file, nil))} 108 end 109 110 instance = allocate 111 compile_opts.each do |compile_opt| 112 template = instance.send(:retrieve_template, compile_opt) 113 begin 114 Render.tilt_template_compiled_method(template, locals, self) 115 rescue NotImplementedError 116 # When freezing template caches, you may want to precompile a template for a 117 # template type that doesn't support template precompilation, just to populate 118 # the cache. Tilt rescues NotImplementedError in this case, which we can ignore. 119 nil 120 end 121 end 122 123 nil 124 end
Precompile the given views with the given locals, handling optimized template methods.
# File lib/roda/plugins/precompile_templates.rb 127 def precompile_views(views, locals=EMPTY_ARRAY) 128 instance = allocate 129 views = Array(views) 130 131 if locals.empty? 132 opts = OPTS 133 else 134 locals_hash = {} 135 locals.each{|k| locals_hash[k] = nil} 136 opts = {:locals=>locals_hash} 137 end 138 139 views.each do |view| 140 instance.send(:retrieve_template, instance.send(:render_template_opts, view, opts)) 141 end 142 143 if locals_hash 144 views.each do |view| 145 instance.send(:_optimized_render_method_for_locals, view, locals_hash) 146 end 147 end 148 149 nil 150 end