The idea is that I want to load this smarty .tpl file to a PHP string using file_get_contents(), and I thought it would replace the {$post} with the actual value, just like when you write a PHP string using curly braces in double quotes. However, it wont do the trick, as file_get_contents() does not replace $post by the appropriate value. What can I do in this case? Is there a way for file_get_content() to work with dynamic string content?
You could eval() it, but that’s a rather big security hole. It really is better to include/require the code and explicitly put echoing of variables into it.
because it’s an AJAX request, I just want to update a certain element/div on the html page after the backend PHP code executes. The PHP code will use file_get_contents() to retrieve the text from smarty template file in string format, and this value is passed down to javascript/JQuery to update the element/div. There’s no smarty rendering here, its just a smarty template that is being reused in both the original request and AJAX request. The former is rendered properly by Smarty, the latter has to be retrieved and processed in another way, as theres no Smarty in AJAX request.
But actually this code tries to emulate Smarty renderer and I don’t know why do you want to write your own Smarty when there is original one exists already.
Well there is actually a downside of using smarty’s approach, as I have to manually assign the template variables $post to smarty by calling Smarty::assign(). But it’s okay, a small price to pay anyway, thanks for your suggestion.
Well, otherwise you’d be using PHP templates to generate a smarty template, which you then don’t pass through smarty… So basically you just want to use PHP and not smarty. :-p
Yeah thats clearly an alternative too. But the reason why I am loading a smarty template is that I want it to be reusable by both the actual smarty template engine, and the AJAX request. It may seem strange, but I actually did find a way to get it done.