您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

4 年前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # pupa [![Build Status](https://travis-ci.org/sindresorhus/pupa.svg?branch=master)](https://travis-ci.org/sindresorhus/pupa)
  2. > Simple micro templating
  3. Useful when all you need is to fill in some placeholders.
  4. ## Install
  5. ```
  6. $ npm install pupa
  7. ```
  8. ## Usage
  9. ```js
  10. const pupa = require('pupa');
  11. pupa('The mobile number of {name} is {phone.mobile}', {
  12. name: 'Sindre',
  13. phone: {
  14. mobile: '609 24 363'
  15. }
  16. });
  17. //=> 'The mobile number of Sindre is 609 24 363'
  18. pupa('I like {0} and {1}', ['🦄', '🐮']);
  19. //=> 'I like 🦄 and 🐮'
  20. // Double braces encodes the HTML entities to avoid code injection
  21. pupa('I like {{0}} and {{1}}', ['<br>🦄</br>', '<i>🐮</i>']);
  22. //=> 'I like &lt;br&gt;🦄&lt;/br&gt; and &lt;i&gt;🐮&lt;/i&gt;'
  23. ```
  24. ## API
  25. ### pupa(template, data)
  26. #### template
  27. Type: `string`
  28. Text with placeholders for `data` properties.
  29. #### data
  30. Type: `object | unknown[]`
  31. Data to interpolate into `template`.
  32. ## FAQ
  33. ### What about template literals?
  34. Template literals expand on creation. This module expands the template on execution, which can be useful if either or both template and data are lazily created or user-supplied.
  35. ## Related
  36. - [pupa-cli](https://github.com/sindresorhus/pupa-cli) - CLI for this module