This article describes the CodeIgniter view usage precautions. Share it with everyone for your reference, the details are as follows:
The view in CI is the template file under application/views/. The template supports the direct use of PHP, so there is not much to say about the implementation of the template. Here are some things to note about views:
1. The CI view can load multiple views in the controller, and the page will finally combine these contents and output them. If the load action here is left to the controller, the controller will need to be adjusted when the template structure needs to be adjusted, which is not good. It can be encapsulated in the controller or directly handed over to the view to ensure that each ACTION only loads one view file.
2. Let the view do what it is good at. Do not define HTML directly in the PHP code. This will make it difficult to maintain both the program and the view. If you want to load HTML, you can return it through load.
3. Although there is a universal get_instance method, do not directly read data or do other module things in the view. The role of the view is to receive data and display it to ensure that people fulfill their responsibilities and things are put to their best use.
4. Regarding the question of whether to use templates, I personally tend to use templates. When using PHP directly, you need to deal with the definition of variables and the problem of converting to HTML entities. Templates can solve these problems and provide some more convenient operations. If we want to talk about efficiency issues, they are not here, and most of them are not in PHP.
Readers who are interested in more content related to the CodeIgniter framework can check out the special topics on this site: "Introduction to codeigniter tutorial" and "Advanced tutorial on CI (CodeIgniter) framework"
I hope this article will be helpful to everyone’s PHP program design based on the CodeIgniter framework.