Template Library


Template Library

我正在尝试使用Phil Sturgeon创建的模板库。他的模板库有一些很棒的文档,但我在如何完成文件结构方面遇到了一些问题。

-views
    -layouts
        -default.php
    -partials
        -header_view.php
        -footer_view.php
        -metadata_view.php
    login_view.php

在我的登录控制器索引函数中,我有以下代码来确定要使用的布局以及要加载的视图作为模板的主体。我还在header_view.php文件中包含了我所拥有的内容,我认为这是我无法正常工作的内容。我希望能够针对header_view内部的部分元数据。但是,根据我正确执行的文档,我收到来自header_view.php文件的错误未定义的索引元数据。关于这个问题的任何光。

来自 github repostitory 的文档:https://github.com/philsturgeon/codeigniter-template

$this->template
        ->title('Login Form')
        ->set_partial('header', 'partials/header_view')
        ->set_partial('metadata', 'partials/metadata_view')
        ->set_partial('footer', 'partials/footer_view')
        ->set_layout('default')
        ->build('login_form_view');

默认.php

<?php echo $template['partials']['header']; ?>
<?php echo $template['body']; ?>
<?php echo $template['partials']['footer']; ?>

header_view.php

<!DOCTYPE html>
<html>
<head>
    <title><?php echo $template['title']; ?></title>
    <?php echo $template['partials']['metadata']; ?>
</head>
    <body
好吧,

我是一个编码点火器粉丝; 老实说,我认为 CI 的真正力量在于它没有加载花哨的"模板库"或身份验证库; 然而,它为您提供了所有工具来快速,非常愉快地构建自己的工具;

所以我的答案不是你真正要求的,而是一个简单的例子,说明如何在不到 10 分钟的时间内在 CI 中构建自己的所谓的 templaling lib 并且不流畅地工作;

  1. 在应用程序/核心文件夹中创建MY_Controller;

    class MY_Controller extends CI_Controller {
    protected $noEcho=true,$body = 'base/layout', $title = 'YOUR CONTROLLER TITLE',
    $js = array(), //filename
    $inline_js = '', //script
    $css = array(), 
    $inline_css = '', //style
    $content = array(); //html
    

里面将是3个基本功能

  1. 设置页面部分;
  2. 管理您的资产
  3. 打印出您的最终结果页面;

1

function output($data,$section='content'){
//checking type of data to be pushed its either array or string(html)
//this is a view it should be formated like this array( viewname,$data )
if( is_array($data) ){
  $this->content[ $section ][] = $this->load->view( $data[0], $data[1], TRUE );
  return $this;//to allow chaing
}elseif( is_string($data) ){//this is html
  $this->content[ $section ][] = $data;
  return $this;//to allow chaing
}
}
第二个是允许您将js,css

和内联js&css添加到此页面的函数;

function _asset( $link, $txt = FALSE ) {
if ( $txt !== FALSE ) {
  if ( $txt == 'js' )
    $this->inline_js[] = $txt;
  elseif ( $txt == 'css' )
    $this->inline_css[] = $txt;
}else{
  if ( pathinfo( $link, PATHINFO_EXTENSION ) == 'css' ){
    $this->css[] = link_tag( base_url( 'assets/css/' . trim( $link, "/''" ) ) );
  }else{
    $this->js[] = '<script src="' . base_url( 'assets/js/' . trim( $link, "/''" ) ) . '"></script>';
  }   
}
return $this;
}

最后是一个将所有部分组合在一起的功能;

protected function print_page(){
  if ( $this->noEcho ) ob_clean();
  $data=array();
  $data[ 'title' ] = $this->title;
  $data[ 'css' ]        = is_array( $this->css ) ? implode( "'n", $this->css ) : '';
  $data[ 'js' ]         = is_array( $this->js ) ? implode( "'n", $this->js ) : '';
  $data[ 'inline_css' ] = ( $this->inline_css ) ? '<style>' . implode( "'n", $this->inline_css ) . '</style>' : '';
  $data[ 'inline_js' ]  = ( $this->inline_js ) ? implode( "'n", $this->inline_js ) : '';
  foreach ( $this->content as $section => $content ) {
    $data[ $section ] = is_array( $content ) ? implode( "'n'n'n ", $content ) : $content;
  } //$this->content as $section => $content
  return $this->load->view( $this->body, $data );
}

现在将所有三个放在一起,并将控制器扩展到此基本控制器;

现在,对于您尝试构建的示例页面,我会这样做:

控制器:

public function __construct() {
  parent::__construct();
  $this->title = 'Controller title';
  $this->body = 'base/default';
  //load all your assets.. if its across all pages then load them in MY_controller construct
  $this->assets('jquery.min.js')
       ->assets('bootstrap.css')
       ->assets('alert("itworks");','js');//will be added to $inline_js
}
function index(){
$var = $This->some_model->get_data();
$this->output( array('some_View',$var) )
     ->output('<hr/>')
     ->output('THIS WILL BE IN $FOOTER','footer')
     ->print_page();
}

现在那:)有多干净?

现在,您的控制器将加载this->body上设置的视图,并将所有部分传递给它。因此,对于上面的示例,您的视图将收到 2 个变量。

  1. $content : 病态some_view视图 +
  2. $footer :将包含我们传递给它的 html;
  3. $css,$js,$inline_js,$inline_css 变量包含所有资产
  4. $title包含您的页面标题;

在结尾我希望这个小演示能帮助您了解这 3 个小函数可以做的无限可能性,这要归功于 CI 本机视图加载器。