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 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示