Read php tags inside a non-php file and execute

蓝咒 提交于 2020-01-30 06:27:24

问题


What I'm trying to achieve is build a simple and lightweight theme engine, where anyone will be able to create his own template easily, I have managed to do this using PHP, and for example, if someone wants to input the logo, all they have to do is use: {logo} , and etc.

The template files extensions are '.inc'.. My problem is, i don't know how to allow users to still execute php code, using PHP (< ?php ?>) tags.

I have tried both adding this in my htaccess:

AddType application/x-httpd-php .inc

I know other template engines exist, such as smarty, but id rather go for something a lot more simpler.


回答1:


Not too many people will be happy to use a template engine which requires adding new web server handlers to use it.

Therefore it would be advisable to use a standard fully supported extension like .php instead of .inc or in case you are really willing to use .inc simply include the file with output buffering turned on and collect the output in a variable like this:

ob_start();
require "template.inc";
$content = ob_get_clean();

$content will hold whatever was inside template.inc so you can do whatever you want with it.

Not only that but if your template.inc file contains PHP variables they will be parsed too.




回答2:


It has nothing to do with htaccess rules. You need to evaluate PHP content right after you finished interpreting your template tags. For instance:

eval( "?> $content_of_template <?php " );


来源:https://stackoverflow.com/questions/6724048/read-php-tags-inside-a-non-php-file-and-execute

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!