module Roda::RodaPlugins::PrecompileTemplates

  1. lib/roda/plugins/precompile_templates.rb

The precompile_templates plugin adds support for precompiling template code. This can result in a large memory savings for applications that have large templates or a large number of small templates if the application uses a forking webserver. By default, template compilation is lazy, so all the child processes in a forking webserver will have their own copy of the compiled template. By using the precompile_templates plugin, you can precompile the templates in the parent process before forking, and then all of the child processes can use the same precompiled templates, which saves memory.

Another advantage of the precompile_templates plugin is that after template precompilation, access to the template file in the file system is no longer needed, so this can be used with security features that do not allow access to the template files at runtime.

After loading the plugin, you should call precompile_views with an array of views to compile, using the same argument you are passing to view or render:

plugin :precompile_templates
precompile_views %w'view1 view2'

If the view requires local variables, you should call precompile_views with a second argument for the local variables:

plugin :precompile_templates
precompile_views :view3, [:local_var1, :local_var2]

After all templates are precompiled, you can optionally use freeze_template_caches!, which will freeze the template caches so that any template compilation at runtime will result in an error. This also speeds up template cache access, since the template caches no longer need a mutex.

freeze_template_caches!

Note that you should use Tilt 2.0.1+ if you are using this plugin, so that locals are handled in the same order.

Methods

Public Class

  1. load_dependencies

Public Class methods

load_dependencies(app, opts=OPTS)

Load the render plugin as precompile_templates depends on it.

[show source]
   # File lib/roda/plugins/precompile_templates.rb
45 def self.load_dependencies(app, opts=OPTS)
46   app.plugin :render
47 end