{"library":"mustache","title":"Mustache.js - Logic-less Templates","description":"Mustache.js is a zero-dependency, logic-less templating engine for JavaScript, faithfully implementing the Mustache template system. Its current stable version is 4.2.0, with releases primarily focusing on bug fixes and minor enhancements. The library enables developers to utilize tag-based templates for a wide array of outputs, including HTML, configuration files, and source code, by expanding tags with values provided from a JavaScript object or hash. A key differentiator is its strict adherence to a 'logic-less' philosophy, meaning it explicitly avoids traditional control flow statements (like `if/else` or `for` loops), rendering content solely based on the presence or absence of data and iterating over arrays without explicit loops. Mustache.js offers broad compatibility, supporting CommonJS, AMD, and ECMAScript Modules, making it suitable for modern web browsers, Node.js server-side environments, and even as a command-line tool.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install mustache"],"cli":{"name":"mustache","version":null}},"imports":["import Mustache from 'mustache';","const Mustache = require('mustache');","import Mustache from 'mustache';\nconst output = Mustache.render('{{foo}}', { foo: 'bar' });","<script src=\"https://unpkg.com/mustache@latest\"></script>\n<script>\n  const output = Mustache.render('Hello {{name}}', { name: 'World' });\n</script>"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import Mustache from 'mustache';\nimport { readFileSync } from 'fs';\n\n// Basic usage\nconst view = {\n  title: 'My Title',\n  calc: () => 2 + 2,\n  name: 'World'\n};\nconst template = '<h1>{{title}}</h1><p>The answer is {{calc}}.</p><p>Hello, {{name}}!</p>';\nconst output = Mustache.render(template, view);\nconsole.log('Basic Render:\\n', output);\n\n// Using a template file (e.g., 'template.mustache')\n// Create a file named 'template.mustache' with content: 'User: {{username}}\\nEmail: {{email}}'\ntry {\n  const fileTemplate = readFileSync('template.mustache', 'utf8');\n  const fileView = { username: 'john.doe', email: 'john@example.com' };\n  const fileOutput = Mustache.render(fileTemplate, fileView);\n  console.log('\\nFile Render:\\n', fileOutput);\n} catch (error) {\n  console.warn('\\nCould not read template.mustache. Skipping file example.', error.message);\n  console.log('To run this part, create a file named `template.mustache` in the same directory with content:');\n  console.log(`User: {{username}}\\nEmail: {{email}}`);\n}\n\n// Example with partials (assuming a partial named 'greet' is available)\n// If you have a partials object: { greet: 'Hello {{name}}!' }\n// You could then use {{> greet}} in your main template.\nconst partialsView = { user: 'Alice' };\nconst mainTemplateWithPartial = 'Main template for {{user}}! {{> greet}}';\nconst partials = { greet: 'Welcome, {{user}}!' };\nconst partialOutput = Mustache.render(mainTemplateWithPartial, partialsView, partials);\nconsole.log('\\nRender with Partials:\\n', partialOutput);\n","lang":"typescript","description":"This quickstart demonstrates basic template rendering with a view object, rendering from a local file, and an example of using partials. It highlights the `Mustache.render` function.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}