| | |
| | if( $this->layout !== '' ) { |
| | - $layoutFile = $this->layout; |
| | + $layout = $this->layout; |
| | $this->layout = ''; |
| | |
| | $this->assign( 'content', $this->blocks['content'] ?? '' ); |
| | - |
| | - $res = $this->evaluate( $layoutFile ); |
| | + $res = $this->evaluate( $layout ); |
| | } |
| | |
| | return $res; |
| | } |
| | |
| | private function evaluate( string $file ): string { |
| | - $path = __DIR__ . '/' . $file; |
| | - $code = \file_get_contents( $path ); |
| | - $php = $this->compile( $code ); |
| | - $tmp = \tempnam( \sys_get_temp_dir(), 'tpl_' ); |
| | - $res = ''; |
| | + $php = $this->compile( \file_get_contents( __DIR__ . '/' . $file ) ); |
| | + $tmp = \tempnam( \sys_get_temp_dir(), 'tpl_' ); |
| | |
| | \file_put_contents( $tmp, $php ); |
 |
| | |
| | private function compile( string $code ): string { |
| | - $c = \preg_replace_callback( '/\{([^\}]+)\}/', function( $m ) { |
| | - $inner = \preg_replace_callback( |
| | - '/(\$[a-zA-Z0-9_]+)((?:\.[a-zA-Z0-9_]+)+)/', |
| | - function( $m2 ) { |
| | - $parts = \explode( '.', \ltrim( $m2[2], '.' ) ); |
| | - $res = $m2[1]; |
| | - |
| | - foreach( $parts as $p ) { |
| | - $res .= "['$p']"; |
| | - } |
| | + $c = \preg_replace_callback( |
| | + '/\{strip\}(.*?)\{\/strip\}/s', |
| | + function( $m ) { |
| | + return \preg_replace( '/\s*\n\s*/', '', $m[1] ); |
| | + }, |
| | + $code |
| | + ); |
| | |
| | - return $res; |
| | - }, |
| | - $m[1] |
| | - ); |
| | + $c = \preg_replace_callback( |
| | + '/\{([^\}]+)\}/', |
| | + function( $m ) { |
| | + return '{' . \preg_replace_callback( |
| | + '/(\$[a-zA-Z0-9_]+)((?:\.[a-zA-Z0-9_]+)+)/', |
| | + function( $m2 ) { |
| | + $parts = \explode( '.', \ltrim( $m2[2], '.' ) ); |
| | + $res = $m2[1]; |
| | |
| | - return '{' . $inner . '}'; |
| | - }, $code ); |
| | + foreach( $parts as $p ) { |
| | + $res .= "['$p']"; |
| | + } |
| | |
| | - $c = \preg_replace( |
| | - '/\{extends\s+"([^"]+)"\}/', |
| | - '<?php $this->layout = "$1"; ?>', |
| | + return $res; |
| | + }, |
| | + $m[1] |
| | + ) . '}'; |
| | + }, |
| | $c |
| | ); |
| | |
| | - $c = \preg_replace( |
| | + $search = [ |
| | + '/\{extends\s+"([^"]+)"\}/', |
| | '/\{block\s+([a-zA-Z0-9_]+)\}/', |
| | - '<?php \ob_start(); ?>', |
| | - $c |
| | - ); |
| | - |
| | - $c = \preg_replace( |
| | '/\{\/block\s+([a-zA-Z0-9_]+)\}/', |
| | - '<?php $this->blocks["$1"] = \ob_get_clean(); ?>', |
| | - $c |
| | - ); |
| | - |
| | - $c = \preg_replace( |
| | '/\{yield\s+([a-zA-Z0-9_]+)\}/', |
| | - '<?php echo $this->blocks["$1"] ?? ""; ?>', |
| | - $c |
| | - ); |
| | - |
| | - $c = \preg_replace( '/\{if\s+([^}]+)\}/', '<?php if( $1 ) { ?>', $c ); |
| | - $c = \preg_replace( |
| | + '/\{if\s+([^}]+)\}/', |
| | '/\{elseif\s+([^}]+)\}/', |
| | - '<?php } elseif( $1 ) { ?>', |
| | - $c |
| | - ); |
| | - |
| | - $c = \str_replace( '{else}', '<?php } else { ?>', $c ); |
| | - $c = \str_replace( '{/if}', '<?php } ?>', $c ); |
| | - |
| | - $c = \preg_replace( |
| | '/\{foreach\s+([^}]+)\}/', |
| | - '<?php foreach( $1 ) { ?>', |
| | - $c |
| | - ); |
| | - |
| | - $c = \str_replace( '{/foreach}', '<?php } ?>', $c ); |
| | - |
| | - $c = \preg_replace( '/\{\!\$([^}]+)\}/', '<?php echo (\$$1 ?? ""); ?>', $c ); |
| | - |
| | - $c = \preg_replace( |
| | + '/\{\!\$([^}]+)\}/', |
| | '/\{\$([^}]+)\}/', |
| | - '<?php echo \htmlspecialchars( (string)(\$$1 ?? "") ); ?>', |
| | - $c |
| | - ); |
| | + '/\{else\}/', |
| | + '/\{\/if\}/', |
| | + '/\{\/foreach\}/' |
| | + ]; |
| | + $replace = [ |
| | + '<?php $this->layout = "$1"; ?>', |
| | + '<?php \ob_start(); ?>', |
| | + '<?php $this->blocks["$1"] = \ob_get_clean(); ?>', |
| | + '<?php echo $this->blocks["$1"] ?? ""; ?>', |
| | + '<?php if( $1 ) { ?>', |
| | + '<?php } elseif( $1 ) { ?>', |
| | + '<?php foreach( $1 ) { ?>', |
| | + '<?php echo \$$1 ?? ""; ?>', |
| | + '<?php echo \htmlspecialchars( (string)( \$$1 ?? "" ) ); ?>', |
| | + '<?php } else { ?>', |
| | + '<?php } ?>', |
| | + '<?php } ?>' |
| | + ]; |
| | |
| | - return $c; |
| | + return \preg_replace( $search, $replace, $c ); |
| | } |
| | } |