2017-04-14 74 views
-1

我知道并使用html,js,less。但我是nodejs,pug/jade的新手。试图了解它是如何工作的。如何开始与帕格工作?

我读过很多关于帕格/玉的语法的教程,但我还没有找到一个教程如何使用它。

我必须在哪写代码? 如何在浏览器中打开它? 我需要什么?

+0

这是一个很好的问题,它只是在这里(Stackoverflow)人们对于如何提出良好的技术问题有非常高的标准。 – Costa

回答

1

打开你的终端(CTRL + ALT +许多计算机牛逼

然后输入了:sudo npm install pug --global

这将在您的计算机上安装的哈巴狗。您可能不需要上述命令中的sudo

然后键入pug --help

这将会给你:

Usage: pug [options] [dir|file ...] 

Options: 

-h, --help    output usage information 
-V, --version   output the version number 
-O, --obj <str|path> JSON/JavaScript options object or file 
-o, --out <dir>  output the rendered HTML or compiled JavaScript to <dir> 
-p, --path <path>  filename used to resolve includes 
-b, --basedir <path> path used as root directory to resolve absolute includes 
-P, --pretty   compile pretty HTML output 
-c, --client   compile function for client-side 
-n, --name <str>  the name of the compiled template (requires --client) 
-D, --no-debug   compile without debugging (smaller functions) 
-w, --watch   watch files for changes and automatically re-render 
-E, --extension <ext> specify the output file extension 
-s, --silent   do not output logs 
--name-after-file  name the template after the last section of the file path (requires --client and overriden by --name) 
--doctype <str>  specify the doctype on the command line (useful if it is not specified by the template) 

Examples: 

# Render all files in the `templates` directory: $ pug templates 

# Create {foo,bar}.html: $ pug {foo,bar}.pug 

# Using `pug` over standard input and output streams $ pug <my.pug> my.html $ echo 'h1 Pug!' | pug 

# Render all files in `foo` and `bar` directories to `/tmp`: $ pug foo bar --out /tmp 

# Specify options through a string: $ pug -O '{"doctype": "html"}' foo.pug 
# or, using JavaScript instead of JSON $ pug -O "{doctype: 'html'}" foo.pug 

# Specify options through a file: $ echo "exports.doctype = 'html';" > options.js $ pug -O options.js foo.pug 
# or, JSON works too $ echo '{"doctype": "html"}' > options.json $ pug -O options.json foo.pug 

您可以按照这种模式与大多数node.js的命令行工具。

如果您没有NPM和节点。js安装你当然也需要这些:)

对于帕格,我建议从上面列出的例子开始,最后,你会想创建实际的文件和目录写在帕格,然后使用命令行将它们编译为HTML。

如果你正在编写静态网站,这将做得很好。如果你想编写动态网站,帕格根据各种用户特定的数据创建一个不同的页面,你将要学习使用像express.js这样的帕格。

0

我认为有很多方法可以与帕格一起工作。

这里是我该怎么办:

  • 首先我创建了一个节点/ Express服务器。他会处理我的路线并服务我的帕格文件。
  • 然后我配置我的服务器与帕格工作:app.set('view engine', 'pug');(这一行会告诉你要使用帕格文件)。

app.set('views', './views')(这条线将设置views目录 - 在这里您可以创建你的哈巴狗文件

  • ,那么你只是在你的路由使用res.render('filename',{variable you want to give to your view});
+0

谢谢你的回复。但我仍然不明白。 对不起,您可以告诉我关于每一步的细节吗?如何在浏览器中显示帕格码的结果? 我安装node.js并启动它。另外我启动命令提示符。 接下来应该做什么? 我必须在哪写“app.set('view engine','pug');”和其他线? – Beginner

0

我发现。在YouTube上回答

  1. 首先你必须启动node.js和node.js命令提示符
  2. 键入的node.js命令提示:
    NPM安装哈巴狗
    NPM安装哈巴狗-CLI -g

  3. 创建例如 “index.pug”。

  4. 使用“cd”功能在命令提示符下更改目录(放置index.pug的位置)。例如“cd C:\ node”
  5. 在命令propmt中键入“pug -w index.pug”以获得转换后的index.html。