Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include a slim file in another slim file

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).

like image 308
Francesco Belladonna Avatar asked Sep 21 '13 21:09

Francesco Belladonna


3 Answers

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).

like image 190
Igor Springer Avatar answered Nov 03 '22 19:11

Igor Springer


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.

like image 2
Steve Benner Avatar answered Nov 03 '22 19:11

Steve Benner


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

like image 1
Francesco Belladonna Avatar answered Nov 03 '22 19:11

Francesco Belladonna