Answer the question
In order to leave comments, you need to log in
When to use in pug mixins, when to block, when to include?
When to use in pug mixins, when to block, when to include? I don’t understand what to use when, everything seems to be clear with includes, and blocks and mixins cause confusion
Answer the question
In order to leave comments, you need to log in
Hello.
In fact, there is no single answer and rules for when to use what. Often, for example, in the Russian-speaking community, you can hear the opinion that they say mixins decide, and include should be used only in rare cases, for example, to connect these same mixins to the page, but I do not fully share this point of view.
I myself am engaged in the layout of sites, I write what I use now:
extends core/layout.pug
block vars
- var pageTitle = 'Название'
- var pageDescription = 'Описание'
- var pageKeywords = 'Ключевые слова'
- var userState= 'logout'
block content
include ../bemto/bemto
include mixins
doctype html
html(lang="ru")
block vars
head
meta(charset='utf-8')
meta(http-equiv='X-UA-Compatible', content='IE=edge')
meta(name='viewport', content='width=device-width, initial-scale=1, shrink-to-fit=no')
meta( name="format-detection" content="telephone=no")
link(href="https://fonts.googleapis.com/css?family=Montserrat:300,300i,400,400i,500,700,700i&subset=cyrillic" rel="stylesheet")
title #{pageTitle}
meta(name='description', content= pageDescription)
meta(name='keywords', content= pageKeywords)
link(rel="stylesheet" href="css/bundle.min.css")
body
block content
+b.overlay(id="overlay")
include ../blocks/product-card-modal
include ../blocks/cart-modal
include ../blocks/login-modal
block scripts
script(src="js/bundle.min.js")
1. Inside a mixin to get the children you pass to it
2. Inside a template that will extend another template in the future, to get the children from the template that extends that template.
3. Inside a template that extends to pass the children of
Examples 2 and 3 here: https://pugjs.org/language/inheritance.html
And here is the example for 1
mixin foo()
div.bar
block
+foo()
div.foo Hello world
<div class="bar">
<div class="foo">Hello world</div>
</div>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question