-2

I'm thinking of building a website using mustache.js to render the website and having PHP as the backend. The PHP will then mainly contain queries to the database and some SESSION checkings.

Previously I've used plain PHP to render the frontend but it gets way too messy.

An example of what I'm thinking:

HTML File

<html>
    <head>
        <script>
           $.ajax{
               url: "path/to/file.php",
               ...
               success: function(response){
                   var template = $('#template').html();
                   var rendered = Mustache.render(template, response)
                   $('#template').html(rendered);
               }
           }
        </script>
    </head>
    <body>
        <p id="template">Your favourite website is {{website}}</p>
    </body>
</html>

PHP File

<?php
    function getWebsite($userid){
        $mysqli = (...);
        $stmt = $mysqli -> prepare("SELECT name FROM websites WHERE userId = ?");
        $stmt -> bind_param("i", $userId);
        $stmt -> execute();
        ...
        ...
        echo json_encode($results);
    }
?>

Is this a good structure or will it still get messy? Will it be slower?

2
  • I use mustache.js in one of my sites for building certain parts of the interface. Works fine. May 7, 2014 at 3:45
  • it's php, of course it's messy ;)
    – jwenting
    May 7, 2014 at 8:48

1 Answer 1

2

You really need to provide some more information for any advice to be suitable, but I'll try and answer in a general manner.

This does, in some ways, provide a rather neat separation of logic. However there's going to be some overhead in the way of HTTP requests - and potentially some pitfalls with regards to debugging.

The main issues that occur to me are:

HTTP Requests

For every page there will be an additional HTTP request, essentially one for the template (the .html file) and one for the content (the json payload).

JS Operations

This is going to introduce some fair overhead with regards to client side operations. For example, just to display a "page" you're going to need to make an AJAX request, parse the JSON response and associated template and then inject this in to the DOM. Whilst not the most heavy, arguably not essential either.

Accessibility

If, for whatever reason, Javascript isn't enabled - your viewers are stuck.

Maintenance

This may not seem like a big deal at the moment, but given time it could well become one.

Your real problem

However, from reading your post it appears that you're comfortable writing PHP and have used it inline previously. In that case, use a server side templating engine! There's even a port of Mustache, Mustache.php! Another one to consider could be Smarty. There are quite a few to choose from.

What you really seem to be expressing is the desire to use structured 'view's. A kin to what a lot of PHP MVC frameworks give you - depending on the complexity of your project using a framework may be a good way to go.

There's many frameworks to choose from, but I personally have experience with Yii, Laravel and CodeIgniter. CodeIgniter is quite possibly the simplest, but Laravel is my favourite. However, there are many options out there - and they all provide mechanisms for separating your views from the rest of the rest of the application, and as far as I know, they all include templating engines. They also make life a lot simpler with regards to database operations - and most other things you'll end up writing boilerplate for.

They can be a bit of a pain to get your head around at the beginning (suddenly you need to get used to how the framework wants you to do things) but actually, they do save time and help you manage complexity - it's a worthwhile learning curve to endure!

Not the answer you're looking for? Browse other questions tagged or ask your own question.