简体   繁体   English

使用 javascript 一次为多个网页重用 html 模板

[英]Reusing an html template for multiple webpages at a time with javascript

A few months ago, I started a website.几个月前,我开了一个网站。 My problem is that I am starting to get a lot of pretty much what are just duplicate pages.我的问题是我开始得到很多只是重复页面的东西。 I copy the raw HTML for my homepage and then get rid of everything that is very specific to my homepage, and that is only so I can use my same header and navigation menus.我为我的主页复制了原始的 HTML,然后删除了与我的主页非常相关的所有内容,只有这样我才能使用相同的 header 和导航菜单。 All of my pages have a lot of code in common.我所有的页面都有很多共同的代码。 It is getting really tedious to redo this for each page.为每一页重做这个变得非常乏味。 I know people have had this problem before, and I'm sure that there is some big secret (to me) API that everybody already knows about for doing this.我知道人们以前遇到过这个问题,而且我确信每个人都已经知道这样做的一些大秘密(对我而言)API。 But as I am approaching 50 pages, what I need is a basic HTML template page (which I can make) and then some javascript just to print that code out into one of my other pages.但是当我接近 50 页时,我需要一个基本的 HTML 模板页面(我可以制作),然后是一些 javascript 只是为了将该代码打印到我的其他页面之一。 So if I want to add another item to my menu, I don't have to go through and change all 50 HTML pages, I can just change the template, and then that will be mushed into every other page.因此,如果我想在我的菜单中添加另一个项目,我不必通过 go 并更改所有 50 个 HTML 页面,我只需更改模板,然后将其粘贴到所有其他页面中。 Maybe there is a server-side solution for this too.也许也有一个服务器端解决方案。 And feel free to mention that.并随时提及。 But I am really good with javascript and not so good at server-side stuff.但我真的很擅长 javascript 而不是很擅长服务器端的东西。 I really don't want this to affect my page load times (which I am very proud of and have worked very hard on).我真的不希望这影响我的页面加载时间(我对此感到非常自豪并且非常努力)。 So maybe server-side is the solution for me.所以也许服务器端是我的解决方案。 But If anyone has any ideas, I would appreciate it very much.但如果有人有任何想法,我将不胜感激。 Thank you in advance.先感谢您。

you mean all the pages you have right now are static?您的意思是您现在拥有的所有页面都是 static?

if yes, don't do it.如果是,请不要这样做。 if you generate all the pages in that way after a while you can't expand your website easily and gods forbid if you wanna change a small design then you have to do it in all pages.如果您在一段时间后以这种方式生成所有页面,您将无法轻松扩展您的网站,如果您想更改一个小设计,那么您必须在所有页面中进行。

for dynamic pages you only design the page once and you load the parts that change with JS or PHP.对于动态页面,您只需设计一次页面,然后加载随 JS 或 PHP 更改的部分。

you need 3 things:你需要三样东西:

1- HTML Template of a page 1- HTML 页面模板

2- Data base (SQL or NOSQL) 2- 数据库(SQL 或 NOSQL)

3- a backend language to communicate with the database and load the data for you based on the url that user enters. 3- 与数据库通信并根据用户输入的 url 为您加载数据的后端语言。 this can be anything from php to python这可以是从 php 到 python 的任何东西

4(optional) to make your website mobile friendly and possible to work with by low speed internet, get the data with js and load it to the page partially. 4(可选)使您的网站对移动设备友好并且可以通过低速互联网使用,使用 js 获取数据并将其部分加载到页面。

if you ask my opinion, php laravel is really good that takes care of a lot of things for you.如果您问我的意见,php laravel 真的很好,可以为您处理很多事情。 and for the js part you can use Vuejs.对于 js 部分,您可以使用 Vuejs。

have a good day!祝你有美好的一天!

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

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