dongyuqie4322 2014-05-05 12:35
浏览 55
已采纳

smarty如何从内存中包含文件并分配所有父模板变量?

I have the following function that loads a template from a file:

public function renderText($text, $data=[], $return=false) {
    $template = $this->smarty->createTemplate('string: '.$text, null, null, $data);
    if($return)
        return $template->fetch();
    else
        $template->display();
}

And I load the template with the needed variables using

$this->renderText(Yii::app()->smarty->renderText($object->readData(), ['contract'=>$contract], true));

This works perfect for my situation. However, I need to extend this function, so that I can load another template inside this template from memory. Something like {{include}} function but the one that's specific for my application and is loaded from memory. So far I have this function include_content:

$template = Object::model()->findByAttributes(['name'=>$params['template']]);
echo $template->readData();

But this doesn't read the variables, just prints them like a string: So if I have two templates:

Main: {{include_content template="SomeBody"}}

SomeBody: {{$contract->id}}

If I open "Main", I need it to print the contract id, not the string "{{$contract->id}}",

I have tried using the renderText function again, but that function doesn't add the parent template's variables in it. What can I do to achieve that?

Thank you,

  • 写回答

1条回答 默认 最新

  • doufu3718 2014-05-05 12:56
    关注

    Maybe you need fetch function? It lets you to parse smarty template and set output to variable. In Smarty manual: http://www.smarty.net/docs/en/api.fetch.tpl

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端