0

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?

three
  • 8,262
  • 3
  • 35
  • 39

3 Answers3

7

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"
0

I believe that in Rails it doesn't exist something like template inheritance, but I believe that it isn't necessary with yield and content_for methods.

For example, you can have a _form.html.slim layout:

.form
  = yield

A _login.html.slim partial:

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

And when you want to display a login with the form layout, you should do something like:

= render partial: "login", layout: "form"
Pigueiras
  • 18,778
  • 10
  • 64
  • 87
0

login.html.slim

= render layout: 'form' do
  form
    input type="text" name="email"
    input type="submit"