Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

By default, any file that PHP touches is usually suffixed with .php. It's universally understood by Apache / Nginx as the default for PHP files and most setups expect PHP files to end in this extension. In short, .php is the standard for everything PHP.

However, I'm wondering if perhaps view files should have a different extension to help differentiate them from other PHP files.

First, when it comes to views I have found that almost all MVC frameworks using a matching view file named after the controller or method. In addition, you generally also have a matching model named the same thing. This causes a problem with most IDE's and editors.

For example, you might have a "user" controller, a "user" view, and a "user" model. The results in having three files open called "user.php" which makes it a bother when you are moving around and clicking on the wrong tabs.

Second, separating views as a fundamentally different kind of PHP file (the presentation type) is another argument for changing the extension of the view files to something other than .php. Something that immediately tells your brain what type of content belongs in it.

Third, some applications expose parts (or all) of the PHP files in the webroot and it's directories. Rather than adding something like <?php if(!defined(ABC)) die('not allowed'); it would be nice to have an easy way to forbid access to views while allowing other files like the customary index.php.

Are there any other good/bad reasons to think it's a good idea to change the file extension of view files?

share|improve this question
    
Check out Joomla for the if defined stuff, all of their pages have it. Also, drupal adds tpl to php templates, Example: node.tpl.php –  Malfist Feb 8 '12 at 18:48

8 Answers 8

When you're talking about convention, there are a few bajillion different ways to do it. Ways I've seen:

index.tpl.php
index.phtml
templates/index.php
views/index.php

You can combine these methods to get some really amazing conventions!

views/templates/index.tpl.phtml

But if you ever do that you should feel bad about yourself.

The long and short of it - if you're using a framework, use their template convention. If you're doing it your own way, just be consistent, and, most importantly, if your template files are in web root and not named *.php make sure your webserver does not serve them as text!!!!!

share|improve this answer

To differentiate views from models and controllers, I would rather use directories:

+ /root
+    /controllers
         DefaultController.php
+    /models
+    /views
+        /Default
             Home.php ← Home view of DefaultController.
             About.php ← Another view.

As for your second argument, do your views contain PHP code? If yes, then they are not fundamentally different kind. They are just... different. That's why they are in a separate directory.

A counter-argument could be that in an IDE, you don't see the full path. If I open side by side /models/Home.php and /views/Default/Home.php, how would I know that the first Home.php is a model and the second Home.php is a view?

But still, even in this case, I would avoid changing extensions, but rather use prefixes/suffixes: HomeModel.php and HomeView.php are clear enough in an IDE. In fact, if you change extensions just for the IDE, another developer would have to reconfigure her IDE to accept and understand those extensions as PHP source code files.

share|improve this answer
    
Good points. One other thing you bring up is that if views contain PHP then they are not true, pure views. I however, was thinking about true views as in views that handle the entirety of constructing the visual display of the data without the controller, libraries, or models needing to know anything about templates or presentation. That differs from the other view that only HTML and template syntax belongs. –  Xeoncross Feb 8 '12 at 18:57

The naming convention should help, not hinder, clarity. The setup you describe clearly hinders clarity and therefore is not helpful.

The file extension should always represent what is in the file. If it's php, it should be ".php". That said, I like the Zend MVC standard of using ".phtml" for the view files. Which, in essence, says to me, this is primarily an html file that you can also expect to contain some php.

Going back to Zend, the controller is UserController.php, the model is User.php (or Application_User.php, depending on how you roll), and the view for the index action is index.phtml, in the scripts/user directory.

If the framework leaves you so confused you want to change standards, I'd look for another framework.

share|improve this answer

MVC pattern already divides/segregates your project's code to a folder structure to reflect Model, View and Controller. In my opinion that should be enough to identify file types.

Your example with User controller may vary across frameworks. I mostly worked with CodeIgniter and it sure does not force file naming convention as you presented. You free to call your Views the name you want, put them in sub directories named after your controller etc. (like it's done in ASP.NET MVC)

share|improve this answer

It does not matter what extension you assign to your files. But, most probably, you would like to distinct view files from other files you have in the application. Some kinds of distinction may be more suitable for you, just choose one and stick to it.

share|improve this answer

Older versions of Apache & PHP supported additional file extensions, based on the installed PHP version:

customers.php3
sales.php3
products.php4
purchases.php4

I think, still can be done, in the Apache or PHP configuration, but, it think, that will be complicated, and bring more problems, that advantages.

But, sometimes, its good to differentiate among several related source-code files.

One trick its using hyphens ("minus") or dots inside the filename, not the file extension, or file suffix.

Example 1:

customers-model.php
customers-view.php
customers-controller.php

Example 2:

customers.model.php
customers.view.php
customers.controller.php

Example 3 (Prefixes or suffixes):

CustomersModel.php
CustomersView.php
CustomersController.php

This way, you still support the file extension, and implement a "subextension", for your source code.

share|improve this answer
1  
I think, still can be done, in the Apache or PHP configuration, but, it think, that will be complicated, and bring more problems, that advantages. It only takes an extremely simple AddType directive in httpd.conf, possibly the simplest Apache directive there is. What problems? –  Yannis Rizos Feb 9 '12 at 2:00
    
@Yannis Rizos. Right, Its also, seems easy for me, to modify the httpd.conf file and change all includes to custom extension. But, depends on the original post can do that. –  umlcat Feb 9 '12 at 23:59

If you are concerned about preventing access to view files in your webroot, then the solution is to use .htaccess to redirect requests to those files over to your application. I prefer a bootstrap index file that handles all requests.

share|improve this answer

It really depends on the content of your view files.

A view could look like this :

<?php

class Home_View {

    public function render($parameters) {
        [...]
    }

    [...]
}

?>

It could also look like this :

<html>
    <head>
        <?php echo $parameters['styles'] ?>
    <title><?php echo $parameters['title'] ?></title>
    </head>
    <body>
        <?php echo $parameters['content'] ?>
    </body>
</html>

Both of the above cases, it makes sense to use the .php extension, because your view is basicly just plain PHP.

A view doesn't have to be plain PHP, though. A view could one of many other filetypes that can be understood by your controller.

If your view is a valid Smarty template, consider .tpl.

If your view is a valid Twig template, consider .twig.

If your view is a valid Latte template, consider .latte.

If your view is a valid Mustache template, consider .mustache.

If your view is valid HTML, consider .phtml or .html.

If your view is a valid Atom file, consider .atom.

If your view is a valid JSON file, consider .json.

If your view is a valid XML file, consider .xml.

You could of course always used your own custom extension, but I wouldn't recommend it because you'd not be able to take advantage of the autoformatting and code highlighting features of your favorite IDE (at least not out of the box).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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