简体   繁体   English

如何在另一个文件中包含 html 文件以避免重复代码?

[英]How to include html file in another to avoid repeating code?

I code with django and when I am working with django template, I do the below to avoid repeating code.我使用 django 进行编码,当我使用 django 模板时,我执行以下操作以避免重复代码。 Let me illustrate it with an example:让我用一个例子来说明它:

Suppose I have two pages in my website:假设我的网站中有两个页面:

1) home 2) about 1) 家 2) 关于

In django I code as below:在 django 中,我的代码如下:

I first build a base.html:我先建一个base.html:

<!DOCTYPE html>
<html>
    <head>
        <title>{% block title %}home{% endblock title %}</title>
        <link rel="stylesheet" href="{% static 'css/bootstrap.min.css' %}">
        <link rel="stylesheet" href="{% static 'css/my-base-css.css' %}">
        {% block stylesheet %}{% endblock stylesheet %}
    </head>
    <body>
        <h1>this is my site</h1>
        {% block body %}{% endblock body %}
    </body>
</html>

I then build home.html:然后我建立 home.html:

{% extends 'base.html' %}

{% block stylesheet %}<link rel="stylesheet" href="{% static 'css/home-page-css.css' %}">
{% endblock stylesheet %}

{% block body %}
    <h2>This is home</h2>
{% endblock body %}

I also build about.html:我还构建了 about.html:

{% extends 'base.html' %}

{% block title %}
my-website-about
{% endblock title %}

{% block stylesheet %}<link rel="stylesheet" href="{% static 'css/about-page-css.css' %}">
{% endblock stylesheet %}

{% block body %}
    <h2>This is about</h2>
{% endblock body %}

I now want to do the same without having a backend.我现在想在没有后端的情况下做同样的事情。 I have a static website.我有一个 static 网站。 How can I do the same without having a backend like django or php, etc.?如果没有 django 或 php 等后端,我该如何做同样的事情?

There is a similar question in here: Include another HTML file in a HTML file这里有一个类似的问题: Include another HTML file in a HTML file

This can solve my problem.这可以解决我的问题。 However, it is a little different from what I want.但是,它与我想要的有点不同。 It is loading another html in an html file but I am looking for extending another html;它正在 html 文件中加载另一个 html 但我正在寻找扩展另一个 html; I mean adding to another base.html and having a new html file我的意思是添加到另一个 base.html 并拥有一个新的 html 文件

It looks like you're using the Django Templating Language which is similar to Jinja (I've only used this one because I've mostly used Flask but the way it works should be similar).看起来您正在使用类似于 Jinja 的 Django 模板语言(我只使用了这个,因为我主要使用 Flask,但它的工作方式应该相似)。 Django uses this language in its template engine and the way it works is by basically taking your HTML file, passing it through a backend (Django), and replacing the variables/logic you have there with actual values. Django 在其模板引擎中使用这种语言,它的工作方式基本上是获取您的 HTML 文件,将其传递给后端(Django),然后用实际值替换您那里的变量/逻辑。 In the end, you'll have a fully built HTML file.最后,您将拥有一个完整构建的 HTML 文件。

The short answer is no.最简洁的答案是不。

From my understanding of template engines, you need to have a backend that can actually work out which values (by replacing this syntax { some_variable } ) you should put in the final HTML output.根据我对模板引擎的理解,您需要有一个后端可以实际计算出您应该将哪些值(通过替换此语法{ some_variable } )放入最终的 HTML output 中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM