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

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 นะครับ และด้านล่างนี้ก็เป็นระบบต่างๆที่แจกให้นำไปลองใช้ลองศึกษากันครับ

สร้างแบบฟอร์มล็อกอิน HTML + PHP

       ผมจะสร้างระบบตัวอย่างเพื่อแสดงให้เห็นการทำงานของสคริปต์ PHP ที่ใช้ในการล็อกอินอย่างง่ายๆ นะครับ โดยที่ผมจะสร้างโฟลเดอร์ใหม่ขึ้นในไดร์ฟที่เราได้กำหนดให้เป็น Directory Root สำหรับรันสคริปต์ PHP ซึ่งผมกำหนดเอาไว้ที่ D:\www ดังนั้นผมจะสร้างโฟลเดอร์ teacher_assistant ขึ้นมาใหม่จะได้พาธเป็น D:\www\teacher_assistant สำหรับการเข้าถึง จะใช้ URL ดังนี้ http://localhost/teacher_assistant หลังจากสร้างโฟลเดอร์เสร็จเรียบร้อยก็จะเริ่มกระบวนการเตรียมโครงสร้างโปรเจ็กต์ด้วยเครื่องมือช่วยเขียนโปรแกรมชื่อว่า eclipse ซึ่งดูวิธีการสร้างโปรเจ็กต์ได้จากบทความ เริ่มต้นสร้างโปรเจ็กต์ ด้วยโปรแกรม eclipse เครื่องมือเขียนโปรแกรม php ในบทความเป็นการสร้างโปรเจ็กต์ที่มีพาธเป็นโฟลเดอร์ login แต่ในที่นี้ต้องเป็นพาธให้ตรงกับโฟลเดอร์ใหม่ที่เราได้สร้างขึ้นด้วยนะครับ

PHP กับการคิดส่วนลดเป็นเปอร์เซ็น 5%, 10%, 20% ตามช่วงราคาที่กำหนด

<?php     $total_price = 1000;     $discount = 0;         if($total_price >= 500 && $total_price < 1001){         $discount = 5;     }elseif($total_price >= 1001 && $total_price <= 5000){         $discount = 10;     }elseif($total_price >= 5001){         $discount = 20;     }         $discount_bath = ($total_price*$discount)/100; ?> <pre> ซื้อสินค้าครบ 0 ถึง 499 บาท ไม่ได้ส่วนลด ซื้อสินค้าครบ 500 ถึง 1000 บาท ได้ส่วนลด 5% ซื้อสินค้าครบ 1001 ถึง 5000บาท ได้ส่วนลด 10% ซื้อสินค้าครบ 5001 บาทขึ้นไป ได้ส่วนลด 20% </pre> <h3>รวมราคาสินค้า = <?php echo number_format($total_price,2);?></h3> <h5>ส่วนลด = <?php echo $discount;?>%  (<?php echo $discount_bath;?> บาท)</h5> <h2>ราคาหลังส่วนลด = <?php echo number_format($total_price - $discount_bath,2);?>บาท</h2> "PHP ไม่ได้สร้างสุดยอดโปรแกรม แต่ PHP ช่วยให้งานคุณง่ายขึ้นต