Skip to content
This repository has been archived by the owner on Aug 13, 2020. It is now read-only.

Commit

Permalink
Cleaned up regex
Browse files Browse the repository at this point in the history
  • Loading branch information
josephspurrier committed May 6, 2014
1 parent 7fa8993 commit fbd6fd0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,21 @@ If you are looking for a lightweight template engine to use with PHP, please
fork the project and add your own customizations. I'd love you see what you
come up with.

# Support Control Structures Syntax

This is the syntax for the control structures.

```
{if},{elseif},{else},{end}
{for},{endfor}
{foreach},{endforeach}
{while},{endwhile}
```



# Render a string

Create an instance of the class, assign a few variables, and then render the string.
Expand Down
17 changes: 9 additions & 8 deletions src/SurfStack/Templating/Template_Engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ protected function stripTags($content)
//$stripTags[] = '/'.$stripSpace.'\{\*(.[^\}\{]*?)\*\}'.$stripSpace.'/s';

// Strip section tags and all space around
$stripTags[] = '/\s*\{\s*(section)\s*(.*?)\}(.*?)\{\/\s*section\s*\}\s*/si';
$stripTags[] = '/\s*\{(section)\s*(.*?)\}(.*?)\{\/section\s*\}\s*/si';

return preg_replace($stripTags, '', $content);
}
Expand Down Expand Up @@ -681,7 +681,7 @@ protected function updateEmbeddedFiles($content)
'require',
) as $c)
{
$regex['<?php '.$c.' $1; ?>'] = '/\{\s*'.$c.'\s+(.*?)\}/';
$regex['<?php '.$c.' $1; ?>'] = '/\{'.$c.'\s+(.*?)\}/';
}

// Embed files into templates
Expand Down Expand Up @@ -956,7 +956,7 @@ protected function modifyTemplateRegex($content)
'declare',
) as $c)
{
$regex['/\{\s*'.$c.'\s*(.*?)\}/'] = '<?php '.$c.' $1: ?>';
$regex['/\{'.$c.'\s*(.*?)\}/'] = '<?php '.$c.' $1: ?>';
}

// Replace bottoms (semicolons)
Expand All @@ -969,20 +969,21 @@ protected function modifyTemplateRegex($content)
'endswitch',
'break',
'continue',
'dowhile',
) as $c)
{
$regex['/\{\s*'.$c.'\s*(.*?)\}/'] = '<?php '.$c.'$1; ?>';
$regex['/\{'.$c.'\s*(.*?)\}/'] = '<?php '.$c.'$1; ?>';
}

// Replace the outliers
$custom = array(
'/\{\*(.[^\}\{]*?)\*\}/s' => '/*$1*/',
'/\{=e\s*(.*?)\}/' => '<?php echo htmlentities($1, ENT_QUOTES, "UTF-8"); ?>',
'/\{=\s*(.*?)\}/' => '<?php echo $1; ?>',
'/\{\s*case\s*(.*?)\}/' => '<?php case $1: ?>',
'/\{\s*switch\s*(.[^'."\r\n".']*?)'."\r\n".'(.*?)\}/' => '<?php switch $1:'."\r\n".'$2: ?>',
'/\{\s*switch\s*(.[^'."\n".']*?)'."\n".'(.*?)\}/' => '<?php switch $1:'."\n".'$2: ?>',
'/\{\s*\$\s*(.*?)\}/' => '<?php $$1; ?>',
'/\{case\s*(.*?)\}/' => '<?php case $1: ?>',
'/\{switch\s*(.[^'."\r\n".']*?)'."\r\n".'(.*?)\}/' => '<?php switch $1:'."\r\n".'$2: ?>',
'/\{switch\s*(.[^'."\n".']*?)'."\n".'(.*?)\}/' => '<?php switch $1:'."\n".'$2: ?>',
'/\{\$\s*(.*?)\}/' => '<?php $$1; ?>',
);

// Replace the { tags with PHP tags
Expand Down

0 comments on commit fbd6fd0

Please sign in to comment.