简体   繁体   中英

PHP + Smarty: Parse PHP+HTML into a String?

I am using PHP in combination with Smarty Templates to generate pages serverside. Currently, I am loading a page as follows:

$smarty->assign('app', file_get_contents("some_content.php"));

Where some content contains HTML with PHP tags and code inside those tags.

I would like the PHP content inside this file within the current scope (That of the script reading the file), so that a particular function I've defined is available. How would I go about doing so? All the information I can find is regarding the eval(...) function, which doesn't seem to be able to cope with the HTML/PHP mixture: would I need to perform a find/eval/replace operation to achieve the desired result, or is there a more elegant way of doing this?

Ich, I'm such a dummkopf. There is an answer right on the PHP manual for eval, right under my nose. Here is the answer I neglected to notice .

From my opinion, this short snippet of the code you posted shows that something is generally wrong there :)

But nevertheless you can achieve whatever you are trying to achieve by doing the following:

ob_start();
include("some_content.php");
$result = ob_get_clean();
$smarty->assign('app', $result);

You can use {literal}...{/literal} smarty tags to display any content in smarty templates as is. It used to transfer java scripts and other specific content.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM