Fat Free Framework 3 呈现任意 HTML(不是来自文件)
Fat Free Framework 3 render arbitrary HTML (not from files)
我想知道如果 HTML 代码部分不在实际的 .html 文件中,是否有可能以某种方式渲染它。
你经常这样做:
$template = new Template;
echo $template->render('whatever.html');
但我真的很想做:
$template = new Template;
$rendered_content = $template->render($my_html_code);
原因是:我已经创建了几个电子邮件模板,如果我可以使用 F3 模板引擎替换我放入其中的占位符变量,并准备好最终处理的 HTML 代码,那就太好了将通过电子邮件发送。
我知道我可能会将模板转储到临时 .html 文件,但这对我来说听起来有点难看,除非没有其他可用选项,否则我宁愿不这样做。
您正在寻找 resolve()
方法:
$tpl = new Template;
$rendered_content = $tpl->resolve($my_html_code);
这种方法足以替代"placeholder variables",例如My name is {{ @name }}
。
如果你还打算使用模板标签(<repeat>
、<check>
等),你需要先parse()
字符串,然后将结果传递给resolve()
。例如:
$tpl = new Template;
$rendered_content = $tpl->resolve($tpl->parse($my_html_code));
我想知道如果 HTML 代码部分不在实际的 .html 文件中,是否有可能以某种方式渲染它。
你经常这样做:
$template = new Template;
echo $template->render('whatever.html');
但我真的很想做:
$template = new Template;
$rendered_content = $template->render($my_html_code);
原因是:我已经创建了几个电子邮件模板,如果我可以使用 F3 模板引擎替换我放入其中的占位符变量,并准备好最终处理的 HTML 代码,那就太好了将通过电子邮件发送。
我知道我可能会将模板转储到临时 .html 文件,但这对我来说听起来有点难看,除非没有其他可用选项,否则我宁愿不这样做。
您正在寻找 resolve()
方法:
$tpl = new Template;
$rendered_content = $tpl->resolve($my_html_code);
这种方法足以替代"placeholder variables",例如My name is {{ @name }}
。
如果你还打算使用模板标签(<repeat>
、<check>
等),你需要先parse()
字符串,然后将结果传递给resolve()
。例如:
$tpl = new Template;
$rendered_content = $tpl->resolve($tpl->parse($my_html_code));