smarty how to include file from memory and assign all the parent template variables?

StackOverflow https://stackoverflow.com/questions/23472624

  •  15-07-2023
  •  | 
  •  

Вопрос

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,

Это было полезно?

Решение

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

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top