Skip to content

Instantly share code, notes, and snippets.

@Mosharush
Created August 9, 2017 23:44
Show Gist options
  • Save Mosharush/ec645c150757257e4461898f938a87a0 to your computer and use it in GitHub Desktop.
Save Mosharush/ec645c150757257e4461898f938a87a0 to your computer and use it in GitHub Desktop.
PHP function to replace MVC Viewer to end result
<?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;
}
@Mosharush
Copy link
Author

Usage Example

Mvc Viewer:

<!DOCTYPE HTML>
<html>
    <head>
        <title>{{var}}</title>
    </head>
    <body>
        Hello!
        <br/>
        My favorite number is {arr.1}.
        <br/>
        And my name is {foo.bar()}, oopss my name is {foo.bar(Moshe)}.
        <br/>
        If $num var is 5 you will see here ok in bold: {num==5?<b>ok</b>}
    </body>
</html>

Mvc Modal & Controller:

<?php
    $var = 'dynamic title';
    $num = 5;
    $arr = array('one', 'two', 'three');
    
    class foo
    {
        public $var = 'foo content';
        
        public function bar($str = 'without param')
        {
            return $str;
        }
    }
    $foo = new foo();  

Complate Code & use:

 <?php
include 'procMvc.function.php';

$view = <<<HTML
<!DOCTYPE HTML>
<html>
    <head>
        <title>{{var}}</title>
    </head>
    <body>
        Hello!
        <br/>
        My favorite number is {arr.1}.
        <br/>
        And my name is {foo.bar()}, oopss my name is {foo.bar(Moshe)}.
        <br/>
        If $num var is 5 you will see here ok in bold: {num==5?<b>ok</b>}
    </body>
</html>
HTML;


    $var = 'dynamic title';
    $num = 5;
    $arr = array('one', 'two', 'three');
    
    class foo
    {
        public $var = 'foo content';
        
        public function bar($str = 'without param')
        {
            return $str;
        }
    }
    $foo = new foo();  


// Get the result:
$result = procMvc( $view );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment