Sample basic php template for file structure, with example

In the early days of PHP applications, “spaghetti code” was a familiar sight. In this article, we’ll cover how khổng lồ separate the view of your PHP application from its other components.

Bạn đang xem: Sample basic php template for file structure, with example

We’ll look at why using such an architecture is useful và what tools we can use to accomplish this. Here’s what we’ll cover:

Learn some basic MVC concepts,Review some popular templating libraries,Play around with a small custom-made view class.Explore the basics of using the Twig library.

To fully benefit from this article, you should already know how lớn write & run your own PHP scripts on a Web VPS (i.e. using Apache).

Further Reading on SmashingMag:

A Quick Introduction To The MVC Pattern

In the early days of PHP applications, “spaghetti code” was a familiar sight. Fragments of PHPhường code were mixed in with HTML mark-up. There were no frameworks, so Web applications were just a bunch of source files. As the PHPhường language matured, developers started khổng lồ think about the cleanliness and maintainability of their code. The model-view-controller (MVC) pattern was introduced.

MVC is a software architecture that allows for the separation of business logic from the user interface. In this architecture, the user sees và interacts with the view that, in the case of Web applications, is generated HTML code (along with JavaScript, CSS, images, etc.)


User actions are passed (as HTTPhường. requests, GET or POST methods) to lớn the controller. The controller is a piece of code that handles và processes user input đầu vào & then reads & makes necessary changes lớn the model, which is responsible for the storage and modification of data. (In simple terms, the mã sản phẩm consists of the database structure và contents, and the code used to access it.) Then, the controller generates the proper view that will be sent & displayed to lớn user.

Such separation of layers has many advantages…

Code Is Easier to Maintain

Because the Mã Sản Phẩm is separated, changing internal data relations without changing the rest of the application is easier. For example, the model for the user could provide the is_active() method, returning a boolean variable. In the business logic, it would be enough to check what value is returned by this method, without any knowledge of its internals.

You can also check for various conditions (for example, whether the user has confirmed their registration, paid their monthly fee, etc.), and you can change these rules in one place, without having to lớn change anything in the other parts of the code. It also becomes easy khổng lồ make a platform-independent application that allows for switching between database engines; this is often performed by ORM (object-relational mapper) libraries, which are a sub-layer of the Mã Sản Phẩm layer. ORMs add a level of abstraction lớn the library that accesses the specific database directly.

Xem thêm: Cách Cài Mật Khẩu Máy Tính Win Xp, Hướng Dẫn Cài Đặt Mật Khẩu Cho Máy Tính

The Same Content in Multiple Views

Separating the view allows a single result khổng lồ be presented in different forms. For example, based on some lô ghích, a news page could be displayed in normal, thiết bị di động và RSS versions, all using the same content returned from the controller.

More Secure

Variables are escaped by default before being sent khổng lồ the view, which is useful in applications that display user-generated nội dung. Consider a variable with some unknown HTML or JavaScript code being passed lớn the view (which should never happen in your application). Escaping the variables prevents malicious code from being injected in such cases.

Better Code

Separating the layers forces the programmer to design better applications that are easier & cheaper lớn maintain. Also, designers và front-over developers don’t have sầu to lớn work with the business-xúc tích và ngắn gọn code, because their task is only to display the contents of the variables that are provided lớn the view. This reduces the risk of the breaking some PHPhường code.

Today, every modern framework implements the MVC architecture. There are also some libraries that enable you to use selected features of the architecture. In this article, we’ll go over the recipes for the view, and in future articles we’ll cover the mã sản phẩm and some ORM libraries.

Overview Of PHP Template Engines

As you might expect, many templating libraries allow you to lớn separate the view layer from the rest of the application. In the most basic scenario, you can implement such a library by yourself (which we’ll vì chưng in the next section). You don’t even need a special library; sometimes it is enough just lớn separate your view (i.e. the template’s HTML files) inlớn a different directory, prepare some variables in the simple controller (usually the main PHP file) and include the templates.

Every modern PHPhường Web application framework employs some kind of templating engine. Most of them use plain PHP by default (including Symfony 1.x, Zkết thúc Framework and CakePHP), but many standalone libraries can be plugged into your favorite framework or custom application.

Xem thêm: Techsmith Camtasia Studio 9 Activation Key {Latest Version} Full Free Here!


Smarty was one of the first và most advanced templating engines. It was developed as a sub-project of the main PHP project, but it has lost its popularity in recent years due khổng lồ poor performance & forced backward compatibility with PHP.. 4. It has also lacked many modern features, such as template inheritance. Now, it is rising bachồng to lớn power with version 3. Many other frameworks (such as Twig and Django) have sầu inherited some of the basic concepts laid down by Smarty.

Info User Information: Name: $name Addr: escape Date: date_format:"%b %e, %Y"

Chuyên mục: Theme wordpress