mirror of
https://github.com/codeigniter4/CodeIgniter4.git
synced 2025-02-20 11:44:28 +08:00
643 lines
41 KiB
HTML
643 lines
41 KiB
HTML
|
||
|
||
<!DOCTYPE html>
|
||
<html class="writer-html5" lang="en" >
|
||
<head>
|
||
<meta charset="utf-8" />
|
||
|
||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||
|
||
<title>Views — CodeIgniter 4.1.2 documentation</title>
|
||
|
||
|
||
|
||
<link rel="stylesheet" href="../_static/css/citheme.css" type="text/css" />
|
||
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
|
||
|
||
|
||
|
||
<link rel="shortcut icon" href="../_static/favicon.ico"/>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<!--[if lt IE 9]>
|
||
<script src="../_static/js/html5shiv.min.js"></script>
|
||
<![endif]-->
|
||
|
||
|
||
<script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
|
||
<script src="../_static/jquery.js"></script>
|
||
<script src="../_static/underscore.js"></script>
|
||
<script src="../_static/doctools.js"></script>
|
||
<script src="../_static/language_data.js"></script>
|
||
<script src="../_static/js/citheme.js"></script>
|
||
<script src="../_static/js/carbon.js"></script>
|
||
|
||
<script type="text/javascript" src="../_static/js/theme.js"></script>
|
||
|
||
|
||
<link rel="index" title="Index" href="../genindex.html" />
|
||
<link rel="search" title="Search" href="../search.html" />
|
||
<link rel="next" title="View Cells" href="view_cells.html" />
|
||
<link rel="prev" title="Building Responses" href="index.html" />
|
||
</head>
|
||
|
||
<body class="wy-body-for-nav">
|
||
|
||
|
||
<div class="wy-grid-for-nav">
|
||
|
||
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
|
||
<div class="wy-side-scroll">
|
||
<div class="wy-side-nav-search" style="background: #DD4814" >
|
||
|
||
|
||
|
||
<a href="../index.html">
|
||
|
||
|
||
|
||
|
||
<img src="../_static/ci-logo-text.png" class="logo" alt="Logo"/>
|
||
|
||
</a>
|
||
|
||
|
||
|
||
|
||
<div role="search">
|
||
<form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
|
||
<input type="text" name="q" placeholder="Search docs" />
|
||
<input type="hidden" name="check_keywords" value="yes" />
|
||
<input type="hidden" name="area" value="default" />
|
||
</form>
|
||
</div>
|
||
|
||
|
||
</div>
|
||
|
||
|
||
<div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../intro/index.html">Welcome to CodeIgniter4</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../intro/index.html">Welcome to CodeIgniter4</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../intro/requirements.html">Server Requirements</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../intro/credits.html">Credits</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../intro/psr.html">PSR Compliance</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../installation/index.html">Installation</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../installation/installing_composer.html">Composer Installation</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../installation/installing_manual.html">Manual Installation</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../installation/running.html">Running Your App</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../installation/upgrading.html">Upgrading From a Previous Version</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../installation/troubleshooting.html">Troubleshooting</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../installation/repositories.html">CodeIgniter Repositories</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../tutorial/index.html">Build Your First Application</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../tutorial/static_pages.html">Static pages</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../tutorial/news_section.html">News section</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../tutorial/create_news_items.html">Create news items</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../tutorial/conclusion.html">Conclusion</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../concepts/index.html">CodeIgniter4 Overview</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../concepts/structure.html">Application Structure</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../concepts/mvc.html">Models, Views, and Controllers</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../concepts/autoloader.html">Autoloading Files</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../concepts/services.html">Services</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../concepts/factories.html">Factories</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../concepts/http.html">Working With HTTP Requests</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../concepts/security.html">Security Guidelines</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../general/index.html">General Topics</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../general/configuration.html">Configuration</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../general/urls.html">CodeIgniter URLs</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../general/helpers.html">Helper Functions</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../general/common_functions.html">Global Functions and Constants</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../general/logging.html">Logging Information</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../general/errors.html">Error Handling</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../general/caching.html">Web Page Caching</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../general/ajax.html">AJAX Requests</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../general/modules.html">Code Modules</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../general/managing_apps.html">Managing your Applications</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../general/environments.html">Handling Multiple Environments</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../incoming/index.html">Controllers and Routing</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../incoming/controllers.html">Controllers</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../incoming/routing.html">URI Routing</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../incoming/filters.html">Controller Filters</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../incoming/message.html">HTTP Messages</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../incoming/request.html">Request Class</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../incoming/incomingrequest.html">IncomingRequest Class</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../incoming/content_negotiation.html">Content Negotiation</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../incoming/methodspoofing.html">HTTP Method Spoofing</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../incoming/restful.html">RESTful Resource Handling</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul class="current">
|
||
<li class="toctree-l1 current"><a class="reference internal" href="index.html">Building Responses</a><ul class="current">
|
||
<li class="toctree-l2 current"><a class="current reference internal" href="#">Views</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="view_cells.html">View Cells</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="view_renderer.html">View Renderer</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="view_layouts.html">View Layouts</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="view_parser.html">View Parser</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="table.html">HTML Table Class</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="response.html">HTTP Responses</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="api_responses.html">API Response Trait</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="localization.html">Localization</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="alternative_php.html">Alternate PHP Syntax for View Files</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../database/index.html">Working With Databases</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../database/examples.html">Quick Start: Usage Examples</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../database/configuration.html">Database Configuration</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../database/connecting.html">Connecting to a Database</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../database/queries.html">Running Queries</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../database/results.html">Generating Query Results</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../database/helpers.html">Query Helper Functions</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../database/query_builder.html">Query Builder Class</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../database/transactions.html">Transactions</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../database/metadata.html">Getting MetaData</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../database/call_function.html">Custom Function Calls</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../database/events.html">Database Events</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../database/utilities.html">Database Utilities</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../models/index.html">Modeling Data</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../models/model.html">Using CodeIgniter's Model</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../models/entities.html">Using Entity Classes</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../dbmgmt/index.html">Managing Databases</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../dbmgmt/forge.html">Database Manipulation with Database Forge</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../dbmgmt/migration.html">Database Migrations</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../dbmgmt/seeds.html">Database Seeding</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../libraries/index.html">Library Reference</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/caching.html">Caching Driver</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/cookies.html">Cookies</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/curlrequest.html">CURLRequest Class</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/email.html">Email Class</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/encryption.html">Encryption Service</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/files.html">Working with Files</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/honeypot.html">Honeypot Class</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/images.html">Image Manipulation Class</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/pagination.html">Pagination</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/security.html">Security</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/sessions.html">Session Library</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/throttler.html">Throttler</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/time.html">Times and Dates</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/typography.html">Typography</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/uploaded_files.html">Working with Uploaded Files</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/uri.html">Working with URIs</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/user_agent.html">User Agent Class</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../libraries/validation.html">Validation</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../helpers/index.html">Helpers</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/array_helper.html">Array Helper</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/cookie_helper.html">Cookie Helper</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/date_helper.html">Date Helper</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/filesystem_helper.html">Filesystem Helper</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/form_helper.html">Form Helper</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/html_helper.html">HTML Helper</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/inflector_helper.html">Inflector Helper</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/number_helper.html">Number Helper</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/security_helper.html">Security Helper</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/test_helper.html">Test Helper</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/text_helper.html">Text Helper</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/url_helper.html">URL Helper</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../helpers/xml_helper.html">XML Helper</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../testing/index.html">Testing</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../testing/overview.html">Getting Started</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../testing/database.html">Database</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../testing/fabricator.html">Generating Data</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../testing/controllers.html">Controller Testing</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../testing/feature.html">HTTP Testing</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../testing/response.html">Testing Responses</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../testing/benchmark.html">Benchmarking</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../testing/debugging.html">Debugging Your Application</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../cli/index.html">Command Line Usage</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../cli/cli.html">Running via the Command Line</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../cli/cli_commands.html">Custom CLI Commands</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../cli/cli_generators.html">CLI Generators</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../cli/cli_library.html">CLI Library</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../cli/cli_request.html">CLIRequest Class</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../extending/index.html">Extending CodeIgniter</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../extending/core_classes.html">Creating Core System Classes</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../extending/common.html">Replacing Common Functions</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../extending/events.html">Events</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../extending/basecontroller.html">Extending the Controller</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../extending/authentication.html">Authentication</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../extending/contributing.html">Contributing to CodeIgniter</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
|
||
|
||
|
||
</div>
|
||
|
||
</div>
|
||
</nav>
|
||
|
||
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
|
||
|
||
|
||
<nav class="wy-nav-top" aria-label="top navigation">
|
||
|
||
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
|
||
<a href="../index.html">CodeIgniter</a>
|
||
|
||
</nav>
|
||
|
||
|
||
<div class="wy-nav-content">
|
||
|
||
<div class="rst-content">
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<div role="navigation" aria-label="breadcrumbs navigation">
|
||
|
||
<ul class="wy-breadcrumbs">
|
||
|
||
<li><a href="../index.html" class="icon icon-home"></a> »</li>
|
||
|
||
<li><a href="index.html">Building Responses</a> »</li>
|
||
|
||
<li>Views</li>
|
||
|
||
|
||
<li class="wy-breadcrumbs-aside">
|
||
|
||
|
||
|
||
</li>
|
||
|
||
</ul>
|
||
|
||
|
||
<hr/>
|
||
</div>
|
||
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
|
||
<div itemprop="articleBody">
|
||
|
||
<div class="section" id="views">
|
||
<h1>Views<a class="headerlink" href="#views" title="Permalink to this headline">¶</a></h1>
|
||
<div class="contents local topic" id="contents">
|
||
<ul class="simple">
|
||
<li><p><a class="reference internal" href="#creating-a-view" id="id1">Creating a View</a></p></li>
|
||
<li><p><a class="reference internal" href="#displaying-a-view" id="id2">Displaying a View</a></p></li>
|
||
<li><p><a class="reference internal" href="#loading-multiple-views" id="id3">Loading Multiple Views</a></p></li>
|
||
<li><p><a class="reference internal" href="#storing-views-within-sub-directories" id="id4">Storing Views within Sub-directories</a></p></li>
|
||
<li><p><a class="reference internal" href="#namespaced-views" id="id5">Namespaced Views</a></p></li>
|
||
<li><p><a class="reference internal" href="#caching-views" id="id6">Caching Views</a></p></li>
|
||
<li><p><a class="reference internal" href="#adding-dynamic-data-to-the-view" id="id7">Adding Dynamic Data to the View</a></p></li>
|
||
<li><p><a class="reference internal" href="#creating-loops" id="id8">Creating Loops</a></p></li>
|
||
</ul>
|
||
</div>
|
||
<p>A view is simply a web page, or a page fragment, like a header, footer, sidebar, etc. In fact,
|
||
views can flexibly be embedded within other views (within other views, etc.) if you need
|
||
this type of hierarchy.</p>
|
||
<p>Views are never called directly, they must be loaded by a controller. 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 <a class="reference internal" href="../incoming/controllers.html"><span class="doc">Controllers</span></a> page, you should do so before continuing.</p>
|
||
<p>Using the example controller you created in the controller page, let’s add a view to it.</p>
|
||
<div class="section" id="creating-a-view">
|
||
<h2><a class="toc-backref" href="#id1">Creating a View</a><a class="headerlink" href="#creating-a-view" title="Permalink to this headline">¶</a></h2>
|
||
<p>Using your text editor, create a file called <code class="docutils literal notranslate"><span class="pre">BlogView.php</span></code> and put this in it:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><</span><span class="nx">html</span><span class="o">></span>
|
||
<span class="o"><</span><span class="nx">head</span><span class="o">></span>
|
||
<span class="o"><</span><span class="nx">title</span><span class="o">></span><span class="nx">My</span> <span class="nx">Blog</span><span class="o"></</span><span class="nx">title</span><span class="o">></span>
|
||
<span class="o"></</span><span class="nx">head</span><span class="o">></span>
|
||
<span class="o"><</span><span class="nx">body</span><span class="o">></span>
|
||
<span class="o"><</span><span class="nx">h1</span><span class="o">></span><span class="nx">Welcome</span> <span class="nx">to</span> <span class="nx">my</span> <span class="nx">Blog</span><span class="o">!</</span><span class="nx">h1</span><span class="o">></span>
|
||
<span class="o"></</span><span class="nx">body</span><span class="o">></span>
|
||
<span class="o"></</span><span class="nx">html</span><span class="o">></span>
|
||
</pre></div>
|
||
</div>
|
||
<p>Then save the file in your <strong>app/Views</strong> directory.</p>
|
||
</div>
|
||
<div class="section" id="displaying-a-view">
|
||
<h2><a class="toc-backref" href="#id2">Displaying a View</a><a class="headerlink" href="#displaying-a-view" title="Permalink to this headline">¶</a></h2>
|
||
<p>To load and display a particular view file you will use the following function:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="k">echo</span> <span class="nx">view</span><span class="p">(</span><span class="s1">'name'</span><span class="p">);</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>Where <em>name</em> is the name of your view file.</p>
|
||
<div class="admonition important">
|
||
<p class="admonition-title">Important</p>
|
||
<p>If the file extension is omitted, then the views are expected to end with the .php extension.</p>
|
||
</div>
|
||
<p>Now, open the controller file you made earlier called <code class="docutils literal notranslate"><span class="pre">Blog.php</span></code>, and replace the echo statement with the view function:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="k">namespace</span> <span class="nx">App\Controllers</span><span class="p">;</span>
|
||
|
||
<span class="k">class</span> <span class="nc">Blog</span> <span class="k">extends</span> <span class="nx">\CodeIgniter\Controller</span>
|
||
<span class="p">{</span>
|
||
<span class="k">public</span> <span class="k">function</span> <span class="nf">index</span><span class="p">()</span>
|
||
<span class="p">{</span>
|
||
<span class="k">echo</span> <span class="nx">view</span><span class="p">(</span><span class="s1">'BlogView'</span><span class="p">);</span>
|
||
<span class="p">}</span>
|
||
<span class="p">}</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>If you visit your site using the URL you did earlier you should see your new view. The URL was similar to this:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nx">example</span><span class="o">.</span><span class="nx">com</span><span class="o">/</span><span class="nx">index</span><span class="o">.</span><span class="nx">php</span><span class="o">/</span><span class="nx">blog</span><span class="o">/</span>
|
||
</pre></div>
|
||
</div>
|
||
<div class="admonition note">
|
||
<p class="admonition-title">Note</p>
|
||
<p>While all of the examples show echo the view directly, you can also return the output from the view, instead,
|
||
and it will be appended to any captured output.</p>
|
||
</div>
|
||
</div>
|
||
<div class="section" id="loading-multiple-views">
|
||
<h2><a class="toc-backref" href="#id3">Loading Multiple Views</a><a class="headerlink" href="#loading-multiple-views" title="Permalink to this headline">¶</a></h2>
|
||
<p>CodeIgniter will intelligently handle multiple calls to <code class="docutils literal notranslate"><span class="pre">view()</span></code> from within a controller. If more than one
|
||
call happens they will be appended together. For example, you may wish to have a header view, a menu view, a
|
||
content view, and a footer view. That might look something like this:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="k">namespace</span> <span class="nx">App\Controllers</span><span class="p">;</span>
|
||
|
||
<span class="k">class</span> <span class="nc">Page</span> <span class="k">extends</span> <span class="nx">\CodeIgniter\Controller</span>
|
||
<span class="p">{</span>
|
||
<span class="k">public</span> <span class="k">function</span> <span class="nf">index</span><span class="p">()</span>
|
||
<span class="p">{</span>
|
||
<span class="nv">$data</span> <span class="o">=</span> <span class="p">[</span>
|
||
<span class="s1">'page_title'</span> <span class="o">=></span> <span class="s1">'Your title'</span>
|
||
<span class="p">];</span>
|
||
|
||
<span class="k">echo</span> <span class="nx">view</span><span class="p">(</span><span class="s1">'header'</span><span class="p">);</span>
|
||
<span class="k">echo</span> <span class="nx">view</span><span class="p">(</span><span class="s1">'menu'</span><span class="p">);</span>
|
||
<span class="k">echo</span> <span class="nx">view</span><span class="p">(</span><span class="s1">'content'</span><span class="p">,</span> <span class="nv">$data</span><span class="p">);</span>
|
||
<span class="k">echo</span> <span class="nx">view</span><span class="p">(</span><span class="s1">'footer'</span><span class="p">);</span>
|
||
<span class="p">}</span>
|
||
<span class="p">}</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>In the example above, we are using “dynamically added data”, which you will see below.</p>
|
||
</div>
|
||
<div class="section" id="storing-views-within-sub-directories">
|
||
<h2><a class="toc-backref" href="#id4">Storing Views within Sub-directories</a><a class="headerlink" href="#storing-views-within-sub-directories" title="Permalink to this headline">¶</a></h2>
|
||
<p>Your view files can also be stored within sub-directories if you prefer that type of organization.
|
||
When doing so you will need to include the directory name loading the view. Example:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="k">echo</span> <span class="nx">view</span><span class="p">(</span><span class="s1">'directory_name/file_name'</span><span class="p">);</span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
<div class="section" id="namespaced-views">
|
||
<h2><a class="toc-backref" href="#id5">Namespaced Views</a><a class="headerlink" href="#namespaced-views" title="Permalink to this headline">¶</a></h2>
|
||
<p>You can store views under a <strong>View</strong> directory that is namespaced, and load that view as if it was namespaced. While
|
||
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.</p>
|
||
<p>If you have <code class="docutils literal notranslate"><span class="pre">Blog</span></code> directory that has a PSR-4 mapping set up in the <a class="reference internal" href="../concepts/autoloader.html"><span class="doc">Autoloader</span></a> living
|
||
under the namespace <code class="docutils literal notranslate"><span class="pre">Example\Blog</span></code>, you could retrieve view files as if they were namespaced also. Following this
|
||
example, you could load the <strong>BlogView</strong> file from <strong>/blog/views</strong> by prepending the namespace to the view name:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="k">echo</span> <span class="nx">view</span><span class="p">(</span><span class="s1">'Example\Blog\Views\BlogView'</span><span class="p">);</span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
<div class="section" id="caching-views">
|
||
<h2><a class="toc-backref" href="#id6">Caching Views</a><a class="headerlink" href="#caching-views" title="Permalink to this headline">¶</a></h2>
|
||
<p>You can cache a view with the <code class="docutils literal notranslate"><span class="pre">view</span></code> command by passing a <code class="docutils literal notranslate"><span class="pre">cache</span></code> option with the number of seconds to cache
|
||
the view for, in the third parameter:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="c1">// Cache the view for 60 seconds</span>
|
||
<span class="k">echo</span> <span class="nx">view</span><span class="p">(</span><span class="s1">'file_name'</span><span class="p">,</span> <span class="nv">$data</span><span class="p">,</span> <span class="p">[</span><span class="s1">'cache'</span> <span class="o">=></span> <span class="mi">60</span><span class="p">]);</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>By default, the view will be cached using the same name as the view file itself. You can customize this by passing
|
||
along <code class="docutils literal notranslate"><span class="pre">cache_name</span></code> and the cache ID you wish to use:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="c1">// Cache the view for 60 seconds</span>
|
||
<span class="k">echo</span> <span class="nx">view</span><span class="p">(</span><span class="s1">'file_name'</span><span class="p">,</span> <span class="nv">$data</span><span class="p">,</span> <span class="p">[</span><span class="s1">'cache'</span> <span class="o">=></span> <span class="mi">60</span><span class="p">,</span> <span class="s1">'cache_name'</span> <span class="o">=></span> <span class="s1">'my_cached_view'</span><span class="p">]);</span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
<div class="section" id="adding-dynamic-data-to-the-view">
|
||
<h2><a class="toc-backref" href="#id7">Adding Dynamic Data to the View</a><a class="headerlink" href="#adding-dynamic-data-to-the-view" title="Permalink to this headline">¶</a></h2>
|
||
<p>Data is passed from the controller to the view by way of an array in the second parameter of the view function.
|
||
Here’s an example:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$data</span> <span class="o">=</span> <span class="p">[</span>
|
||
<span class="s1">'title'</span> <span class="o">=></span> <span class="s1">'My title'</span><span class="p">,</span>
|
||
<span class="s1">'heading'</span> <span class="o">=></span> <span class="s1">'My Heading'</span><span class="p">,</span>
|
||
<span class="s1">'message'</span> <span class="o">=></span> <span class="s1">'My Message'</span>
|
||
<span class="p">];</span>
|
||
|
||
<span class="k">echo</span> <span class="nx">view</span><span class="p">(</span><span class="s1">'blogview'</span><span class="p">,</span> <span class="nv">$data</span><span class="p">);</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>Let’s try it with your controller file. Open it and add this code:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="k">namespace</span> <span class="nx">App\Controllers</span><span class="p">;</span>
|
||
|
||
<span class="k">class</span> <span class="nc">Blog</span> <span class="k">extends</span> <span class="nx">\CodeIgniter\Controller</span>
|
||
<span class="p">{</span>
|
||
<span class="k">public</span> <span class="k">function</span> <span class="nf">index</span><span class="p">()</span>
|
||
<span class="p">{</span>
|
||
<span class="nv">$data</span><span class="p">[</span><span class="s1">'title'</span><span class="p">]</span> <span class="o">=</span> <span class="s2">"My Real Title"</span><span class="p">;</span>
|
||
<span class="nv">$data</span><span class="p">[</span><span class="s1">'heading'</span><span class="p">]</span> <span class="o">=</span> <span class="s2">"My Real Heading"</span><span class="p">;</span>
|
||
|
||
<span class="k">echo</span> <span class="nx">view</span><span class="p">(</span><span class="s1">'blogview'</span><span class="p">,</span> <span class="nv">$data</span><span class="p">);</span>
|
||
<span class="p">}</span>
|
||
<span class="p">}</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>Now open your view file and change the text to variables that correspond to the array keys in your data:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><</span><span class="nx">html</span><span class="o">></span>
|
||
<span class="o"><</span><span class="nx">head</span><span class="o">></span>
|
||
<span class="o"><</span><span class="nx">title</span><span class="o">><?=</span> <span class="nv">$title</span> <span class="cp">?></span><span class="p"></</span><span class="nt">title</span><span class="p">></span>
|
||
<span class="p"></</span><span class="nt">head</span><span class="p">></span>
|
||
<span class="p"><</span><span class="nt">body</span><span class="p">></span>
|
||
<span class="p"><</span><span class="nt">h1</span><span class="p">></span><span class="cp"><?</span><span class="o">=</span> <span class="nv">$heading</span> <span class="cp">?></span><span class="p"></</span><span class="nt">h1</span><span class="p">></span>
|
||
<span class="p"></</span><span class="nt">body</span><span class="p">></span>
|
||
<span class="p"></</span><span class="nt">html</span><span class="p">></span>
|
||
</pre></div>
|
||
</div>
|
||
<p>Then load the page at the URL you’ve been using and you should see the variables replaced.</p>
|
||
<p>The data passed in is only available during one call to <cite>view</cite>. If you call the function multiple times
|
||
in a single request, you will have to pass the desired data to each view. This keeps any data from “bleeding” into
|
||
other views, potentially causing issues. If you would prefer the data to persist, you can pass the <cite>saveData</cite> option
|
||
into the <cite>$option</cite> array in the third parameter.</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$data</span> <span class="o">=</span> <span class="p">[</span>
|
||
<span class="s1">'title'</span> <span class="o">=></span> <span class="s1">'My title'</span><span class="p">,</span>
|
||
<span class="s1">'heading'</span> <span class="o">=></span> <span class="s1">'My Heading'</span><span class="p">,</span>
|
||
<span class="s1">'message'</span> <span class="o">=></span> <span class="s1">'My Message'</span>
|
||
<span class="p">];</span>
|
||
|
||
<span class="k">echo</span> <span class="nx">view</span><span class="p">(</span><span class="s1">'blogview'</span><span class="p">,</span> <span class="nv">$data</span><span class="p">,</span> <span class="p">[</span><span class="s1">'saveData'</span> <span class="o">=></span> <span class="k">true</span><span class="p">]);</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>Additionally, if you would like the default functionality of the view function to be that it does save the data
|
||
between calls, you can set <code class="docutils literal notranslate"><span class="pre">$saveData</span></code> to <strong>true</strong> in <strong>app/Config/Views.php</strong>.</p>
|
||
</div>
|
||
<div class="section" id="creating-loops">
|
||
<h2><a class="toc-backref" href="#id8">Creating Loops</a><a class="headerlink" href="#creating-loops" title="Permalink to this headline">¶</a></h2>
|
||
<p>The data array you pass to your view files is not limited to simple variables. You can pass multi dimensional
|
||
arrays, which can be looped to generate multiple rows. For example, if you pull data from your database it will
|
||
typically be in the form of a multi-dimensional array.</p>
|
||
<p>Here’s a simple example. Add this to your controller:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span>
|
||
|
||
<span class="k">namespace</span> <span class="nx">App\Controllers</span><span class="p">;</span>
|
||
|
||
<span class="k">class</span> <span class="nc">Blog</span> <span class="k">extends</span> <span class="nx">\CodeIgniter\Controller</span>
|
||
<span class="p">{</span>
|
||
<span class="k">public</span> <span class="k">function</span> <span class="nf">index</span><span class="p">()</span>
|
||
<span class="p">{</span>
|
||
<span class="nv">$data</span> <span class="o">=</span> <span class="p">[</span>
|
||
<span class="s1">'todo_list'</span> <span class="o">=></span> <span class="p">[</span><span class="s1">'Clean House'</span><span class="p">,</span> <span class="s1">'Call Mom'</span><span class="p">,</span> <span class="s1">'Run Errands'</span><span class="p">],</span>
|
||
<span class="s1">'title'</span> <span class="o">=></span> <span class="s2">"My Real Title"</span><span class="p">,</span>
|
||
<span class="s1">'heading'</span> <span class="o">=></span> <span class="s2">"My Real Heading"</span>
|
||
<span class="p">];</span>
|
||
|
||
<span class="k">echo</span> <span class="nx">view</span><span class="p">(</span><span class="s1">'blogview'</span><span class="p">,</span> <span class="nv">$data</span><span class="p">);</span>
|
||
<span class="p">}</span>
|
||
<span class="p">}</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>Now open your view file and create a loop:</p>
|
||
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o"><</span><span class="nx">html</span><span class="o">></span>
|
||
<span class="o"><</span><span class="nx">head</span><span class="o">></span>
|
||
<span class="o"><</span><span class="nx">title</span><span class="o">><?=</span> <span class="nv">$title</span> <span class="cp">?></span><span class="p"></</span><span class="nt">title</span><span class="p">></span>
|
||
<span class="p"></</span><span class="nt">head</span><span class="p">></span>
|
||
<span class="p"><</span><span class="nt">body</span><span class="p">></span>
|
||
<span class="p"><</span><span class="nt">h1</span><span class="p">></span><span class="cp"><?</span><span class="o">=</span> <span class="nv">$heading</span> <span class="cp">?></span><span class="p"></</span><span class="nt">h1</span><span class="p">></span>
|
||
|
||
<span class="p"><</span><span class="nt">h3</span><span class="p">></span>My Todo List<span class="p"></</span><span class="nt">h3</span><span class="p">></span>
|
||
|
||
<span class="p"><</span><span class="nt">ul</span><span class="p">></span>
|
||
<span class="cp"><?php</span> <span class="k">foreach</span> <span class="p">(</span><span class="nv">$todo_list</span> <span class="k">as</span> <span class="nv">$item</span><span class="p">)</span><span class="o">:</span><span class="cp">?></span>
|
||
|
||
<span class="p"><</span><span class="nt">li</span><span class="p">></span><span class="cp"><?</span><span class="o">=</span> <span class="nv">$item</span> <span class="cp">?></span><span class="p"></</span><span class="nt">li</span><span class="p">></span>
|
||
|
||
<span class="cp"><?php</span> <span class="k">endforeach</span><span class="p">;</span><span class="cp">?></span>
|
||
<span class="p"></</span><span class="nt">ul</span><span class="p">></span>
|
||
|
||
<span class="p"></</span><span class="nt">body</span><span class="p">></span>
|
||
<span class="p"></</span><span class="nt">html</span><span class="p">></span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
|
||
</div>
|
||
|
||
</div>
|
||
<footer>
|
||
<div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
|
||
<a href="view_cells.html" class="btn btn-neutral float-right" title="View Cells" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
|
||
<a href="index.html" class="btn btn-neutral float-left" title="Building Responses" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
|
||
</div>
|
||
|
||
<hr/>
|
||
|
||
<div role="contentinfo">
|
||
<p>
|
||
© Copyright 2019-2021 CodeIgniter Foundation.
|
||
<span class="lastupdated">
|
||
Last updated on May 25, 2021.
|
||
</span>
|
||
|
||
</p>
|
||
</div>
|
||
|
||
|
||
|
||
Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
|
||
|
||
<a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
|
||
|
||
provided by <a href="https://readthedocs.org">Read the Docs</a>.
|
||
|
||
</footer>
|
||
</div>
|
||
</div>
|
||
|
||
</section>
|
||
|
||
</div>
|
||
|
||
|
||
<script type="text/javascript">
|
||
jQuery(function () {
|
||
SphinxRtdTheme.Navigation.enable(false);
|
||
});
|
||
</script>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
</body>
|
||
</html> |