Express / Pug - How to strip comments? - node.js

I am using Express with Pug as a view engine.
Pug renders comments in the HTML. For example:
div
// comment
p Hello
Would be rendered as:
<div>
<!--comment-->
<p>Hello</p>
</div>
How would I disable this functionality and instead strip them from the template so that they aren't visible in the rendered HTML?
I can see there is this pug-strip-comments package, but there is no documentation whatsoever for Express.
I want to avoid using something like a middleware to strip comments.
Thank you.

You can strip pug comments by using the following code. Just add //- instead of just // and it will strip it from from the rendered HTML.
//- will NOT output within markup
p foo
p bar
// will output within markup
p foo
p bar
You can take a look at the comments documentation for pug here:
https://pugjs.org/language/comments.html

Related

How to display html from a mongodb database in nodejs

I'm stuck with trying to figure out how to display html tags inside of a mongodb table. Everytime I view the data through the browser, it shows the text of the html. Example:
I have this in a mongodb database table:
Test
It should display as this when viewed on a webpage: Test
But instead, it displays Test
Your Issue
I haven't used EJS before but I'm assuming its an issue with how you're inserting the HTML strings from the database into your EJS templates.
In most templating engines, simply inserting a string containing HTML markup will result in the engine escaping the HTML instead of rendering it.
EXAMPLE
INPUT > <div class='box'> <p class='item'>Hello World</p></div>
OUTPUT > <div class='box'> <p class='item'>Hello World</p></div>
This is normal and there are many reasons for it, one of which is security.
Solution
If it is, in fact, an issue with EJS escaping the HTML, you must instruct EJS to not escape it. I've never used EJS before but a quick google search for EJS escape html brought up this result How to escape HTML in node.js EJS view?
For future reference
While EJS will escape your HTML by default stack overflow does not. When you inserted <strong> in your question, stack overflow rendered it as HTML.
To get StackOverflow (and most other markdown readers) to render any HTML markup literally like I've done above, wrap the HTML in graves `.
Also, try to provide more information in your questions. The more information, the easier it is to deduce the likely source of the problem.

Delete Html entities or replace tag

when i display ckeditor text the html tags are also displayed. how can a i show only the text? i'm getting this:
<b>Paragraph</b>
Paragraph
I use Jade template and node.
It's possible to delete Tag before save in database ? or remove tag after ?
Thanks.
I found a solution.
Juste use !{value} instead of #{value} with Jade template to decode html entities.
value contains html entities.
use unescape syntax in the pug file.
p
!= 'This code is <strong>not</strong> escaped!'
or
- var riskyBusiness = "<em>This code is <strong>not</strong> escaped!</em>";
.quote
p Joel: !{riskyBusiness}

Replace variables in jade without rendering to html

I am using jade as a kind of markup language for a thermal printer. That means on the website i render a print preview to html, and i am sending the jade(with custom tags) directly to the printer which interprets the tags for different text styles.
It works pretty well but now i would like to use also locals in that template but render only the locals not to html.
That means
p #{name}
should become
p John Doe
instead of
<p>John Doe</p>
Is there a possibility to do this with some function in the jade package ? Or do i need to write that from scratch. I also want to use jade "each" loop.
You can prefix it with a pipe symbol:
| p #{name}

how to render multiple jade templates

I am trying to understand how JADE works when there are several templates.
I worked by this tutorial:
http://www.franz-enzenhofer.com/jade
But, I got this:
$ curl http://localhost:3000
<h1> Franz Enzenhofer</h1>
It seems that the command "res.render('index.jade',..." only took the index.jade template, but didn't insert it into the layout.jade template as should happen...
I assume you are using partials. They were removed with express v3. See the View System Changes part for more information.
From express v3 on you should use blocks. For example:
my-template.jade:
extends my-layout
block head
script(src="myfile.js")
block content
h1 My page
my-layout.jade
doctype 5
html
head
title My title
block head
body
#content
block content

Comments in Jade/pug

I tried making comments in Jade/pug, but the comments render as text in the HTML. This is my code:
doctype html
html(lang='en')
body
/ This should be a comment
What am I doing something stupid?
As written in the comment documentation, you can either use // which will translate to a HTML comment or //- which won't be visible in the outputted HTML code.
In jade we use //- for comment.
If you are trying to comment a block, make sure it should be indented properly like in below example-
doctype html
html(lang='en')
body
//-
This should be a comment
Indent correctly for block content

Resources