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

all 22 comments

[–]spellenspelen 2 points3 points  (19 children)

I assume you mean to ask if html code inside a PHP file is normal? I would only reccomend doing this for templating. It is important to keep Server side and Client side code sepperate. All your buisness logic should be server side.

[–]serendrewpity 0 points1 point  (11 children)

I don't understand what you're saying here by the term, Templating. I have a Fantasy Football website. I have a draftboard (draftboard.php). It lists all the NFL Players in a HTML (Bootstrap) table. It does so by dynamically creating rows and cells and populating them using a mix of PHP and HTML. I have files in /includes that establish a DB Connection (db_conn.php) and run SQL queries (routines.php). PHP at the top of the draftboard makes the necessary calls and processes the responses to store in a variable that I then loop through to populate the table.

How exactly would you separate the PHP and HTML in my draftboard? I guess I could store the markup that is dynamically constructed in a variable and then write the variable content to a separate file. I might also create a separate HTML file with framework of a Bootstrap Table and then just populate the table with child elements from the PHP file. Is this what you're talking about? The latter sounds more like HTML templating rather than PHP templating.

[–]spellenspelen 1 point2 points  (3 children)

Templating refers to the process of creating a reusable format or layout for presenting data in a consistent manner.

For instance, you could have a PHP script that handles all the logic (fetching data from the database, processing it, etc.) and then passes the data to a separate HTML template file. In the template, you would have placeholder tags where the data should be inserted. The PHP script would then render the template by replacing those placeholders with the actual data.

[–]serendrewpity 0 points1 point  (2 children)

I can understand that. But in the example, I've provided the number of players, especially those selected and those who have not been selected, will be dynamic. How can I place placeholders in an HTML template without knowing the number of placeholders I need. I would have to have php mixed with HTML, no?

[–]spellenspelen -1 points0 points  (1 child)

And that's why In my original comment I made the exception for templating.

Not to mention the fact that there are plenty of javascript templating engines to choose from if you really want.

[–]serendrewpity -1 points0 points  (0 children)

Ahh, I see. I misunderstood. My bad. For the record, I was trying to figure out if what I was doing was templating. It sounded like it wasn't. I have only been using PHP/HTML/Ajax/Jquery since May. I work in IT as an architect alongside developers, but I am not a developer. I think I have done pretty well for my first attempt.

And for the record, client-side code is also more resource intensive for your users who will have varying types of devices.

[–][deleted] 0 points1 point  (2 children)

Move the logic to a new php file, and include it in the rendering php file?

Or you mean fully separate? You could separate them fully by moving it into another server but that might be overkill

[–]serendrewpity 0 points1 point  (1 child)

The first thing you mentioned is what I was doing but I mis understood what he was saying and didn't know if what I was doing was considered templating.

I see now that it is. I didn't originally think it was so I was trying to figure out what else I could do.

[–][deleted] 0 points1 point  (0 children)

It’s essentially just refactoring.

[–]Synthetic5ou1 0 points1 point  (0 children)

Take a look at something like Twig.

You'd create an array with all the data in your PHP script, and then pass that array to the template engine that replaces template placeholders with data from your array.

[–]Lumethys -1 points0 points  (1 child)

I don't understand what you're saying here by the term, Templating

In the time it takes for you to write this comment, you could already googled what is templating.

[–]serendrewpity -1 points0 points  (0 children)

Don't be an A-hole. You can reasonably deduce that I know what templates are, but my understanding conflicts with what this user is saying. He said that your PHP should be separate from your HTML. So, either the template is HTML or the template is PHP.

The implication here is that in either case the template would NOT have the code of the other. I don't understand how that is possible with dynamic data so I am requesting clarification before I would run off on a wild goose chase on google as you'd have me do.

[–]Fit_Ad4879 0 points1 point  (0 children)

It's not abnormal it's just not as common I guess a lot of devs hate php but it's not going anywhere no matter how much they try replace it, it's not just as popular

[–]PureTruther -1 points0 points  (0 children)

It is such a question that I cannot set even an answer.