I'm working on a static website (so no real server support) and I would like to include a small slim snippet in another, possibly passing a variable to it.
Is this possible? In rails is quite easy, though the render
method, but I have no idea how to do it on slim (obviously load
method doesn't work for slim).
Slim contains Include
plugin that allows to include other files directly in template files at compile time:
require 'slim/include'
include partial_name
Documentations is available here: https://github.com/slim-template/slim/blob/master/doc/include.md
If you need to include the files at runtime
Slim::Template.new("#{ name }.slim").render
does the job (https://github.com/slim-template/slim#include-helper).
I would strongly reocommend checking out Middleman if you want to build a static website using Slim. Middleman borrows common helper functions like render
and partial
from Padrino, a web framework sort of like Rails but built using the more lightweight Sinatra (all of these are great software).
The poins is that you can configure Middleman to use slim and then nest partials (or layouts as well) arbitrarily. If you run into snags, check this stack overflow thread. It's pretty simple though!
The Middleman docs explain how to use partials here, and you can see how a real-world example looks in my gist for embedding an HTML5 video player.
Looks like it can be done in this way:
Slim::Template.new('template.slim', optional_option_hash).render(scope)
Found in slim readme: https://github.com/slim-template/slim
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With