在包含它之前处理PHP文件

I'm developing a PHP templating engine1 and I want to allow PHP syntax alongside the templating syntax. My process currently is to compile the template into a php file (convert all template syntax to PHP), store it to disk and then load it using include2. The compilation will only take place if the original template has been modified or if no compilation has ever been made.

However, there are a few issues with this approach:

  1. I cannot be sure whether I have enough permissions to create new files
  2. Creating and saving files is a costly process

Is there another approach to this?

For example, the following code:

<ul>
    <?php forach( $list as $item ): ?> 
        <li>{{item}}</li>
    <?php endforeach; ?>
</ul>

Would be compiled to:

<ul>
    <?php forach( $list as $item ): ?> 
        <li><?php echo $item ?></li>
    <?php endforeach; ?>
</ul>


[1] I know that there are hundreds of those, but this one is suppose to be the answer to special cases. The goal is to enhance PHP's built in templating engine, rather than replace it altogether like other engines.
[2] Note that I do not want to use eval for this as it is both costly and dangerous.