First I used CodeIgniter, just started to learn the framework when used for a while, and then the company with Thinkphp, it was no longer used, after 2, there will be some contrast. First of all, the 2 frame folder capacity is relatively large, not lightweight and so on, I do not look good.
Today Nathan CodeIgniter Framework
Example of an official website controller invocation template
<?php echo $title;? >
Feeling this mode is not good, is this the legendary controller template separation? This mode can only be used to trick the phper of the beginning, regardless of the call smarty or the like to refute me, just say his own characteristics, and what framework can be combined with smarty to use.
The above code can be implemented with a PHP function, please refer to the use of the extract function.
Many use CodeIgniter Phper estimate is directed at the so-called graceful to go, can use coherent writing,
In essence, there is no significant effect on the template (view),
Just calculate all the variables first, change the name, and then, when needed, fill in the new name to the desired place.
Then, when the individual is wandering around a foreign site, a template is found, which has a great similarity to the CodeIgniter model. The template name is unknown. You can look at it and you can get a general idea of the CodeIgniter principle.
It's the core template code I write about
Path = $path;} /** * Set The path to the template files. * * @param string $path path to template files * * @return void */function Set_path ($path) {$this->path = $path;} /** * Set a template variable. * * @param string $name name of the variable to set * @param mixed $value The value of the variable * * @return void */fun Ction set ($name, $value) {$this->vars[$name] = $value;} /** * Open, Parse, and return the template file. * * @param string string The template file name * * @return string */function Fetch ($file) {extract ($this->vars); Extract the VARs to local namespaceob_start (); Start output Bufferinginclude ($this->path. $file); Include the file$contents = Ob_get_contents (); Get the contents of the Bufferob_end_clean (); End Buffering and Discardreturn $contents; Return the contents}}/** * A extension to template that provides automatic caching of * template contents. */class Cachedtemplate extends Template {var $cache _id;var $expire; var $cached;/** * Constructor. * * @param string $path path to template files * @para M string $cache _id unique cache identifier * @param int $expire Number of seconds the cache would live * * @return void */f Unction cachedtemplate ($path, $cache _id = null, $expire = N) {$this->template ($path); $this->cache_id = $cache _id ? ' cache/'. MD5 ($cache _id): $cache _id; $this->expire = $expire;} /** * Test to see whether the currently loaded cache_id have a valid * corrosponding cache file. * * @return bool */function is_cached () {if ($this->cached) return true;//Passed a cache_id?if (! $this->cache_id) ret Urn false;//Cache file exists?if (!file_exists ($this->cache_id)) return false;//Can get the time of the file?if (! ( $mtime = Filemtime ($this->cache_id))) return false;//cache Expired?if (($mtime + $this->expire) < time ()) {@unli NK ($this->cache_id); return false;} else {/** * Cache the results of this is_cached () call. Why? So * weDon ' t has to double the overhead for each template. * If We didn ' t cache, it would is hitting the file system * twice as much (File_exists () & Filemtime () [twice each]). */$this->cached = True;return true;}} /** * This function returns a cached copy of a template (if it exists), * Otherwise, it parses it as normal and caches the Content. * * @param $file string The template file * @return string */function fetch_cache ($file) {if ($this->is_cached ()) {$fp = @fopen ($this->cache_id, ' R '); $contents = Fread ($fp, FileSize ($this->cache_id)); fclose ($fp); return $contents;} else {$contents = $this->fetch ($file);//Write the Cacheif ($fp = @fopen ($this->cache_id, ' W ')) {fwrite ($fp, $conten TS); fclose ($FP);} else {die (' Unable to write cache. ');} return $contents;}}}? >
In this core template class, the same is used extract ($this->vars); function to split the array.
Let's look at his "controller" usage.
Set (' title ', ' User List ');/** * Create A template object for the inner template and set its variables. The * fetch_user_list () function simply returns an array of users. */$body = & New Template ($path), $body->set (' User_list ', fetch_user_list ());/** * Set the fetched template of the Ner template to the ' body ' variable in * the outer template. */$TPL->set (' body ', $body->fetch (' user_list.tpl.php ')); This is the direct call template/** * Echo the results. */echo $tpl->fetch (' index.tpl.php ');/** * Just a function to simulate the retrieval of a user list. */function fetch_user_list () {return array (' id ' = 1, ' name ' = ' bob ', ' email ' = ' bob@mozilla.org ', ' Banned ' and false), array (' id ' = = 2, ' name ' = ' Judy ', ' email ' = ' judy@php.net ', ' banned ' = False) , array (' id ' = = 3, ' name ' = ' Joe ', ' email ' = ' joe@opera.com ', ' banned ' = ' false '), array (' id ' = = 4, ' Name ' = ' Billy ', ' email ' = ' billy@wakeside.com ', ' banned ' and ' = True ', array (' id ' = = 5, ' name ' = ' Eileen ', ' email ' = ' eileen@slashdot.org ', ' banned ' and ' = False ')}? >
Set is the assignment value. and see how it's "view" is output, basically exactly like CodeIgniter.
index.tpl.php<?= $title;? >
user_list.tpl.php
Detailed Download Address: http://download.csdn.net/detail/xjl756425616/3984218
Reply to discussion (solution)
Copyright (c) 2003 .....
10 Years!!!
Just because it's April 1 today?
LZ recommended a few frames chant! ~
PHP amp
What do you think
Copyright (c) 2003 .....
10 Years!!!
Just because it's April 1 today?
What is the relationship between technology and date? I just feel like CodeIgniter's way.
LZ recommended a few frames chant! ~
There is no recommendation I have used those 2, lack of comprehensive contrast.
template.php
/**
* Copyright (c) 2003 Brian E. Lozier (Brian@massassi.net)
Reply
CodeIgniter is not a template engine
Since it is the framework, of course he also has his own template engine
10 years ago, things were too old.
Don't you want to be unconventional, too?
4.1 April Fools ' Day
PHP amp
What do you think
I'll download it and take a look.
What's the difference between your template and CI?
Template in the final analysis, is not ob+extract?
PHP amp
What do you think
Look, there's nothing to comment about, like the template engine.
Recommended CI, is the CI easy to get started, templates, not all is the same,
In fact, the framework is not right and wrong, look at the demand. For example, I now the company project urgent, come are new, ability uneven. CI framework is very suitable, with the framework of the use of experience, with this thing 1, 2 days basic can be started to develop.
However, the limitations are also obvious ...
I personally think CI is good, MVC division of labor, the code is simple and clear
CI also can, I have been using and did not find any problems.
PHP amp
What do you think
Look, there's nothing to comment about, like the template engine.
There is nothing wrong with extra, because 99% of the framework does this for template parsing, such as YII2:
Public Function Renderphpfile ($_file_, $_params_ = []) { ob_start (); Ob_implicit_flush (false); Extract ($_params_, extr_overwrite); Require ($_file_); return Ob_get_clean (); }
Some even use eval (), such as dedecms .....