ข้ามไปที่เนื้อหาหลัก

PHP Codeigniter and Template Parser Class call Variable out of Pairs

This below code is part of  View from CodeIgniter Docuemnt
https://www.codeigniter.com/userguide3/libraries/parser.html




and Controller like this






When run on web browser all array data will display at braces same key name.
and associative array will display in  {blog_entries} .......... {/blog_entries} 





But, site_url cannot display in {blog_entries} .......... {/blog_entries} 

Create test page controllers/Welcome.php
<?phpdefined('BASEPATH') OR exit('No direct script access allowed');class Welcome extends CI_Controller { public function index() { $this->load->helper('url'); $this->load->library('parser'); $data = array( 'site_url' => site_url(), 'blog_title'   => 'My Blog Title', 'blog_heading' => 'My Blog Heading', 'blog_entries' => array( array('title' => 'Title 1', 'body' => 'Body 1'), array('title' => 'Title 2', 'body' => 'Body 2'), array('title' => 'Title 3', 'body' => 'Body 3'), array('title' => 'Title 4', 'body' => 'Body 4'), array('title' => 'Title 5', 'body' => 'Body 5') ) ); $this->parser->parse('welcome_message', $data); }}?>

Create view file views/welcome_message.php
<!DOCTYPE html><html lang="en"><head> <meta charset="utf-8"> <title>{blog_title}</title> <style type="text/css"> body { background-color: #fff; margin: 40px; font: 13px/20px normal Helvetica, Arial, sans-serif; color: #4F5155; } </style></head><body>        <h3>H3 : {blog_heading}</h3> <p>ลิงค์ : {site_url}</p>        {blog_entries}            <h5>H5 : {title}</h5>            <p>P : {body}</p> <p>Link : {site_url}</p>        {/blog_entries}</body></html>

When reload page again {site_url} is a normal text not display link url





For solve this problems

Override parser class with below code

Create file MY_Parser.php at this path  application/libraries/MY_Parser.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');/** * Overrides the CI Template Parser to allow for multiple occurrences of the * same variable pair * */class MY_Parser extends CI_Parser { // -------------------------------------------------------------------- /**  * Parse a template  *  * Parses pseudo-variables contained in the specified template view,  * replacing them with the data in the second param  *  * @param string  * @param array  * @param bool  * @return string  */ public function parse($template, $data, $return = FALSE) { $template = $this->CI->load->view($template, $data, TRUE); $results = $this->_parse_double($template, $data); $results = $this->_parse($results, $data, TRUE); if ($return === FALSE) { $this->CI->output->append_output($results); } return $results; } // -------------------------------------------------------------------- /**  * Parse a single key/value  *  * @param string  * @param string  * @param string  * @return string  */ protected function _parse_double($results, $data) { $replace = array(); preg_match_all("/\{\{(.*?)\}\}/si", $results, $matches); foreach ($matches[1] as $match) { $key = '{{'.$match.'}}'; $replace[$key] = isset($data[$match]) ? $data[$match] : $key; } $results = strtr($results, $replace); return $results; }}// END Parser Class/* End of file MY_Parser.php *//* Location: ./application/libraries/MY_Parser.php */


Then edit your view file views/welcome_message.php
in Variable Pairs if call out of pairs variable used double braces

{{site_url}}


<!DOCTYPE html><html lang="en"><head> <meta charset="utf-8"> <title>{blog_title}</title> <style type="text/css"> body { background-color: #fff; margin: 40px; font: 13px/20px normal Helvetica, Arial, sans-serif; color: #4F5155; } </style></head><body>        <h3>H3 : {blog_heading}</h3> <p>ลิงค์ : {site_url}</p>        {blog_entries}            <h5>H5 : {title}</h5>            <p>P : {body}</p> <p>Link : {{site_url}}</p>        {/blog_entries}</body></html>

Now go to reload page you can see link URL at {{site_url}}






Function Reference

How to get the shortest rather than longest possible regex match with preg_match()
{{something1}} something2 {{something3}} something4https://stackoverflow.com/questions/5897478/how-to-get-the-shortest-rather-than-longest-possible-regex-match-with-preg-match

Translate characters or replace substrings
http://php.net/manual/en/function.strtr.php

ความคิดเห็น

โพสต์ยอดนิยมจากบล็อกนี้

แจกฟรี!! ระบบต่างๆที่พัฒนาด้วย PHP สำหรับนำไปใช้ในงานต่างๆ

       สำหรับหลายท่านที่ขอโค้ดเข้ามาทาง Inbox ของเฟซบุ๊กแฟนเพจ หรือถามถึงระบบต่างๆหลังไมค์มานั้น ส่วนใหญ่ก็มีแจกอยู่แล้วในเว็บบอร์ด ThaiCreate.Com นะครับ และด้านล่างนี้ก็เป็นระบบต่างๆที่แจกให้นำไปลองใช้ลองศึกษากันครับ

สอนเขียน PHP แสดงการจองห้องประชุมแบบไฮไลท์ตามช่วงเวลา (แบบเชื่อมต่อฐานข้อมูล MySQL)

ตัวอย่าง ผลลัพธ์ที่ได้จากการจองในฐานข้อมูล ตาราง tb_room สร้างตารางรายชื่อห้องประชุม สำหรับ id นั้นเป็น Primarykey จะกำหนดให้สร้างอัตโนมัติ ทุกครั้งที่เราเพิ่มชื่อห้องประชุมใหม่ -- -- Database: `tobedev_example` -- -- -------------------------------------------------------- -- -- Table structure for table `tb_room` -- CREATE TABLE IF NOT EXISTS `tb_room` (   `id` int(11) NOT NULL,   `name` varchar(30) NOT NULL ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8; -- -- Dumping data for table `tb_room` -- INSERT INTO `tb_room` (`id`, `name`) VALUES (1, 'ห้องประชุม 1'), (2, 'ห้องประชุม 2'), (3, 'ห้องประชุม 3'), (4, 'ห้องประชุม 4'), (5, 'ห้องประชุม 5'); -- -- Indexes for dumped tables -- -- -- Indexes for table `tb_room` -- ALTER TABLE `tb_room`   ADD PRIMARY KEY (`id`); -- -- AUTO_INCREMENT for dumped tables -- -- -- AUTO_INCREMENT for table `tb_room` -- ALTER TABLE `tb_room`   MODIFY `i...

การนำไอคอน มาแสดงบน Fullcalendar

เนื่องจากมีน้องคนหนึ่งให้ช่วยดูโค้ดเกี่ยวกับ Fullcalendar ให้หน่อย แล้วพอดีช่วงนี้ผมก็กำลังสนใจปฏิทิน Fullcalendar อยู่พอดี จึงลองหาสาเหตุที่ไม่สามารถแสดงข้อมูลจาก MySQL และไม่สามารถแทรก icon เข้าไปใน Fullcalendar ได้ จากที่สังเกตุได้ครั้งแรกคือ Error ที่ Console ของ Firefox เกี่ยวกับฟังก์ชั่นที่เขียนผิด และก็มีการ echo ค้างไว้ในส่วนของไฟล์ getCalendar.php ก็เลยจัดการทดสอบแล้วลบ echo ออกให้เหลือแค่ echo json_encode($event_array); ที่ได้ใช้งานจริงเท่านั้น ขั้นตอนการตรวจสอบความถูกต้องของโค้ด PHP 1) ต้องแน่ใจว่าคำสั่งที่เขียนไว้ สามารถดึงข้อมูลมาแสดงผลได้ด้วยการ echo $sql; 2) นำคำสั่งที่ได้ไปรันในโปรแกรมจัดการฐานข้อมูล ในที่นี้คือ phpMyAdmin 3) เมื่อตรวจสอบดูผลลัพธ์ที่ได้ หากถูกต้องมีข้อมูลก็แสดงว่าการ Query ทำงานได้ 4) มาดูการทำงานของ JavaScript ในส่วนของ jQuery มีการแจ้งเตือนที่ฟังก์ชั่น .on() ซึ่งจะใช้กับ jQuery เวอร์ชั่นใหม่เท่านั้น นั่นหมายถึงเวอร์ชั่น jQuery ที่มากับ Fullcalendar เป็นเวอร์ชั่นเก่า ก็ให้เปลี่ยนไปใช้ .live() แทน ก็จะทำงานได้ปกติ 5) ม...