Sign in Registration
ruen

Placing content separate from PHP code

When developing, it is often required to indicate in the code any values ​​of variables containing Russian or English text. This text can be called content . In order to ensure easy maintenance of the code in the future, it is necessary to think carefully about the code structure . Namely, according to the MVC concept , content should be placed separately from code. This approach will make it easy to change values, translate text into different languages.

How can you implement this in PHP? The simplest solution is to create a content file next to the logic file. For example, the file with the code will be named admin.php , and next to it, place admin.content . Next, you need to create once the code that will include the content file, as a result of which all content will be available in the file with logic.

Размещение контента отдельно от кода при разработке сайта или приложения на PHP

The file with the content itself can be represented as a array , the keys of which will be the names of the variables, and the values ​​of the content of this variable itself. Such an array can be multidimensional . In the code, after connecting the content file , you can refer to the array elements by specifying the keys. You can also implement immediately multilingualism , in this case the array may contain keys with the names of languages, inside these keys directly the necessary keys and values.

Контент отдельно от кода

This code splitting approach allows you to easily replace content values ​​when needed. For example, you need to change the name of a menu item - for this you do not need to search for this fragment in a long logic file, you just need to find the required key and change its values, and next to it you can easily specify a value for another language .

Usage is shown in the screenshot below. One method is created for the entire application, which connects the content file (which you specify, for example, by calling the method with the parameter-name of the content file). In this method, you can implement everything you need, for example, taking into account the application language, which will allow, depending on the site settings, to use the necessary content values. Next, an array from the content file is written to the content variable, after which you can simply access the required values ​​by the key.

Контент отдельно от кода, файл логики

Thus, separating content from code is an important part of developing a quality website or web application . This approach is used by the Eqsash CMS , which can be downloaded from the download section on this site.

Comments (0)
For commenting sign in or register.
Оставить заявку

Latest articles

Popular sections

Eqsash (Tools)

Android app - VK LAST USER ID, отучитель от зависимости и т.д.:
Available on Google Play

Amessage (Communication)

Login to the web version
Android app:
Available on Google Play

Share this

Subscribe to

YouTube

Books

IT notes - In simple language about the most necessary things (HTML, CSS, JavaScript, PHP, databases, Drupal, Bitrix, SEO, domains, security and more), PDF, 500 p.