@@ -10,7 +10,9 @@ A view is simply a web page, or a page fragment, like a header, footer, sidebar,
1010views can flexibly be embedded within other views (within other views, etc.) if you need
1111this type of hierarchy.
1212
13- Views are never called directly, they must be loaded by a controller. Remember that in an MVC framework,
13+ Views are never called directly, they must be loaded by a controller or :ref: `view route <view-routes >`.
14+
15+ Remember that in an MVC framework,
1416the Controller acts as the traffic cop, so it is responsible for fetching a particular view. If you have
1517not read the :doc: `Controllers </incoming/controllers >` page, you should do so before continuing.
1618
@@ -19,7 +21,7 @@ Using the example controller you created in the controller page, let's add a vie
1921Creating a View
2022===============
2123
22- Using your text editor, create a file called `` blog_view.php `` and put this in it::
24+ Using your text editor, create a file called ** blog_view.php ** and put this in it::
2325
2426 <html>
2527 <head>
@@ -35,19 +37,27 @@ Then save the file in your **app/Views** directory.
3537Displaying a View
3638=================
3739
38- To load and display a particular view file you will use the following function :
40+ To load and display a particular view file you will use the following code in your controller :
3941
4042.. literalinclude :: views/001.php
43+ :lines: 2-
4144
4245Where *name * is the name of your view file.
4346
44- .. important :: If the file extension is omitted, then the views are expected to end with the .php extension.
47+ .. important :: If the file extension is omitted, then the views are expected to end with the ** .php** extension.
4548
46- Now, open the controller file you made earlier called ``Blog.php ``, and replace the echo statement with the view function:
49+ Now, create a file called **Blog.php ** in the **app/Controllers ** directory,
50+ and put this in it:
4751
4852.. literalinclude :: views/002.php
4953
50- If you visit your site using the URL you did earlier you should see your new view. The URL was similar to this::
54+ Open the routing file located at **app/Config/Routes.php **, and look for the "Route Definitions".
55+ Add the following code:
56+
57+ .. literalinclude :: views/013.php
58+ :lines: 2-
59+
60+ If you visit your site, you should see your new view. The URL was similar to this::
5161
5262 example.com/index.php/blog/
5363
@@ -69,6 +79,7 @@ Your view files can also be stored within sub-directories if you prefer that typ
6979When doing so you will need to include the directory name loading the view. Example:
7080
7181.. literalinclude :: views/004.php
82+ :lines: 2-
7283
7384Namespaced Views
7485================
@@ -77,8 +88,10 @@ You can store views under a **View** directory that is namespaced, and load that
7788PHP does not support loading non-class files from a namespace, CodeIgniter provides this feature to make it possible
7889to package your views together in a module-like fashion for easy re-use or distribution.
7990
80- If you have ``example/blog `` directory that has a PSR-4 mapping set up in the :doc: `Autoloader </concepts/autoloader >` living
81- under the namespace ``Example\Blog ``, you could retrieve view files as if they were namespaced also. Following this
91+ If you have **example/blog ** directory that has a PSR-4 mapping set up in the :doc: `Autoloader </concepts/autoloader >` living
92+ under the namespace ``Example\Blog ``, you could retrieve view files as if they were namespaced also.
93+
94+ Following this
8295example, you could load the **blog_view.php ** file from **example/blog/Views ** by prepending the namespace to the view name:
8396
8497.. literalinclude :: views/005.php
@@ -88,15 +101,17 @@ example, you could load the **blog_view.php** file from **example/blog/Views** b
88101Caching Views
89102=============
90103
91- You can cache a view with the ``view `` command by passing a ``cache `` option with the number of seconds to cache
104+ You can cache a view with the ``view() `` function by passing a ``cache `` option with the number of seconds to cache
92105the view for, in the third parameter:
93106
94107.. literalinclude :: views/006.php
108+ :lines: 2-
95109
96110By default, the view will be cached using the same name as the view file itself. You can customize this by passing
97111along ``cache_name `` and the cache ID you wish to use:
98112
99113.. literalinclude :: views/007.php
114+ :lines: 2-
100115
101116Adding Dynamic Data to the View
102117===============================
@@ -105,6 +120,7 @@ Data is passed from the controller to the view by way of an array in the second
105120Here's an example:
106121
107122.. literalinclude :: views/008.php
123+ :lines: 2-
108124
109125Let's try it with your controller file. Open it and add this code:
110126
@@ -134,9 +150,10 @@ other views, potentially causing issues. If you would prefer to clean the data a
134150into the ``$option `` array in the third parameter.
135151
136152.. literalinclude :: views/010.php
153+ :lines: 2-
137154
138155Additionally, if you would like the default functionality of the ``view() `` function to be that it does clear the data
139- between calls, you can set ``$saveData `` to ** false ** in **app/Config/Views.php **.
156+ between calls, you can set ``$saveData `` to `` false `` in **app/Config/Views.php **.
140157
141158Creating Loops
142159==============
0 commit comments