CodeIgniter_04_View

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

  1. /app/view/good.php
  2. /app/Libraries/Good.php
  3. /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') ?>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值