Since came to the new company has been very busy, recently this period of time finally a little idle, hurriedly continue to write this series, feel no longer write on the rotten tail.
As we said before, when we get this piece of {{ $name }} content, we just need to convert it <?php echo $name ?> so that we can recognize it and output the corresponding variable value.
That would require a regular match {{ $name }} , and then replace and replace them with {{ }} each <?php echo other ?> .
But to think of a problem, if I write PHP code in the view, which contains {{ $name }} , it will be replaced. Examples are as follows:
<?php$name = ' Test '; $str = "{$name}}";? >
To solve this problem, we need to remove the PHP code, leaving only the HTML code to do the replacement processing. Fortunately PHP has a method Token_get_all, will be provided by the PHP tag to split the content. Use this method to parse the following:
$content = <<<view<?php\ $name = ' test '; \ $str = ' {{\ $name}} ';? >
Here the $ symbol before plus \ is for the transfer, which is not needed in the real reality. The results are as follows:
Array ([0] = = Array ([0] = 379 [1] = <?php [2] = 1) [1] = = Array ([0] = 382 [1] = [2] = 2) [2] = = [3] = = Array ([0] = 382 [1] = [2] = 2) [4] = Arra Y ([0] = 323 [1] = ' Test ' [2] = 2) [5] = =; [6] = = Array ([0] = 382 [1] = [2] = 2) [7] = = [ 8] = = Array ([0] = 382 [1] = [2] = 3) [9] = = "[1] 0] = = Array ([0] = 322 [1] = = {{[2] = 3) [One] = ARR Ay ([0] = [1] = $name [2] = 3) [] = = Array ([0] = 322 [1] + =}} [2] = 3) [[] = [+] =; [+] = Array ([0] = 382 [1] = [2] = 3) [+] = Arra Y ([0] = 381 [1] =?> [2] = 4) [+] = = Array ([0] = 321 [1] =>
You can see that the PHP-related code is parsed, we just need to determine the HTML code, and then replace it. 321 is the value of the defined constant T_INLINE_HTML , and the markup parses the HTML code.
We define the view file with the suffix SF, then we can create the file in the controller/model/view directory, the view.sf content is as follows
<?php$title = ' It is a title '; $str = ' {{$title}} ';? >
And then we're going to change the Controller method in the render code below
Public function render ($view, $params = []) { $file = '). /views/'. $view. '. SF '; $fileContent = file_get_contents ($file); $result = "; foreach (Token_get_all ($fileContent) as $token) { if (Is_array ($token)) { list ($id, $content) = $token; if ($id = = t_inline_html) { $content = preg_replace ('/{{(. *)}}/', ' <?php echo '?> ', $content); } $result. = $content; } else { $result. = $token; } } $generatedFile = '. /runtime/cache/'. MD5 ($file); File_put_contents ($generatedFile, $result); Extract ($params); Require_once $generatedFile;}
Modify the actionView following
Public Function Actionview () {$this->render (' Site/view ', [' body ' = ' Test body information ']);}
Visit Http://localhost/simple-framework/public/index.php?r=site/view to get the following page
We'll be here before today. Project content and blog content will also be put on GitHub, welcome suggestions.
Code
Blog Project: