0

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?

3 Answers 3

1

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);
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks Eugene, I took your code and made it into a nice little function called parse_in_scope, with the argument being the file to parse and the return value being $result. Works perfectly, thanks!
PS, what do you think is 'generally wrong' with the snippet? The code is a bit of an encapsulation of a larger problem, but I'd like to hear what you're thinking :^).
For me, the only situation that somehow justifies trying to assign evaluated php/html code to Smarty variable is an attempt to use some 3-rd party code "as-is" :)
That's pretty much what I'm doing, I actually got a bit sidetracked and it may be unnecessary to do so, but it's nice to have this trick in my tool belt.
1

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.