question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allowing filters to use dynamic variables in Pug

See original GitHub issue

Enter your suggestions in details:

Pug has a filter system that is currently hooked up to Lume’s filters, that other template engines like Nunjucks also use. However, Pug limits filter inputs to plain text only, which is incredibly inconvenient for Lume users.

For example, the date filter added by the Date plugin can be used in Pug like this:

:date(pattern="HUMAN_DATETIME") 2022-11-22 7:00:30Z

However, this does not work with Pug variables (so something like :date()= variable does not work), and that is by design:

Warning Filters are rendered at compile time. This makes them fast, but it also means that they cannot support dynamic content or options.

By default, compilation in the browser does not have access to JSTransformer-based filters, unless the JSTransformer modules are explicitly packed and made available through a CommonJS platform (such as Browserify or Webpack). In fact, the page you are reading right now uses Browserify to make the filters available in the browser.

Templates pre-compiled on the server do not have this limitation.

As Lume precompiles templates as an SSG, I think we should lift this limitation and bring Pug in line with other template engines like Nunjucks.

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Comments:10 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
pluiedevcommented, Nov 30, 2022

Nice! Is it possible to have both? For example filters.md(value) and :md?

It’s possible yeah, you just have to inject filters into the page data before rendering, which makes them available to the template as a global variable. Gonna open a PR that does exactly that very soon 👍

0reactions
oscaroterocommented, Nov 29, 2022

Nice! Is it possible to have both? For example filters.md(value) and :md?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Dynamic/Runtime Markdown/Filter? · Issue #2603 · pugjs/pug
I'm not sure what is blocking filters from being dynamic and able to use data at runtime for server side rendering but is...
Read more >
Interpolation
Pug provides operators for a variety of your different interpolative needs. String Interpolation, Escaped ¶. Consider the placement of the following template's ...
Read more >
How to bind value inside a Pug.js filter - javascript
It looks like Pug filters are run at compile time, and don't allow for variable/dynamic content. See this GitHub issue for more info....
Read more >
pugjs/pug
Hi guys, I have a question about mixins. Is it possible to put my block content in a plain text tag? What I...
Read more >
Pug in 5 minutes
Need to interpolate a variable? There are two ways. You could use Pugs interpolation operator #{} . But, if you're using inline code, ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found