Created
July 17, 2010 18:36
-
-
Save katanacrimson/479735 to your computer and use it in GitHub Desktop.
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 | |
// --- standard template vars | |
$buffer = '{ $data } some test data { $test } { $test_var } { $test_var-value }'; | |
echo 'TEMPLATE: ' . PHP_EOL . $buffer . PHP_EOL . PHP_EOL; | |
$buffer = preg_replace('#\{ \$([a-zA-Z0-9_]*) \}#', '<?php $t->fV(\'$1\'); ?>', $buffer); | |
echo 'RAW PHP: ' . PHP_EOL . cleanBuffer($buffer) . PHP_EOL . PHP_EOL; | |
// --- blocks | |
$buffer = '{ start block_name as @alias } blah blah blah { @alias:var } { $normal_var } zomgblaaaaaaaaaarrrrgh { end @alias }'; | |
echo 'TEMPLATE: ' . PHP_EOL . $buffer . PHP_EOL . PHP_EOL; | |
$buffer = preg_replace(array('#\{ start ([a-zA-Z0-9_]*) as @([a-zA-Z0-9_]*) \}#', '#\{ end @([a-zA-Z0-9_]*) \}#', '#\{ @([a-zA-Z0-9_]*)\:([a-zA-Z0-9_]*) \}#'), array('<?php $t->sB(\'$1\'); while(($$2 = $t->fB(\'$1\')) !== false): ?>', '<?php endwhile; ?>', '<?php (if(isset($$1[\'$2\'])) ? $$1[\'$2\'] : NULL) ?>'), $buffer); | |
$buffer = preg_replace('#\{ \$([a-zA-Z0-9_]*) \}#', '<?php $t->fV(\'$1\'); ?>', $buffer); | |
echo 'RAW PHP: ' . PHP_EOL . cleanBuffer($buffer) . PHP_EOL . PHP_EOL; | |
// --- if statements | |
$buffer = '{ if $test_var == \'string\' } some variable { endif } { if @alias:var == 3 } cake { endif }'; | |
echo 'TEMPLATE: ' . PHP_EOL . $buffer . PHP_EOL . PHP_EOL; | |
// requires multiple passes | |
$buffer = preg_replace(array('#\{ if ([\$a-zA-Z0-9_\'\"@:]*) (\=\=|\!\=) ([\$a-zA-Z0-9_\'\"@:]*) \}#', '#\{ endif \}#'), array('<?php if(%$1% $2 %$3%): ?>', '<?php endif; ?>'), $buffer); | |
$buffer = preg_replace(array('#\%@([a-zA-Z0-9_]*)\:([a-zA-Z0-9_]*)\%#', '#\%\$([a-zA-Z0-9_]*)\%#', '#\%([a-zA-Z0-9_\'\"]*)\%#'), array('(if(isset($$1[\'$2\'])) ? $$1[\'$2\'] : NULL)', '$t->fV(\'$1\')', '$1'), $buffer); | |
echo 'RAW PHP: ' . PHP_EOL . cleanBuffer($buffer) . PHP_EOL . PHP_EOL; | |
function cleanBuffer($buffer) | |
{ | |
return $buffer; | |
/*return str_replace('?> <?php ', '', $buffer);*/ | |
} |
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
obsidian@lithion-mint ~/Documents $ php RegexTest.php | |
TEMPLATE: | |
{ $data } some test data { $test } { $test_var } { $test_var-value } | |
RAW PHP: | |
<?php $t->fV('data'); ?> some test data <?php $t->fV('test'); ?> <?php $t->fV('test_var'); ?> { $test_var-value } | |
TEMPLATE: | |
{ start block_name as @alias } blah blah blah { @alias:var } { $normal_var } zomgblaaaaaaaaaarrrrgh { end @alias } | |
RAW PHP: | |
<?php $t->sB('block_name'); while(($alias = $t->fB('block_name')) !== false): ?> blah blah blah <?php (if(isset($alias['var'])) ? $alias['var'] : NULL) ?> <?php $t->fV('normal_var'); ?> zomgblaaaaaaaaaarrrrgh <?php endwhile; ?> | |
TEMPLATE: | |
{ if $test_var == 'string' } some variable { endif } { if @alias:var == 3 } cake { endif } | |
RAW PHP: | |
<?php if($t->fV('test_var') == 'string'): ?> some variable <?php endif; ?> <?php if((if(isset($alias['var'])) ? $alias['var'] : NULL) == 3): ?> cake <?php endif; ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment