获取Smarty模板(传递数据)为字符串

I'm working with a legacy code that uses Smarty. For one of the Ajax requests I need to return HTML code of a sub-template. Hence, I would like to pass a variable to the sub-template and store the processed template as string, so it can be passed back to Ajax callback.

I have no idea how to do that. Smarty fetch() seems to do something like that, but I'm unable to pass $myData into the sub-template.

What I want is this:

$content = $smarty->get_template_as_string('results.tpl', $myData);
return $content;

The get_template_as_string() method does not exist in Smarty, just serves as an example of what I need.

Smarty doesn't take its data as an array, you need to assign each variable individually; luckily, all you need to do this is a simple foreach loop:

    foreach ($variables as $key => $value) {
        $this->assign($key, $value);
    }

Then, as you say, fetch will render the actual template for you:

    $html = $this->fetch($template_file_name);

Obviously you can wrap all of this up in whatever kind of utility function makes sense for you.