Created
August 9, 2017 23:44
-
-
Save Mosharush/ec645c150757257e4461898f938a87a0 to your computer and use it in GitHub Desktop.
PHP function to replace MVC Viewer to end result
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function procMvc( $view, $vars = null ) { | |
$vars = empty( $vars ) ? $GLOBALS : $vars; | |
/* | |
* Replace $view | |
* only var like varname or array value like arrname.varname | |
* short if like varname==8?hhhh output be hhhh if varname eq to 8 | |
*/ | |
if( preg_match_all( '#{{(.+)([=|!]=(.*)\?(.+)|\((.*)\))?}}#Uuims', $view, $checks ) ) { | |
$replacesIn = array(); | |
$replacesOut = array(); | |
foreach ( $checks[ 1 ] as $key => $var ) { | |
$var = explode( '.', $var ); | |
$value = ''; | |
if( is_array( $var ) ) { | |
$isArr = is_array( $vars ); | |
foreach ( $var as $idx => $subvar ) { | |
if( strpos( $checks[ 2 ][ $key ], '(' ) === 0 ) { | |
if( $value == '' ) { | |
if( empty( $var[ $idx + 1 ] ) ) { | |
$value = $isArr ? $vars[ $subvar ]( $checks[ 5 ][ $key ] ) : $vars -> $subvar( $checks[ 5 ][ $key ] ); | |
} else { | |
$value = $isArr ? $vars[ $subvar ] : $vars -> $subvar; | |
} | |
} else { | |
if( empty( $var[ $idx + 1 ] ) ) { | |
$value = $isArr ? $value[ $subvar ]( $checks[ 5 ][ $key ] ) : $value -> $subvar( $checks[ 5 ][ $key ] ); | |
} else { | |
$value = $isArr ? $value[ $subvar ] : $value -> $subvar; | |
} | |
} | |
} else { | |
if( $value == '' ) { | |
$value = $isArr ? $vars[ $subvar ] : $vars -> $subvar; | |
} else { | |
$isArr = is_array( $value ); | |
if( ( $isArr && isset( $value[ $subvar ] ) ) || ( is_object( $value ) && isset( $value -> $subvar ) ) ) { | |
$value = $isArr ? $value[ $subvar ] : $value -> $subvar; | |
} | |
} | |
} | |
} | |
} else { | |
$value = $vars[ $var ]; | |
} | |
if( empty( $checks[ 2 ][ $key ] ) ) { | |
$replacesIn[] = $checks[ 0 ][ $key ]; | |
$replacesOut[] = $value; | |
} else { | |
if( ( is_array( $value ) && in_array( $checks[ 4 ][ $key ], $value ) ) || $checks[ 3 ][ $key ] == $value ) { | |
$replacesIn[] = $checks[ 0 ][ $key ]; | |
$replacesOut[] = $checks[ 4 ][ $key ]; | |
} else if( ! empty( $value ) ) { | |
$replacesIn[] = $checks[ 0 ][ $key ]; | |
$replacesOut[] = $value; | |
} else { | |
$replacesIn[] = $checks[ 0 ][ $key ]; | |
$replacesOut[] = ''; | |
} | |
} | |
} | |
$view = str_replace( $replacesIn, $replacesOut, $view ); | |
} | |
return $view; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage Example
Mvc Viewer:
Mvc Modal & Controller:
Complate Code & use: