View
1.Introduction
What is View tier?View is the presentation component/tier where displays all the details system provides.
2.An intelligent thought on designating View architecture
2.1 the default thought
return view('view_file_name');
This thought always considers accessing view tier only via the specific file.(static method ?dynamic method)
2.2 how to construct a dynamic webpage
echo view('header');
echo view('body');
echo view('footer');
It appears that building up a web page with three separate/distributed files.
2.3 transfer data from controller to view
/app/controllers/controller.php
$data = ['element_name_01'=>'element_01','element_name_02'=>'element_02'];
echo view('file_name',$data);
/app/view/view.php
foreach($element_name_01 as $data ){
echo $data;
}
//A problem:JS imported files cannot works when it is placed onto the end of body and head ,but the middle of body instead.
3.URL mapping
3.1 problem
A problem emerges when I wanna access one file on upper level.This is because the way URL I input access files is based on the current file base.As a result,codeigniter cannot find the targeted file returning 404 error.
3.2 solution
// ..返回上一級文件夾
<a class="navbar-brand" href="../mall/shop">Navbar</a>
4.view_cell():make the code of view tier cleaner
View Cells allow you to insert HTML that is generated outside of your controller. It simply calls the specified class and method, which must return a string of valid HTML. This method could be in any callable method, found in any class that the autoloader can locate. The only restriction is that the class can not have any constructor parameters. This is intended to be used within views, and is a great aid to modularizing your code.
CodeIgniter Document-view_cell()
4.1 specification
- /app/view/good.php
- /app/Libraries/Good.php
- /app/view/component/good_item.php
analyse method:bottom-up
3->2->1
4.2 code
good_item.php is simple and the details of HTML code.
//attention:title
<p><?=$title?></p>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Accusamus sit veritatis ullam quia consectetur perferendis blanditiis minus, odit fuga excepturi quo dicta cum omnis dolore ut nisi? Eligendi, voluptatem pariatur?</p>
<img src="/assets/image/images.jpg" alt="..." class="img-thumbnail">
/app/Libraries/Good.php
<?php
namespace App\Libraries;
Class Good{
public function loadItem($param){
return view('component/post_item',$param);
}
}
?>
//app/view/good.php
<?php
foreach ($data_01 as $data) {
?>
<?=view_cell('\App\Libraries\Good::loadItem',['title'=>$data]);?>
<?php
}
?>
- One point should be emphasized =>
[‘title’=>$data] --------> $param ---------> $title
5.layout
5.1specification
CodeIgniter supports a simple, yet very flexible, layout system that makes it simple to use one or more base page layouts across your application. Layouts support sections of content that can be inserted from any view being rendered. You could create different layouts to support one-column, two-column, blog archive pages, and more. Layouts are never directly rendered. Instead, you render a view, which specifies the layout that it wants to extend.
Codigniter Document-view-layout
workflow:
view/view.php(basic operator)
load
views/layout/main_layout.php
5.2 codes
views/layout/main_layout.php
(put header && footer files into it and leaves content/the modified part alone)
//--------------------------------------------------------------------------------------------------------------------- -------------------------------------------------contend-----------------------------------------------------------
<!--core part-->
<div class="container">
<?= $this->renderSection('content'); ?>
</div>
<!--end of core part-->
//---------------------------------------------------------------------------------------------------------------------
//renderSection('file_name') : load the corresponsive file
view/view.php
<?= $this->extend('../Views/layout/main_layout') ?>
<?= $this->section('content') ?>
.
.
.
.
<?= $this->endSection() ?>
//extend():specify the files calles this view.php
//section(): specify the section declared in extend() files
5.3 include
Additional Knowledge:Including View Partials
View partials are view files that do not extend any layout. They typically include content that can be reused from view to view.
<?= $this->include('Views/partial/sidebar') ?>