Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

docpad-plugin-partials

docpad175MIT2.14.0

Adds support for Partials (re-usable templates) to DocPad

docpad, docpad-plugin, esnext, node, partial, partials, template, templates, templating

readme

Partials Plugin for DocPad

Travis CI Build Status NPM version NPM downloads Dependency Status Dev Dependency Status
GitHub Sponsors donate button Patreon donate button Flattr donate button Liberapay donate button Buy Me A Coffee donate button Open Collective donate button crypto donate button PayPal donate button Wishlist browse button

This plugin provides DocPad with Partials. Partials are documents which can be inserted into other documents, and are also passed by the docpad rendering engine.

Usage

Create the src/partials directory, and place any partials you want to use in there.

Then call the new partial(filename, objs...) template helper to include the partial. The object arguments are optional, and can be used to send custom data to the partial's template data. Setting the first object argument to false will not send over the template data by default.

Examples

Lets say we have the partial src/partials/hello.html.eco that includes:

Hello <%=@name or 'World'%> Welcome to <%= @site?.name or 'My Site' %>

And a docpad configuration file file that includes:

templateData:
    site:
        name: "Ben's Awesome Site"

We could then render via a src/documents/index.html.eco document in these different ways:

<!-- Include the rendered contents of `src/partials/my-partial` file -->
<!-- and send over the template data -->
<%- @partial('hello') %>
<!-- gives us:
Hello World
Welcome to Ben's Awesome Site
-->

<!-- Include the rendered contents of `src/partials/my-partial` file -->
<!-- and send over the template data -->
<!-- and send over our own custom template data -->
<%- @partial('hello', {name:'Ben'}) %>
<!-- gives us:
Hello Ben
Welcome to Ben's Awesome Site
-->

<!-- Include the rendered contents of `src/partials/my-partial` file -->
<!-- and DO NOT send over the template data -->
<%- @partial('hello', false) %>
<!-- gives us:
Hello World
Welcome to My Site
-->

<!-- Include the rendered contents of `src/partials/my-partial` file -->
<!-- and DO NOT send over the template data -->
<!-- and send over ONLY our own custom template data -->
<%- @partial('hello', false, {name:'Ben'}) %>
<!-- gives us:
Hello Ben
Welcome to My Site
-->

<!-- Include the rendered contents of `src/partials/my-partial` file -->
<!-- and DO NOT send over the template data -->
<!-- and send over our own custom template data with the template data site property -->
<%- @partial('hello', false, {site:{name:@site.name}}, {name:'Ben'}) %>
<!-- gives us:
Hello Ben
Welcome to Ben's Awesome Site
-->

Notes

To increase performance it is recommended you only include the exact template data variables that you need - this is because sending over all the template data can be a costly process as we much destroy all references (do a deep clone) to avoid reference conflicts and over-writes between each render - so sending over as little / as specific data as possible means less reference destroying which means faster processing.

If your partial only needs to be rendered once per (re)generation then you can specify cacheable: true in the partial's meta data, doing so greatly improves performance.

Partials actually render asynchronously, when you call <%- @partial('hello') %> you'll actually get back something a temporary placeholder like [partial:0.1290219301293] while your template is rendering, then once your template has rendered, and once all the partials have rendered, we will then go through and replace these placeholder values with the correct content. We must do this as template rendering is a synchronous process whereas document rendering is an asynchronous process. More info here.

Compatibility

  • Versions 2.8.0 and above DO send the template data by default. You can turn this off by using false as the first object argument or by setting performanceFirst: true in your plugin's configuration options.

  • Versions below 2.8.0 DO NOT send the template data by default. You must add it by using @ or this as the first object argument like so: <%- @partial('my-partial', @) %>

Install

Install this DocPad plugin by entering docpad install partials into your terminal.

History

Discover the release history by heading on over to the HISTORY.md file.

Contribute

Discover how you can contribute by heading on over to the CONTRIBUTING.md file.

Backers

Maintainers

These amazing people are maintaining this project:

Sponsors

No sponsors yet! Will you be the first?

GitHub Sponsors donate button Patreon donate button Flattr donate button Liberapay donate button Buy Me A Coffee donate button Open Collective donate button crypto donate button PayPal donate button Wishlist browse button

Contributors

These amazing people have contributed code to this project:

Discover how you can contribute by heading on over to the CONTRIBUTING.md file.

License

Unless stated otherwise all works are:

and licensed under:

changelog

History

v2.14.0 2020 August 6

  • Updated dependencies, base files, and editions using boundation
  • Minimum required node version changed from node: >=0.8 to node: >=10 to keep up with mandatory ecosystem changes

v2.13.0 2018 July 11

  • Updated base files using boundation
  • Compiled with CoffeeScript v2

v2.12.0 2017 November 6

v2.11.0 2017 April 17

  • Updated dependencies

v2.10.0 2017 April 17

  • Updated base files

v2.9.2 2015 March 5

  • Updated dependencies

v2.9.1 2014 May 7

  • Minor improvements to make debugging easier

v2.9.0 2014 February 26

v2.8.1 2013 November 6

  • Fixed error reporting when a partial breaks (regression since always???)
  • Partials now render inside the renderDocument event, rather than any random time
  • Updated dependencies

v2.8.0 2013 August 23

  • Template data is now sent with partial calls by default, and can be turned off by setting the first object you pass the the partial as false, e.g. <%- @partial('hello', false, blah) %> - This has been documented quite thoroughly now in the README - This backwards compatibility break is due to the incredibly high amount of "user error" issues not sending the template data by default causes. In hindsight, it has become apparent to us that sacrificing docpad performance time for developer performance time and developer happiness is a far better trade. Caring about performance should come secondary to happiness. Not before it. - For those whom do care about performance, you should update all your existing partial calls to make use of the new first false object practice, or if you are too lazy set performanceFirst: true in the plugin's configuration options.

v2.7.2 2013 July 30

  • You can now customise the collection name via the collectioName option
  • Updated some older docpad api conventions for their newer equilevants

v2.7.1 2013 July 25

  • Updated for DocPad v6.46.2

v2.7.0 2013 July 23

  • Updated for DocPad v6.46

v2.6.3 2013 May 28

  • Fixed Cannot call method 'match' of null

v2.6.2 2013 April 17

  • Fixed cacheable partials (broke in v2.6.0)

v2.6.1 2013 April 6

v2.6.0 2013 April 6

  • Partials now begin rendering as soon as we receive them
  • Dependency upgrades

v2.5.2 2013 April 5

  • Dependency upgrades

v2.5.1 2013 April 1

  • Dependency upgrades

v2.5.0 2013 March 7

  • DocPad v6.24.0 support
  • Repackaged
  • Dependency upgrades - bal-util from 1.15.x to ~1.16.8 - coffee-script from 1.4.x to ~1.4.0

v2.4.0 2012 December 15

  • Removed the possibility of name collisions when storing partial info
  • You can now cache a partial by setting cacheable: true in the partial's meta data - Doing this will only render it once per (re)generation. Cache is cleared after each (re)generation.

v2.3.0 2012 November 23

  • New way of doing partials - Should be faster - Should support watching better - Supports fuzzy matching
  • Thanks to ashnur for issue #283

v2.2.1 2012 November 6

  • Fixed memory leak (didn't clean up after generations, so regenerations would take longer and longer) - Fixes #335

v2.2.0 2012 October 7

  • @partial signature now changed from @partial(name,data) to @partial(name,objs...) - If multiple objects are specified, they will be shallow-y merged into an empty object - This allows you to do things like @partial('blah',@,{blah:'blah'}) to extend the partial blah with the template data and some custom partial data

v2.1.2 2012 August 10

  • Re-added markdown files to npm distribution as they are required for the npm website

v2.1.1 2012 July 8

  • Moved the insertion of the partial template helper into the new extendTemplateData event

v2.1.0 2012 July 8

  • Fixed path exists warning
  • Updated for DocPad 6.1

v1.0.1 2012 May 5

  • Updated for DocPad v5.2
  • Partials are now created with createDocument instead of createPartial which has been removed
  • Partials which aren't found will now throw a warning

v1.0.0 2012 April 14

  • Updated for DocPad v5.0
  • Fixed multiple partials on the same document bug

v0.1.0 2012 March 31