Autodetect Loaded Markups
whitehat101 opened this issue · comments
I'm working on a gem for for a rails app. If the user has the relevant gems loaded up, I'd like to use the Tilt::Template for sprockets, if they don't have it, meh.
# try loading common markups
%w(erb haml liquid md radius slim str textile wiki).
each do |ext|
begin
config.angular_templates.markups << ext if Tilt[ext]
rescue LoadError
# They don't have the required library required. Oh well.
end
end
It seems to work exactly as I hoped, however when I tested with Rails 4.1 + Spring, when I start the rails console
for the first time I get the autoloading ... in a non thread-safe way warning. It doesn't seem to happen with rails server
, or if spring is disabled.
WARN: tilt autoloading 'liquid' in a non thread-safe way; explicit require 'liquid' suggested.
WARN: tilt autoloading 'redcarpet' in a non thread-safe way; explicit require 'redcarpet' suggested.
WARN: tilt autoloading 'rdiscount' in a non thread-safe way; explicit require 'rdiscount' suggested.
WARN: tilt autoloading 'rdiscount' in a non thread-safe way; explicit require 'rdiscount' suggested.
WARN: tilt autoloading 'bluecloth' in a non thread-safe way; explicit require 'bluecloth' suggested.
WARN: tilt autoloading 'kramdown' in a non thread-safe way; explicit require 'kramdown' suggested.
WARN: tilt autoloading 'maruku' in a non thread-safe way; explicit require 'maruku' suggested.
WARN: tilt autoloading 'radius' in a non thread-safe way; explicit require 'radius' suggested.
WARN: tilt autoloading 'redcloth' in a non thread-safe way; explicit require 'redcloth' suggested.
WARN: tilt autoloading 'wikicloth' in a non thread-safe way; explicit require 'wikicloth' suggested.
WARN: tilt autoloading 'creole' in a non thread-safe way; explicit require 'creole' suggested.
I'm a little confused, because I expected the Rails is pulling Tilt 1.4, and after reading that source, I'm not confused.require
to cause the LoadError
exception, so I'm not sure why it's even getting to the warn block -- I definitely don't have libraries for liquid or radius installed.
Should I just silence the output from this block and move on, or is there a better way to check if there is a library loaded for an extension?
With HAML or SLIM there is a 1:1 extension to library ratio, and I could check for defined constants, but with markdown there are dozens, and six months from now there could be a new markdown library. I'd like, "if Tilt supports it, we support it" simplicity.
At the moment there is no better way to check if a template engine is available. The only way to check for lazily loaded template engine is to invoke require
which is indeed not thread-safe.
Why do you need a list of available template engines?
We're setting up sprockets to process html-ish things on the asset pipeline for use with AngularJS:
app.config.angular_templates.markups.each do |ext|
Sprockets.register_engine ".#{ext}", Tilt[ext]
end
Sprockets.register_engine '.html', AngularRailsTemplates::Template
You can put a foo.html.haml
in the rails assets folder, and it will render the haml into html and turn that html into a javascript blob that directly inserts the template into Angular's template cache.
We also print the markups
array in a javascript comment to let the user know what extensions have been registered and will be processed.
I'm closing this in favor of keeping the discussion in #228. In 2.1 I will implement two methods: one for attempting to lazy load all available template classes, and another for detecting what templates/extensions are available.