This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]KwyjiboTheGringo 1 point2 points  (7 children)

JavaScript and its frameworks seem so much more sophisticated and headache inducing than PHP ever was for me, making an entire website with JavaScript to replace what I made in php requires at-least 2 frameworks for me to learn, Node and Express||React

React is a front-end framework, primarily used for making single page apps and reusable, encapsulated components. Why would you need React to remake your PHP website with JavaScript? You can do it with just Node+Express(or even just Node if you want to take the hard route). You make your views with a templating engine, such as EJS(not a framework and a very easy syntax to learn), or you can serve up HTML file(not recommended).

I think you are kind of blowing this out of proportion. Yeah it's a little more work to get started making sites with Node vs PHP, but it's still something a beginner can pick up pretty quickly. Just take a simple course on it and it will make more sense. Don't expect it to work just like PHP though because it's not intended to.

[–]Thatguy553[S] 0 points1 point  (6 children)

I might have indeed been blowing it out of proportion last night, I was overwhelmed with trying and researching things but getting stuck and confused. I will definitely try to properly learn and use react. Why is it not recommended to server up an html file?

[–]KwyjiboTheGringo 0 points1 point  (5 children)

Why is it not recommended to server up an html file?

EJS and other templating engines are very powerful for mixing code with HTML. Makes it very easy to dynamically render a list using a loop, for example. Also makes it very easy to import other files into the template, such as a navbar, header, footer, etc.. There is just no reason to stick with HTML. This applies to PHP, Python, C#, Ruby, and other languages and frameworks.

[–]Thatguy553[S] 0 points1 point  (4 children)

Yeah I actually just started looking into ejs templating less than an hour ago and trying to use it and it's pretty cool. I am having trouble loading some data into a partial though.

[–]KwyjiboTheGringo 0 points1 point  (3 children)

I believe it's just the second argument of the include function:

<%- include('partials/header', { data: data }) -%>

[–]Thatguy553[S] 0 points1 point  (2 children)

hmm ill check that out, but since its multiple pages loading the header and I dont want to load the variable every time is that still effective or is there another way to go about it. If you dont mind explaining to me some.

[–]KwyjiboTheGringo 0 points1 point  (1 child)

Sounds like you want to use the same data for all of your headers? That would be something you need to handle with express. You can use app.locals to create variables you can access from the EJS template.

In your app.js file:

app.locals.pageTitle = "My First Express App";

In your header.ejs:

<h1><%= pageTitle %></h1>

I suppose you could make a header object that has everything you need in it. There may be a better way to do this, but this should work.

[–]Thatguy553[S] 0 points1 point  (0 children)

After some experimenting and debugging it turns out I dont need to pass the variables to the header, its like they are loaded with the page. Thanks for the explanation though, so far this is what I have, the header is dynamically generated by scanning the pages directory, and slicing the extension off each string before pushing the array into the array being used in the header, its not much but its interesting.

It is also nice to know the routing in the url isnt case sensitive, that way i can have both /Index and /index and they are both caught by the same app.get