A
A
Andrey2015-08-09 14:36:20
Pug
Andrey, 2015-08-09 14:36:20

Jade. How to pass a variable from one template to another one, included?

In the page template, I have the .container block connected 3 times. But the first time with .container_one modifier, then .container_two and finally .container_three. So in the end it looks like this:

<div class="container container_one">
<div class="container container_two">
<div class="container container_three">

I decided that it would be great to move the container block into a separate jade file and include it, passing the variable when connecting.
In TWIG, this is done like this:
{% include 'template.html' with modificator %}
And in the included template, we write:
<div class="container {{ modificator }}">
That's it, the file will be connected with the variable passed to it when calling, and this variable will be used in the included file.
How to implement such a thing in Jade?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Konstantin Kitmanov, 2015-08-09
@f-end

Jade does not have such a beautiful construction, but it does have mixins .

// container.jade
mixin container(modifier)
    .container(class=modifier)

// index.jade
include ./container // надо явно заинклюдить файл с миксином

+container('container-one')
+container('container-two')
+container('container-three')

In this particular case, it's even easier:
// container.jade
mixin container()
    .container&attributes // http://jade-lang.com/reference/attributes/

// index.jade
include ./container

+container().container-one
+container().container-two
+container().container-three

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question