Rendering a portion of a PUG template without refreshing page - html

I have a pug template index.pug for my project. I have also made navbar.pug, footer.pug, sidenav.pug. These files are included in the index.pug.
For server side I am using node.js, express.js and socket.io. Now I want to update only the navbar.pug file according to server response. But when I tried to do that I found some solution. But all of them refresh the full index page.
Is there any method that can help to update an specific portion of the webpage(in PUG) without refreshing the whole page?
Thanks in advance...

No, pug is a server-side only HTML processor. As such there is no way to do what you describe here - to get pug to process more means another round trip to the server to re-render the page.
Although technically you could do this with plain JavaScript, you should look into a client-side library like jquery, React, Angular, or Vue to do what you want. A lot of us use pug in combination with those toolsets to build modern web apps, but with those frameworks pug becomes a quicker way to write HTML and is no longer a server-side pre-processor.

Related

Reuse html in different pages

This is my website, https://unrealcousinzzz.com/.
I made it in HTML, CSS, and JavaScript. I cannot use PHP or anything like that right now because I am hosting it in AWS S3, because lightsail is too expensive for me. On each page in my website, I use the same HTML code. For example, every page has code for my navbar, and code google AdSense.
If I want to change these, I would have to do it on every page right now. Is there a way to make a file that other HTML pages will read, and use that where it is placed in that page?
HTML itself doesn't have any sort of templating, but there are alternatives.
Use something on your dev machine to compile your HTML for you. I use a Node.js script and a JavaScript template engine for this. Basically, I write out my templates, run the script which generates the HTML, and then I upload it to the S3 bucket.
It doesn't have to be done with Node. You could use PHP with this same method. Or, a Bash script for that matter.
Ok, so for me google tag manager is good. I can add custom html, and it goes onto all of my pages.

Server side rendering with EJS

I think I have a misunderstanding of server side rendering with ejs.
Per my understanding, when we create an ejs template and do
res.render('home.ejs')
The entire page is created in the server side. The page will contain all of the images and libraries pre loaded. This is then sent to the browser where the DOM event handler javascript code is run
Is this understanding correct?
What is ejs: Point we should focus to understand ejs: EJS makes easy to write HTML and just that, nothing more, nothing less.
How it works:In general we can not write plain javascript inside html but through ejs we can do that. Previously we have to organize javascript code but now we don't need to be panic about it. Just write plain javascript inside html.
Hence you said 'The entire page is created in the server side' is actually a wrong statement. Thank you so much.

Best way to render React Components inside HTML templates?

I would like to to use React with Django non Single Page App way - Django will take care of routing, and rendering HTML templates and serving data.
React should be used just on some specific components inside HTML page for eg. (dropdowns, autocomplete, modals), ideally being able to just drop for eg. div element with a class inside HTML and pass props for React component.
What’s the best - maintainable, scalable solution to go about this ?
See django-jsx package and also server side rendering paper. I'm not a frontend specialist but when I've faced such problem, my friends offered me to google isomorphic app with django and react.
I found this add React to an HTML page in one minute page/example to be absolutely painless in rendering a React component inside of a Django template!
This probably isn't what you want to do, given that React has a pretty robust ecosystem around it for building performant single page applications (SPA's). You should be able to decouple your React site from your Django app. Then you'd be able to throw up your React app on a performant CDN, rather than having your Django server hosting every visitor.
But if you insist, the most straightforward way to proceed would probably be to create an index.js & index.html at each Django route. In other words, make a separate "React app" at each route, which Django will serve as users go to each endpoint. I've seen this done before. It's laggy and inefficient (relative to an SPA), but can be done.
If you really intend to go so far as to write raw HTML/CSS/JS and just use React for bits and pieces in between, you'll probably be looking to invoke ReactDOM.render using a variety of second arguments (called container) rather than the standard React-y way of doing a single ReactDOM.render(<App />, document.getElementById('root')); for the whole app to inject into a barebones HTML template.
I notice you tagged your question with server side rendering. If that is a hard requirement for you for some reason, I'd look into using next.js, a Node framework optimized for exactly that.

Can I still use `.cshtml` files in a hybrid mobile project?

All the resources use only HTML, but stuff like Cordova and Ionic work on the client, with HTML pages (and JavaScript and CSS of course), and once MVC has served a page, that page is HTML, so the hybrid tooling should be able to use it.
The only problem is that once it's served, it lacks the .html extension, but maybe I can figure a way to add this.
The reason I'd like to stick to .cshtml for my layout is that I can scaffold views from my sever-side view models, and this adds quite a lot of value for me.
If the question is: can you have .cshtml files on your mobile device, the answer is no. .cshtml file is processed on server by the Razor view machine, so plain HTML can be generated. Obviously you don't have MVC, Razor and such on your hybrid ionic app.
You can however work around it by taking your angular templates to be generated on your server by MVC. So, when you setup your templateUrl somewhere, instead of taking the relative path to your local template .html file, you can point to the controller on your server, giving the full path, with http:// and so. The controller will then generate your template for you and return as plain HTML.
Bear in mind, that angular will cache this, so it will be loaded only once.

Call python script from html page

I have to make a html page with css and javascript that I have to enter a url in a form. With this url, I have to get some information from the html of the page with a Python 3.2 Script.
I start learning Python some days ago and I have some question:
I need CherryPy/Django to do that? (I'm asking because I executed a script to get the entire html without using CherryPy/Django and it works - no interaction with browser)
CherryPy examples have the html built in the python code. I must write the html in the python script or can I have an html page that call the script with Ajax (or anything else)?
If I can use Ajax, is XmlHttpRequest a good choice?
Thank you! :D
No, you don't need a web framework, but in general it's a good idea. Django seems like brutal overkill for this. CherryPy or Pyramid or some micro framework seems better.
You can have an HTML page that calls the CherryPy server, but since this page obviously is a part of the system/service you are building, serving it from the server makes more sense.
Sure, why not.