Home Backend Development PHP Tutorial CI框架集成Smarty的方法分析_PHP

CI框架集成Smarty的方法分析_PHP

May 27, 2016 am 10:18 AM
ci framework smarty integrated

本文实例讲述了CI框架集成Smarty的方法。分享给大家供大家参考,具体如下:

因为CI自带的模板功能不是很方便,所以大家普遍采用集成Smarty的方式来弥补CI这方面的不足。

本人在网上看了不少CI集成Smarty的教程,包括咱们CI论坛里面的一个精华帖子

http://codeigniter.org.cn/forums/forum.php?mod=viewthread&tid=10345。

自己对比了一下这些教程,我认为下面这个方案是所有里面最优秀的,强烈推荐给大家(当然也是我自己采取的方案)

出处:

http://www.cnmiss.cn/?p=261

原文里面的一些错误,我在本文里面已经做了修正

下面说下我认为它更加优秀的原因,对比下这个方案和我们论坛的方案,你会发现,这个方案多了一点就是它扩展了核心类,

它将Smarty类方法assign和display扩展到Ci的控制器中,所以我们在CI中使用Smarty的时候可以像这样使用:

public function index()
{
    //$this->load->view('welcome_message');
    $data['title'] = '标题';
    $data['num'] = '123456789';
    //$this->cismarty->assign('data',$data); // 也可以
    $this->assign('data',$data);
    $this->assign('tmp','hello');
    //$this->cismarty->display('test.html'); // 也可以
    $this->display('test.html');
}

Copy after login

通过对核心控制器类的简单扩展,大家在CI中使用Smary的时候和直接使用Smarty的用法习惯是一样的,这是一个很大的优点啊。

而且从核心类库的扩展来看,大家也可以看出该文作者对于CI框架的理解是很好的。

根据这篇文章,我不仅成功集成了Smaty,而且也进一步加强了对于CI的理解。

而且该方案将Smarty的配置文件放到了CI框架的config目录下,对于两者,使用都很规范。

最终实现了"CI和Smaty的无缝结合"。

下面开始是具体教程: // 我在原文的基础上做了一些修改,更正了原文的一些错误 注意下文中有'//'的地方,是我自己修改过的地方,或是自己又增加的地方。

CI版本:2.1.4 // (本文发布时使用的版本)

Smarty版本:Smarty-2.6.26 // 因为我之前用这个版本,为了照顾自己的使用习惯,这里没有使用最新的Smaty版本,大家理解了扩展原理,可以选择自己想用的Smatry版本。

1、到相应站点下载Smarty的源码包; // 我这里用的是 Smarty-2.6.26

2、将源码包里面的libs文件夹copy到CI的项目目录下面的libraries文件夹下,并重命名为Smarty-2.6.26;//

3、在项目目录的libraries文件夹内新建文件Cismarty.php,里面的内容如下:

<&#63;php
if(!defined('BASEPATH')) EXIT('No direct script asscess allowed');
require_once( APPPATH . 'libraries/Smarty-2.6.26/libs/Smarty.class.php' );
class Cismarty extends Smarty {
  protected $ci;
  public function __construct(){
    $this->ci = & get_instance();
    $this->ci->load->config('smarty');//加载smarty的配置文件
    //获取相关的配置项
    $this->template_dir  = $this->ci->config->item('template_dir');
    $this->complie_dir  = $this->ci->config->item('compile_dir');
    $this->cache_dir   = $this->ci->config->item('cache_dir');
    $this->config_dir   = $this->ci->config->item('config_dir');
    $this->template_ext  = $this->ci->config->item('template_ext');
    $this->caching    = $this->ci->config->item('caching');
    $this->cache_lifetime = $this->ci->config->item('lefttime');
  }
}

Copy after login

4、在项目目录的config文件夹内新建文件smarty.php文件,里面的内容如下:

<&#63;php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$config['theme']    = 'default';
$config['template_dir'] = APPPATH . 'views';
$config['compile_dir'] = FCPATH . 'templates_c';
$config['cache_dir']  = FCPATH . 'cache';
$config['config_dir']  = FCPATH . 'configs';
$config['template_ext'] = '.html';
$config['caching']   = false;
$config['lefttime']   = 60;

Copy after login

5、在入口文件所在目录新建文件夹templates_c、cache、configs;

6、在项目目录下面的config目录中找到autoload.php文件
修改这项

$autoload['libraries'] = array('Cismarty');
//目的是:让系统运行时,自动加载,不用人为的在控制器中手动加载

Copy after login

7、在项目目录的core文件夹中新建文件MY_Controller.php 内容如下: // 扩展核心控制类

<&#63;php if (!defined('BASEPATH')) exit('No direct access allowed.');
class MY_Controller extends CI_Controller { // 原文这里写错
  public function __construct() {
    parent::__construct();
  }
  public function assign($key,$val) {
    $this->cismarty->assign($key,$val);
  }
  public function display($html) {
    $this->cismarty->display($html);
  }
}

Copy after login

配置完毕

使用方法实例:

在控制器中如:

<&#63;php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends MY_Controller { // 原文这里写错
  public function index()
  {
    //$this->load->view('welcome_message');
    $data['title'] = '标题';
    $data['num'] = '123456789';
    //$this->cismarty->assign('data',$data); // 亦可
    $this->assign('data',$data);
    $this->assign('tmp','hello');
    //$this->cismarty->display('test.html'); // 亦可
    $this->display('test.html');
  }
}

Copy after login

然后再视图中:试图文件夹位于项目目录的views之下:

新建文件test.html

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>{ $test.title}</title> //( 原文是 <title>{$test['title']}</title>,是错误的写法,也有可能是Smarty版本的原因)
<style type="text/css">
</style>
</head>
<body>
{$test.num|md5} // 原文这里也写错了
<br>
{$tmp}
</body>
</html>

Copy after login

更多关于CodeIgniter相关内容感兴趣的读者可查看本站专题:《smarty模板入门基础教程》、《codeigniter入门教程》、《CI(CodeIgniter)框架进阶教程》、《php优秀开发框架总结》、《ThinkPHP入门教程》、《ThinkPHP常用方法总结》、《Zend FrameWork框架入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家基于CodeIgniter框架的PHP程序设计有所帮助。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1253
24
How to use CI framework in php? How to use CI framework in php? Jun 01, 2023 am 08:48 AM

With the development of network technology, PHP has become one of the important tools for Web development. One of the popular PHP frameworks - CodeIgniter (hereinafter referred to as CI) has also received more and more attention and use. Today, we will take a look at how to use the CI framework. 1. Install the CI framework First, we need to download the CI framework and install it. Download the latest version of the CI framework compressed package from CI's official website (https://codeigniter.com/). After the download is complete, unzip

How to migrate and integrate projects in GitLab How to migrate and integrate projects in GitLab Oct 27, 2023 pm 05:53 PM

How to migrate and integrate projects in GitLab Introduction: In the software development process, project migration and integration is an important task. As a popular code hosting platform, GitLab provides a series of convenient tools and functions to support project migration and integration. This article will introduce the specific steps for project migration and integration in GitLab, and provide some code examples to help readers better understand. 1. Project migration Project migration is to migrate the existing code base from a source code management system to GitLab

Oracle API integration strategy analysis: achieving seamless communication between systems Oracle API integration strategy analysis: achieving seamless communication between systems Mar 07, 2024 pm 10:09 PM

OracleAPI integration strategy analysis: To achieve seamless communication between systems, specific code examples are required. In today's digital era, internal enterprise systems need to communicate with each other and share data, and OracleAPI is one of the important tools to help achieve seamless communication between systems. This article will start with the basic concepts and principles of OracleAPI, explore API integration strategies, and finally give specific code examples to help readers better understand and apply OracleAPI. 1. Basic Oracle API

Integration of PHP and ETL tools Integration of PHP and ETL tools May 16, 2023 am 11:30 AM

As enterprise data becomes larger and more complex, the need for data processing and analysis becomes more urgent. In order to solve this problem, ETL (extract, transform, load) tools have gradually become an important tool for enterprise data processing and analysis. As a popular web development language, PHP can also improve the efficiency and accuracy of data processing and analysis through integration with ETL tools. Introduction to ETL tools ETL tools are a type of software that can extract data, perform data conversion, and load data into the target system. Its full name is extract-transfer

GitLab API integration and custom plug-in development tips GitLab API integration and custom plug-in development tips Oct 20, 2023 pm 05:30 PM

GitLab's API integration and custom plug-in development skills Introduction: GitLab is an open source code hosting platform that provides a rich API interface for developers to use to facilitate integration and custom plug-in development. This article will introduce how to integrate GitLab's API and some tips on custom plug-in development, and provide specific code examples. 1. Obtain API access token for GitLab's API integration. Before API integration, you first need to obtain GitLab's API access token. beat

How to use middleware for WeChat payment integration in Laravel How to use middleware for WeChat payment integration in Laravel Nov 02, 2023 pm 05:21 PM

How to use middleware for WeChat payment integration in Laravel Introduction: WeChat payment is a very common and convenient payment method. For many projects that require online payment services, integrating WeChat payment is an essential step. In the Laravel framework, WeChat payment integration can be achieved by using middleware to better manage the request process and process payment logic. This article will introduce how to use middleware for WeChat payment integration in Laravel and provide specific code examples. 1. Preparation at the beginning

How to use CI framework in PHP How to use CI framework in PHP Jun 27, 2023 pm 04:51 PM

PHP is a popular programming language that is widely used in web development. The CI (CodeIgniter) framework is one of the most popular frameworks in PHP. It provides a complete set of ready-made tools and function libraries, as well as some popular design patterns, allowing developers to develop Web applications more efficiently. This article will introduce the basic steps and methods of developing PHP applications using the CI framework. Understand the basic concepts and structures of the CI framework. Before using the CI framework, we need to understand some basic concepts and structures. Down

Copilot Integration: Collaboration in SharePoint and Dynamics 365 Customer Service Copilot Integration: Collaboration in SharePoint and Dynamics 365 Customer Service Aug 03, 2023 pm 09:21 PM

Microsoft today announced an early preview of SharePoint integration with Copilot in Dynamics 365 Customer Service. This integration will give customer service agents access to a wider range of knowledge sources, resulting in increased productivity and improved customer interactions. Currently, Copilot in Dynamics365 Customer Service leverages an internal knowledge base to provide guidance to customer service agents. By suggesting chat and draft email content, Copilot has become a key tool for increasing the productivity of your customer service team. However, customer feedback indicates that the tool needs to leverage knowledge from external sources such as SharePoint. SharePoint Collaborative Driving Integration In response to this feedback,

See all articles