H
H
HelpSophie2019-06-18 17:30:51
HTML
HelpSophie, 2019-06-18 17:30:51

Include HTML with conditions?

Given: there is a footer, it has a block that is different on different pages, block A, block B, block C.
The footer is a separate file that is connected using include.

<include src="source/html-blocks/block_footer.html"></include>

Can I somehow embed blocks with conditions inside the footer? How?
For example, on the main page inside the footer there should be block A, on catalog pages block B, on all other pages block C.
Use gulp and posthtml-include
task:
gulp.task("html", function () {
  return gulp.src("source/*.html")
    .pipe(posthtml([
      include()
    ]))
    .pipe(gulp.dest("build"));
});

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey delphinpro, 2019-06-18
@HelpSophie

Use full-fledged template engines, not any surrogates.
For example twig (gulp-twig for layout)

{% if some_variable == 2 %}
  {% include "file1.twig" %}
{% else %}
  {% include "file2.twig" %}
{% endif %}

UPD
Read the comments. Twig is what is called like html template engine. Those. a normal html file fed to a template engine will not cause compilation errors. This means that you can essentially just insert template engine control constructs into your html.

X
xmoonlight, 2019-11-05
@xmoonlight

To make it easier to manage chunks of HTML, use includeHTML .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question