file_get_contents not replacing PHP variables by actual values?

Well in an AJAX script, I am trying to use file_get_contents to load a smarty template .tpl file, the code is shown below:

<table>
    <tr>
        <td width='100'><p><img src='/{$post->getUser()->getAvatar()}' width='80' height='80'></p></td>
        <td>
            {$post->getUser()->getFullName()}<br>
            {$post->getTitle()}<br>
            {$post->getDatePosted()}
        </td>
    </tr>
</table>
<table>
    <tr>
        <td>{$post->getContent()}</td>
    </tr>
</table>

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?

[FPHP]require[/FPHP].

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.

Alternatively, templating.

Why don’t you use Smarty’s render method instead of file_get_contents?

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.

This doesn’t make sense for me, because Smarty template is supposed to be rendered with Smarty, and type of request shouldn’t matter here.

If you won’t use Smarty anyway you can solve it this way:

$tplCode = file_get_contents('mytemplate.tpl');
$phpCode = str_replace(['{', '}'], ['<?php ', ' ?>'], $tplCode);
$resultHtml = eval($phpCode);    

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.

I see, it looks interesting and I will try that. And btw, if I create a smarty object and call render on ‘mytemplate.tpl’, will it do the same?

Sure. It will do it even better.

So which smarty method I should call, for loading a template file to string? Is it Smarty::fetch($template)?

Yes. You can check the docs

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.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.