ตายๆๆๆคับ พักนี้ไม่มีเวลาเลยยยย รีบปั่นโปรเจ็คที่ทำอยู่ เจออะไรใหม่ๆก็ศึกษาจนงานเริ่มยืดดด ครบกำหนดส่งแล้วยังไม่เสร็จ - -*
โพสท์ก่อนโน้นนน ผมโพสท์เกี่ยวกับ View Codeigniter คือผมไม่อยากจะเอา Template Engine มาัใช้งานร่วมกับ Codeigniter เลย เพราะผมกลัวว่ามันจะเปลือง memory ลำพัง Codeigniter อย่างเดียวก็เยอะแล้ว ผมเลยใช้ View ของ Codeigniter ไปแบบจำใจ แถมศึกษาเสียเวลา extends Core ของ Codeigniter อีกกว่าจะใช้ views ในแบบของผมได้ แต่ทำยังงัยก็กลับตายรัง กลับมาใช้ Smarty อีกรอบดีกว่า สงสัยชาตินี้จะหนี Smarty ไม่พ้นแล้ว พอไม่ได้ใช้ Smarty ไปพักนึงก็กลับมาทวนความจำหน่อย
โชคดีที่ผมไป extends Core ของ Codeigniter บ่อยจนพอเข้าใจ core ของมันบ้าง แล้ววันนี้ผมก็จะกลับมาใช้ Smarty แทน View แต่ในระหว่างที่กำลังทำการลง Smarty ใน Codeigniter นั้น ผมมองโค้ดเก่าๆเลยรู้สึกว่า "แบบนี้มันไม่ได้ใช้ Smarty แทน View นี่นา"
ในโค้ดเก่าของผมก็คือ สร้าง class ลูก extends Smarty , เซ็ต Smarty Config ใน Contructor , จับใส่โฟลเดอร์ library แล้วก็ load เข้ามาใช้ใน Controller ก็จบ
จริงๆแล้วแบบนี้ก็ถูกแล้วครับ แต่มันไม่ควรจะจบแค่นี้ครับ (หมายถึงถ้าจะพูดว่าใช้ Smarty แทน vew มันไม่ควรจะจบแค่นี้) เพราะถ้าเราทำแค่นี้ output จะไม่ถูกนำไปประมวลผลโดย Output Class ของ Codeigniter , เพราะว่า Loader Class ซึ่งในกรณีนี้มันทำให้หน้า load view แล้วส่ง final output ไปยัง Output Class เพื่อประมวลผลครั้งสุดท้ายอีกที แต่ถ้าเราใช้ Smarty คือ output จะไม่ถูกนำไป final process โดย Output Class
แล้วใน final process ของ output class มันทำอะไร ?
อันนี้เนื่องจากว่าผมต้องการจะแสดง execution time ของสคริปท์ในหน้าเพจ ผมต้องการเวลาที่ใช้ไปตั้งแต่ Benchmark Class ของ Codeigniter เริ่มทำงาน ซึ่ง Class นี้จะเป็น Class แรกใน Codeigniter ที่เริ่มทำงานด้วย และ class นี้จะถูกเรียกอีกครั้งใน Output Class เพื่อเช็คเวลาที่ใช้ไปทั้งหมดตั้งแต่เริ่ม Codeigniter จน final output ถูกส่งไปยัง browser ครับ ผมก็เลยต้องทำการแก้ Class ลูก ที่ผม extends มาจาก Smarty เปลี่ยนจากการ display output ธรรมดาเป็นส่งไปยัง Output Class แทนครับ (การทำงานใน Controller ก็ยังทำงานเหมือนเดิมไม่เปลี่ยนแปลง)
ลองดู CI Smarty ของผมกันครับ
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
| <?php defined('BASEPATH') or die('Direct Acces Not Allowed');
// โหลด Smarty
require_once 'smarty/Smarty.class.php';
// สร้าง Class ลูก ไว้ใช้ใน Codeigniter
class CI_Smarty extends Smarty {
function CI_Smarty()
{
parent::Smarty();
// Smarty Config
$this->template_dir = ABSPATH . 'application/views';
$this->compile_dir = SMARTY_DIR . 'templates_c';
$this->config_dir = SMARTY_DIR . 'configs';
$this->cache_dir = SMARTY_DIR . 'cache';
// จัดการไฟล์ cache และ compiled file เก็บไว้ใน subdirectory
// เพื่อป้องกันกรณีใช้ multiple template จะทำให้ compile ชนกัน
$this->use_sub_dirs = TRUE;
// ไม่ค่อยแน่ใจตัวเองเลย ใช้อันนี้ดีป่าวเนี่ย
$this->left_delimiter = '{!';
$this->right_delimiter = '}';
// โหลด Filter
$this->load_filter('output','trimwhitespace');
// กำหนดค่า view uri , site_url และ site_domain ไว้ใช้ใน smarty ให้อัตโนมัติ
$this->assign('view_uri' , ABSURL . 'application/views/');
$this->assign('site_url' , ABSURL);
$this->assign('site_domain' , DOMAIN);
}
/**
* ไว้เซ็ต base template จะถูก append ต่อจาก $this->template_dir
*
* @access public
* @param string ! ชื่อของโฟลเดอร์เทมเพลท
* @return void
**/
function set_template($template)
{
// เปลี่ยนค่า view uri ให้ smarty อัตโนมัติ
$this->template_dir .= '/' . $template;
$this->assign('view_uri' , ABSURL . 'application/views/' . $template . '/');
}
/**
* ปรับปรุงจากคลาสแม่ ให้ส่ง output ไปยัง Output Class ของ Codeigniter แทนการ echo
*
* @access public
* @param ดูเพิ่มเติมใน Smarty - [libraries/smarty/Smarty.class.php]
* @return void
**/
function display($resource_name, $cache_id = null, $compile_id = null)
{
// ดึง output จาก template
$output = $this->fetch($resource_name, $cache_id, $compile_id);
// เรียก instance ของ codeigniter
$CI =& get_instance();
// ส่ง output เข้า Output Class ของ CodeIgniter
$CI->output->append_output($output );
}
} |
เวลาจะแสดง execution time แค่ใส่ {elapsed_time} ลงไป template แค่นั้นเอง กรณีของผมผมใช้ {! ... } เป็น l/r delim เลยไม่มีปัญหา แต่ถ้าใครใช้ default ของ Smarty ให้ใช้ {ldelim}elapsed_time{rdelim} แทนครับผม