Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use HTML5 pattern attribute in Pug (Jade) template

With Pug (previously named Jade) templating engine, how can I use the pattern attribute of an input ?

When I use a pattern like:

input( type="tel", pattern="\d{7}" )

the rendered pattern is:

<input type="tel" pattern="d{7}">

I also tried with the unescaped attribute != but it still escapes the \ character.

Note: it works perfectly with pattern="[0-9]{7}".

like image 854
JP P. Avatar asked Oct 30 '14 14:10

JP P.


People also ask

How do you use special characters in Pug?

If you need to use special characters, use != instead of = .

How do I connect my Pug to HTML?

The general rendering process of Pug is simple. pug. compile() will compile the Pug source code into a JavaScript function that takes a data object (called “ locals ”) as an argument. Call that resultant function with your data, and voilà!, it will return a string of HTML rendered with your data.

How would you change this HTML page to Pug?

To convert your HTML files to Pug, you can use the html2pug npm package. The html2pug package currently only works in Node. js, so you need an API endpoint if you want to use it from a browser app. Below is an example of using html2pug in Node.

Is Pug better than HTML?

Notice that coding using the PUG framework is much easier and more readable than thr HTML codes. PUG makes use of indentation to distinguish between where a tag starts and ends. This makes the code much cleaner than HTML, where lack of indentation and a need for closing tags makes the code a little cluttered.


1 Answers

The problem here is that the \ character is used to escape Javascripts's own special characters.

You need to escape it so it will be rendered in the pattern, as explained here.

input( type="tel", pattern="\\d{7}" )

will render properly as:

<input type="tel" pattern="\d{7}">
like image 186
JP P. Avatar answered Sep 21 '22 02:09

JP P.