简体   繁体   English

使用nodejs和pug进行客户端模板

[英]Client-Side templating with nodejs and pug

I have a web app that I'm building that has dynamic widgets being constructed on the client-side. 我正在构建一个Web应用程序,该应用程序具有在客户端构建的动态窗口小部件。 Currently I am using nodejs and pug as my server side templating library, and I like the simplicity of pug. 目前,我正在使用nodejs和pug作为服务器端模板库,并且我喜欢pug的简单性。

I would like to have a series of small pug files on the server that the client side can use as building blocks to construct the user desired widget. 我想在服务器上拥有一系列小的pug文件,客户端可以将其用作构建用户所需的小部件的构建块。

I tried using a previous solution found here: client side server side templating nodejs 我尝试使用此处找到的先前解决方案: 客户端服务器端模板化nodejs

However, that solution looks like overkill for what I want. 但是,该解决方案对于我想要的东西来说似乎有些矫kill过正。 Moreover, it looks like the ezel project is no longer maintained, it hasn't been updated in 2 years and it still uses jade (which npm gives me a lot of errors). 而且,看起来ezel项目不再维护,它在2年内没有更新,并且仍然使用玉器(npm给我带来很多错误)。

I just want to be able to construct my dynamic widgets in pug in the browser. 我只希望能够在浏览器的pug中构建动态小部件。 This page seems to have exactly what I want: https://pugjs.org/api/reference.html Specifically the pug.renderFile('path/to/file.pug', options); 该页面似乎正是我想要的: https ://pugjs.org/api/reference.html特别是pug.renderFile('path / to / file.pug',options); function seems like exactly what I want to use to dynamically build my widgets (the user has all the controls on how the widgets are constructed/displayed, so the browser needs to dynamically construct the html views) 函数似乎正是我想要用来动态构建窗口小部件的功能(用户拥有关于如何构建/显示窗口小部件的所有控件,因此浏览器需要动态构建html视图)

My issue is the dependence on: https://pugjs.org/js/pug.js And the need to do require('pug') in the browser. 我的问题是依赖于: https : //pugjs.org/js/pug.js以及在浏览器中执行require('pug')的需求。 I already have pug installed as part of my package.json. 我已经在我的package.json中安装了哈巴狗。 Is there a more robust way of getting pug.js directly? 是否有更健壮的方法直接获取pug.js? I am still new to web development, my background is in C++/Java, so I'm not entirely sure if using pug.js in the browser directly is the best solution or if there are better standard solution. 我仍然是Web开发的新手,我的背景是C ++ / Java,所以我不确定完全在浏览器中直接使用pug.js是最好的解决方案还是有更好的标准解决方案。 The stackoverflow question I posted is the only post I came across that is remotely similar. 我发布的stackoverflow问题是我遇到的唯一远程相似的帖子。

I researched and tested a solution that I really like. 我研究并测试了我真正喜欢的解决方案。 NPM has a cool package called pug-cli. NPM有一个很酷的软件包,称为pug-cli。

https://www.npmjs.com/package/pug-cli https://www.npmjs.com/package/pug-cli

I modified my npm start script to do the following: 我修改了npm start脚本以执行以下操作:

pug -c -w --name-after-file -o public/js/views views/client/

What this allows me to do is write my client views in put in the views/client folder. 这允许我做的是将我的客户视图写在views / client文件夹中。 A task is running in the background that monitors changes in views/client/. 任务在后台运行,监视视图/客户端/中的更改。 Upon changes, it complies .pug files from views/client/ folder into javascript and saves it into public/js/views/. 更改后,它会将views / client /文件夹中的.pug文件编译为javascript并将其保存到public / js / views /中。 Then in the client code you just include Template.js and call Template(parameters) in your js. 然后在客户端代码中,只需包含Template.js并在js中调用Template(parameters)。 There is no need for a pug.js on client side. 客户端上不需要pug.js。 This is with debugging, to turn debug off, run with -D 这是调试,要关闭调试,请使用-D运行

For instance, views/client/example.pug will get automatically complied to public/js/views/exampleTemplate.js Then all you have to do in the client is include this js file, and call exampleTemplate(params) to get your templated string (you can call this arbitrarily with different parameters get different views). 例如,views / client / example.pug将自动被编译为public / js / views / exampleTemplate.js。然后,您在客户端要做的就是包含此js文件,并调用exampleTemplate(params)来获取模板化字符串(您可以使用不同的参数任意调用此方法以获得不同的视图)。 This allows me to arbitrarily/dynamically compose and construct views from the client, when the views are unknown on the server side. 当服务器端的视图未知时,这使我能够从客户端任意/动态地组合和构造视图。

I like this approach for my workflow, but I am open to better suggestions. 我在工作流程中喜欢这种方法,但是我愿意接受更好的建议。

If you use webpack: 如果您使用webpack:

https://github.com/pugjs/pug-loader and https://github.com/willyelm/pug-html-loader serve well. https://github.com/pugjs/pug-loaderhttps://github.com/willyelm/pug-html-loader效果很好。

In case of rollup: 如果汇总:

https://www.npmjs.com/package/rollup-plugin-pug + https://www.npmjs.com/package/rollup-plugin-pug-html seem to be good solution (currently testing how it works, we are now experimenting with native es6 modules and backuping bundles with rollup) https://www.npmjs.com/package/rollup-plugin-pug + https://www.npmjs.com/package/rollup-plugin-pug-html似乎是不错的解决方案(目前正在测试其工作方式,我们现在正在尝试使用本机es6模块并通过汇总备份捆绑软件)

In case of browserify: 如果使用browserify:

https://www.npmjs.com/package/jadeify (never have tried) https://www.npmjs.com/package/jadeify (从未尝试过)

Also pug-cli have got -c key, so you can just run any watcher and generate js files as mentioned above, but it seems to be bit too straightforward since we've got variety of bundling tools now in 2017. pug-cli也具有-c键,因此您可以如上所述运行任何观察器并生成js文件,但是由于我们现在在2017年提供了多种捆绑工具,因此似乎有点太简单了。

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

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