Search code examples
ruby-on-railsrubysinatraslim-lang

Inheritance in Slim template language


With PHP using Twig I can do something like this:

layout.twig

<html>
<body>
{% block content %}{% endblock %}
</body>
</html>

form.twig

{% extends "layout.twig" %}
{% block content %}
<div class="form">{% block form %}{% endblock %}</div>
{% endblock %}

login.twig

{% extends form %}
{% block form %}
<form>
    <input type="text" name="email" />
    <input type="submit">
</form>
{% endblock %}

This way I have a layout for all pages, a layout for pages with forms and login page.

But with Slim I can specify only main layout that is parent for all templates:

layout.slim

html
  body ==yield

and special layouts for every page on my site:
login.slim

div.form
  form
    input type="text" name="email"
    input type="submit"

Is there a simple way to realize Twig-like inheritance with more than one level in Slim?


Solution

  • It looks that I found the solution for Slim with Sinatra:

    layout.slim

    html
      body
        == yield
    

    form.slim

    == slim :layout
      div.form
        == yield
    

    login.slim

    == slim :form
      form
        input type="text" name="email"
        input type="submit"