Skip to content

Commit

Permalink
Merge pull request #7158 from kenjis/fix-docs-views.rst
Browse files Browse the repository at this point in the history
docs: update views.rst
  • Loading branch information
kenjis committed Jan 22, 2023
2 parents 8743f4e + 5e762b4 commit 1d7ecd1
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 20 deletions.
37 changes: 27 additions & 10 deletions user_guide_src/source/outgoing/views.rst
Expand Up @@ -10,7 +10,9 @@ A view is simply a web page, or a page fragment, like a header, footer, sidebar,
views can flexibly be embedded within other views (within other views, etc.) if you need
this type of hierarchy.

Views are never called directly, they must be loaded by a controller. Remember that in an MVC framework,
Views are never called directly, they must be loaded by a controller or :ref:`view route <view-routes>`.

Remember that in an MVC framework,
the Controller acts as the traffic cop, so it is responsible for fetching a particular view. If you have
not read the :doc:`Controllers </incoming/controllers>` page, you should do so before continuing.

Expand All @@ -19,7 +21,7 @@ Using the example controller you created in the controller page, let's add a vie
Creating a View
===============

Using your text editor, create a file called ``blog_view.php`` and put this in it::
Using your text editor, create a file called **blog_view.php** and put this in it::

<html>
<head>
Expand All @@ -35,19 +37,27 @@ Then save the file in your **app/Views** directory.
Displaying a View
=================

To load and display a particular view file you will use the following function:
To load and display a particular view file you will use the following code in your controller:

.. literalinclude:: views/001.php
:lines: 2-

Where *name* is the name of your view file.

.. important:: If the file extension is omitted, then the views are expected to end with the .php extension.
.. important:: If the file extension is omitted, then the views are expected to end with the **.php** extension.

Now, open the controller file you made earlier called ``Blog.php``, and replace the echo statement with the view function:
Now, create a file called **Blog.php** in the **app/Controllers** directory,
and put this in it:

.. literalinclude:: views/002.php

If you visit your site using the URL you did earlier you should see your new view. The URL was similar to this::
Open the routing file located at **app/Config/Routes.php**, and look for the "Route Definitions".
Add the following code:

.. literalinclude:: views/013.php
:lines: 2-

If you visit your site, you should see your new view. The URL was similar to this::

example.com/index.php/blog/

Expand All @@ -69,6 +79,7 @@ Your view files can also be stored within sub-directories if you prefer that typ
When doing so you will need to include the directory name loading the view. Example:

.. literalinclude:: views/004.php
:lines: 2-

Namespaced Views
================
Expand All @@ -77,8 +88,10 @@ You can store views under a **View** directory that is namespaced, and load that
PHP does not support loading non-class files from a namespace, CodeIgniter provides this feature to make it possible
to package your views together in a module-like fashion for easy re-use or distribution.

If you have ``example/blog`` directory that has a PSR-4 mapping set up in the :doc:`Autoloader </concepts/autoloader>` living
under the namespace ``Example\Blog``, you could retrieve view files as if they were namespaced also. Following this
If you have **example/blog** directory that has a PSR-4 mapping set up in the :doc:`Autoloader </concepts/autoloader>` living
under the namespace ``Example\Blog``, you could retrieve view files as if they were namespaced also.

Following this
example, you could load the **blog_view.php** file from **example/blog/Views** by prepending the namespace to the view name:

.. literalinclude:: views/005.php
Expand All @@ -88,15 +101,17 @@ example, you could load the **blog_view.php** file from **example/blog/Views** b
Caching Views
=============

You can cache a view with the ``view`` command by passing a ``cache`` option with the number of seconds to cache
You can cache a view with the ``view()`` function by passing a ``cache`` option with the number of seconds to cache
the view for, in the third parameter:

.. literalinclude:: views/006.php
:lines: 2-

By default, the view will be cached using the same name as the view file itself. You can customize this by passing
along ``cache_name`` and the cache ID you wish to use:

.. literalinclude:: views/007.php
:lines: 2-

Adding Dynamic Data to the View
===============================
Expand All @@ -105,6 +120,7 @@ Data is passed from the controller to the view by way of an array in the second
Here's an example:

.. literalinclude:: views/008.php
:lines: 2-

Let's try it with your controller file. Open it and add this code:

Expand Down Expand Up @@ -134,9 +150,10 @@ other views, potentially causing issues. If you would prefer to clean the data a
into the ``$option`` array in the third parameter.

.. literalinclude:: views/010.php
:lines: 2-

Additionally, if you would like the default functionality of the ``view()`` function to be that it does clear the data
between calls, you can set ``$saveData`` to **false** in **app/Config/Views.php**.
between calls, you can set ``$saveData`` to ``false`` in **app/Config/Views.php**.

Creating Loops
==============
Expand Down
4 changes: 1 addition & 3 deletions user_guide_src/source/outgoing/views/002.php
Expand Up @@ -2,9 +2,7 @@

namespace App\Controllers;

use CodeIgniter\Controller;

class Blog extends Controller
class Blog extends BaseController
{
public function index()
{
Expand Down
4 changes: 1 addition & 3 deletions user_guide_src/source/outgoing/views/009.php
Expand Up @@ -2,9 +2,7 @@

namespace App\Controllers;

use CodeIgniter\Controller;

class Blog extends Controller
class Blog extends BaseController
{
public function index()
{
Expand Down
4 changes: 1 addition & 3 deletions user_guide_src/source/outgoing/views/011.php
Expand Up @@ -2,9 +2,7 @@

namespace App\Controllers;

use CodeIgniter\Controller;

class Blog extends Controller
class Blog extends BaseController
{
public function index()
{
Expand Down
2 changes: 1 addition & 1 deletion user_guide_src/source/outgoing/views/012.php
Expand Up @@ -5,7 +5,7 @@
<body>
<h1><?= esc($heading) ?></h1>

<h3>My Todo List</h3>
<h2>My Todo List</h2>

<ul>
<?php foreach ($todo_list as $item): ?>
Expand Down
5 changes: 5 additions & 0 deletions user_guide_src/source/outgoing/views/013.php
@@ -0,0 +1,5 @@
<?php

use App\Controllers\Blog;

$routes->get('blog', [Blog::class, 'index']);

0 comments on commit 1d7ecd1

Please sign in to comment.