Convert Figma logo to code with AI

pugjs logopug

Pug – robust, elegant, feature rich template engine for Node.js

21,636
1,958
21,636
315

Top Related Projects

3,758

HTML Abstraction Markup Language - A Markup Haiku

Minimal templating on steroids.

A powerful templating engine with inheritance, asynchronous control, and more (jinja2 inspired)

11,005

Liquid markup language. Safe, customer facing template language for flexible web apps.

Quick Overview

Pug (formerly known as Jade) is a high-performance template engine heavily influenced by Haml and implemented with JavaScript for Node.js and browsers. It offers a clean, whitespace-sensitive syntax for writing HTML templates, with features like mixins, includes, and inheritance to promote code reuse and maintainability.

Pros

  • Concise and readable syntax, reducing the amount of code needed to write HTML templates
  • Powerful features like mixins, includes, and template inheritance for better code organization and reuse
  • Integrates well with Express.js and other Node.js frameworks
  • Supports both server-side and client-side rendering

Cons

  • Learning curve for developers accustomed to traditional HTML syntax
  • Whitespace sensitivity can lead to errors if not properly managed
  • Limited browser support for client-side rendering compared to other template engines
  • Debugging can be challenging due to the abstraction layer between Pug and HTML

Code Examples

  1. Basic Pug template:
doctype html
html(lang="en")
  head
    title= pageTitle
  body
    h1 Welcome to Pug
    p This is a paragraph
  1. Using variables and conditionals:
- var user = {name: 'John', loggedIn: true}
h1 Welcome #{user.name}
if user.loggedIn
  p You are logged in
else
  p Please log in
  1. Creating a mixin for reusable components:
mixin card(title, content)
  .card
    h2= title
    p= content

+card('Hello', 'This is a card component')
+card('Another Card', 'With different content')

Getting Started

To use Pug in your Node.js project:

  1. Install Pug:
npm install pug
  1. Create a Pug file (e.g., template.pug):
h1= title
p= message
  1. Render the template in your Node.js code:
const pug = require('pug');

const html = pug.renderFile('template.pug', {
  title: 'Hello Pug',
  message: 'Welcome to Pug templating!'
});

console.log(html);

This will output the rendered HTML to the console. For more advanced usage, refer to the official Pug documentation.

Competitor Comparisons

3,758

HTML Abstraction Markup Language - A Markup Haiku

Pros of Haml

  • More mature and established, with a longer history in the Ruby ecosystem
  • Closer to HTML syntax, potentially easier for designers to learn
  • Better integration with Ruby on Rails and other Ruby-based frameworks

Cons of Haml

  • Less flexible indentation rules compared to Pug
  • Limited support outside of Ruby environments
  • Slightly more verbose syntax for certain elements

Code Comparison

Haml:

%html
  %head
    %title= page_title
  %body
    %h1.title Welcome to #{@name}'s page

Pug:

html
  head
    title= pageTitle
  body
    h1.title Welcome to #{name}'s page

Both Haml and Pug are templating languages that aim to simplify HTML writing. Haml has its roots in the Ruby ecosystem, while Pug (formerly Jade) is more closely associated with JavaScript and Node.js.

Pug offers more flexibility in indentation and a slightly more concise syntax. It's widely used in Node.js projects and has better support in JavaScript environments. Pug also provides more advanced features like mixins and includes.

Haml, on the other hand, has better integration with Ruby-based projects and may be more intuitive for those familiar with HTML syntax. It's particularly popular in Ruby on Rails applications.

The choice between Haml and Pug often depends on the primary programming language and ecosystem of the project, as well as personal preference for syntax style.

Minimal templating on steroids.

Pros of Handlebars.js

  • More familiar syntax for developers coming from HTML backgrounds
  • Easier to integrate with existing HTML templates
  • Supports precompilation for better runtime performance

Cons of Handlebars.js

  • Less expressive and more verbose than Pug
  • Limited built-in functionality compared to Pug's feature-rich syntax
  • Requires more boilerplate code for complex logic

Code Comparison

Handlebars.js:

<div class="entry">
  <h1>{{title}}</h1>
  {{#if author}}
    <h2>By {{author.name}}</h2>
  {{/if}}
</div>

Pug:

.entry
  h1= title
  if author
    h2 By #{author.name}

The code comparison demonstrates the syntax differences between Handlebars.js and Pug. Handlebars.js maintains an HTML-like structure with curly braces for variables and helpers, while Pug uses a more concise, indentation-based syntax with simplified tags and built-in language constructs.

Handlebars.js is generally easier for developers familiar with HTML, but Pug offers a more powerful and expressive templating language. The choice between the two often depends on project requirements, team preferences, and the complexity of the templates being created.

A powerful templating engine with inheritance, asynchronous control, and more (jinja2 inspired)

Pros of Nunjucks

  • Syntax closer to HTML, making it easier for designers and those familiar with HTML
  • More powerful and flexible inheritance system
  • Better integration with existing JavaScript code and ecosystem

Cons of Nunjucks

  • Slightly more verbose syntax compared to Pug's concise style
  • Less opinionated, which may lead to inconsistencies in large projects
  • Slower compilation times for complex templates

Code Comparison

Nunjucks:

<ul>
  {% for item in items %}
    <li>{{ item.name }}</li>
  {% endfor %}
</ul>

Pug:

ul
  each item in items
    li= item.name

Both Nunjucks and Pug are popular templating engines for JavaScript. Nunjucks, developed by Mozilla, offers a syntax closer to traditional HTML, making it more accessible for designers and those familiar with HTML. It also provides a more powerful inheritance system and better integration with existing JavaScript code.

On the other hand, Pug (formerly Jade) offers a more concise and indentation-based syntax, which can lead to cleaner and more readable code for some developers. However, this syntax can be less intuitive for those used to HTML.

The code comparison shows how both engines handle a simple loop to generate a list. Nunjucks uses a syntax closer to traditional templating languages, while Pug's syntax is more minimalistic and relies on indentation.

Ultimately, the choice between Nunjucks and Pug depends on personal preference, team familiarity, and project requirements.

11,005

Liquid markup language. Safe, customer facing template language for flexible web apps.

Pros of Liquid

  • More widely adopted in e-commerce platforms, especially Shopify
  • Easier learning curve for designers and non-programmers
  • Better documentation and community support

Cons of Liquid

  • Less expressive and feature-rich compared to Pug
  • Limited control structures and programming capabilities
  • Slower rendering performance for complex templates

Code Comparison

Pug:

doctype html
html(lang="en")
  head
    title= pageTitle
  body
    h1= heading

Liquid:

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>{{ page_title }}</title>
  </head>
  <body>
    <h1>{{ heading }}</h1>
  </body>
</html>

Key Differences

  • Syntax: Pug uses indentation and minimal syntax, while Liquid uses HTML-like tags
  • Variables: Pug uses = for variable output, Liquid uses {{ }}
  • Logic: Pug allows inline JavaScript, Liquid has its own simplified logic syntax
  • Extensibility: Pug is more extensible with mixins and includes, Liquid has limited extensibility
  • Performance: Pug generally offers better performance for complex templates

Use Cases

  • Pug: Ideal for Node.js projects, complex web applications, and developers who prefer concise syntax
  • Liquid: Best suited for e-commerce platforms, especially Shopify, and projects with non-technical contributors

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Pug

Full documentation is at pugjs.org

Pug is a high-performance template engine heavily influenced by Haml and implemented with JavaScript for Node.js and browsers. For bug reports, feature requests and questions, open an issue. For discussion join the chat room.

You can test drive Pug online here.

Professionally supported pug is now available

Build Status Rolling Versions NPM version Join Gitter Chat

Packages

Package NameVersion
pugNPM version
pug-attrsNPM version
pug-code-genNPM version
pug-errorNPM version
pug-filtersNPM version
pug-lexerNPM version
pug-linkerNPM version
pug-loadNPM version
pug-parserNPM version
pug-runtimeNPM version
pug-strip-commentsNPM version
pug-walkNPM version

Rename from "Jade"

This project was formerly known as "Jade". However, it was revealed to us that "Jade" is a registered trademark; as a result, a rename was needed. After some discussion among the maintainers, "Pug" was chosen as the new name for this project. As of version 2, "pug" is the official package name.

If your package or app currently uses jade, don't worry: we have secured permissions to continue to occupy that package name, although all new versions will be released under pug.

Before the renaming, work had already begun on “Jade 2.0.0”. Therefore, the rename to Pug coincided with the major version bump. As a result, upgrading from Jade to Pug will be the same process as upgrading any other package with a major version bump.

The syntax of Pug has several differences, deprecations, and removals compared to its predecessor. These differences are documented in #2305.

The website and documentation for Pug are still being updated. But if you are new to Pug, you should get started with the new syntax and install the Pug package from npm.

Installation

Package

To use Pug in your own JavaScript projects:

$ npm install pug

Command Line

After installing the latest version of Node.js, install with:

$ npm install pug-cli -g

and run with

$ pug --help

Syntax

Pug is a clean, whitespace sensitive syntax for writing HTML. Here is a simple example:

doctype html
html(lang="en")
  head
    title= pageTitle
    script(type='text/javascript').
      if (foo) bar(1 + 5);
  body
    h1 Pug - node template engine
    #container.col
      if youAreUsingPug
        p You are amazing
      else
        p Get on it!
      p.
        Pug is a terse and simple templating language with a
        strong focus on performance and powerful features.

Pug transforms the above to:

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Pug</title>
    <script type="text/javascript">
      if (foo) bar(1 + 5);
    </script>
  </head>
  <body>
    <h1>Pug - node template engine</h1>
    <div id="container" class="col">
      <p>You are amazing</p>
      <p>
        Pug is a terse and simple templating language with a strong focus on
        performance and powerful features.
      </p>
    </div>
  </body>
</html>

API

For full API, see pugjs.org/api/reference.html

var pug = require('pug');

// compile
var fn = pug.compile('string of pug', options);
var html = fn(locals);

// render
var html = pug.render('string of pug', merge(options, locals));

// renderFile
var html = pug.renderFile('filename.pug', merge(options, locals));

Options

  • filename Used in exceptions, and required when using includes
  • compileDebug When false no debug instrumentation is compiled
  • pretty Add pretty-indentation whitespace to output (false by default)

Browser Support

The latest version of pug can be downloaded for the browser in standalone form. It only supports the very latest browsers, though, and is a large file. It is recommended that you pre-compile your pug templates to JavaScript.

To compile a template for use on the client using the command line, do:

$ pug --client --no-debug filename.pug

which will produce filename.js containing the compiled template.

Tutorials

Implementations in other languages

Ports in other languages

Ports to other languages, with very close syntax:

Equivalents in other languages

Templates engines for other languages with a different syntax, but a similar philosophy:

Framework implementations/adapters

Embedded view engines for frameworks:

CMS plugins

Additional Resources

Backers

Support us with a monthly donation and help us continue our activities. [Become a backer]

Sponsors

Become a sponsor and get your logo on our README on GitHub with a link to your site. [Become a sponsor]

License

MIT

NPM DownloadsLast 30 Days