Skip to content

Commit

Permalink
Merge pull request #8780 from kenjis/docs-improve-views
Browse files Browse the repository at this point in the history
docs: improve View pages
  • Loading branch information
kenjis authored Apr 16, 2024
2 parents 6a65664 + 9483b35 commit d425bdf
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 10 deletions.
1 change: 1 addition & 0 deletions user_guide_src/source/general/common_functions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ Service Accessors
:rtype: string

Grabs the current RendererInterface-compatible class
(:doc:`View <../outgoing/view_renderer>` class by default)
and tells it to render the specified view. Simply provides
a convenience method that can be used in Controllers,
libraries, and routed closures.
Expand Down
2 changes: 1 addition & 1 deletion user_guide_src/source/outgoing/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ View components are used to build what is returned to the user.
:titlesonly:

views
view_cells
view_renderer
view_layouts
view_cells
view_parser
view_decorators
table
Expand Down
18 changes: 17 additions & 1 deletion user_guide_src/source/outgoing/view_decorators.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,36 @@
View Decorators
###############

.. contents::
:local:
:depth: 2

*************************
What are View Decorators?
*************************

View Decorators allow your application to modify the HTML output during the rendering process. This happens just
prior to being cached, and allows you to apply custom functionality to your views.

You can use View Decorators with :doc:`view_renderer` or :doc:`view_parser`.

*******************
Creating Decorators
*******************

Creating Decorator Class
========================

Creating your own view decorators requires creating a new class that implements ``CodeIgniter\View\ViewDecoratorInterface``.
This requires a single method that takes the generated HTML string, performs any modifications on it, and returns
the resulting HTML.

.. literalinclude:: view_decorators/001.php

Once created, the class must be registered in ``app/Config/View.php``:
Registering Decorator Class
===========================

Once created, the class must be registered in **app/Config/View.php**:

.. literalinclude:: view_decorators/002.php

Expand Down
4 changes: 2 additions & 2 deletions user_guide_src/source/outgoing/view_decorators/002.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

class View extends BaseView
{
// ...

public array $decorators = [
'App\Views\Decorators\MyDecorator',
];

// ...
}
14 changes: 8 additions & 6 deletions user_guide_src/source/outgoing/view_parser.rst
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ need to be unique, or a later parameter setting will over-ride an earlier one.
This also impacts escaping parameter values for different contexts inside your
script. You will have to give each escaped value a unique parameter name.

Parser templates
Parser Templates
================

You can use the ``render()`` method to parse (or render) simple templates,
Expand All @@ -90,10 +90,12 @@ like this:
.. literalinclude:: view_parser/003.php

View parameters are passed to ``setData()`` as an associative
array of data to be replaced in the template. In the above example, the
template would contain two variables: ``{blog_title}`` and ``{blog_heading}``
The first parameter to ``render()`` contains the name of the :doc:`view
file </outgoing/views>`, Where *blog_template* is the name of your view file.
array of data to be replaced in the template.

In the above example, the template would contain two variables: ``{blog_title}`` and ``{blog_heading}``

The first parameter to ``render()`` contains the name of the template, where
``blog_template`` is the name of your view template file.

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

Expand All @@ -102,7 +104,7 @@ Parser Configuration Options

Several options can be passed to the ``render()`` or ``renderString()`` methods.

- ``cache`` - the time in seconds, to save a view's results; ignored for renderString()
- ``cache`` - the time in seconds, to save a view's results; ignored for ``renderString()``
- ``cache_name`` - the ID used to save/retrieve a cached view result; defaults to the viewpath;
ignored for renderString()
- ``saveData`` - true if the view data parameters should be retained for subsequent calls;
Expand Down
2 changes: 2 additions & 0 deletions user_guide_src/source/outgoing/views.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ 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.

.. note:: The ``view()`` function uses :doc:`view_renderer` internally.

Now, create a file called **Blog.php** in the **app/Controllers** directory,
and put this in it:

Expand Down

0 comments on commit d425bdf

Please sign in to comment.