FatFree 模板中的 PHP 代码

发布于 2025-01-06 03:27:57 字数 656 浏览 2 评论 0原文

我正在尝试使用 FatFree 框架并尝试使用模板引擎。我使用以下代码渲染模板 -

echo Template::serve('template.php');

我面临的问题是,在 template.php 文件中,F3 标签被识别,但任何 PHP 代码都不起作用。例如,如果我在 template.php 文件中有以下代码 -

<?php
if (F3::get('var') == 'var1') {
   ?>
   <span>var1 is present</span>
   <?php
} else {
   ?>
   <span>var1 not present</span>
   <?php
}
?>

无论 var 的值如何,都会打印 var1 is presentvar1 not present 。另外,php for 循环不起作用 - 所以基本上所有 php 代码都不起作用。

但是,如果我使用 编写上述 PHP 代码,则一切正常。我们可以不在模板中使用 PHP 代码吗?如果是这样的话,这是一个严重的限制。

I am trying to work with FatFree framework and trying to use the template engine. I render the template with the following code -

echo Template::serve('template.php');

The problem which I'm facing is that, inside the template.php file the F3 tags are recognised but any PHP code doesn't work. For instance, if I have the following code in the template.php file -

<?php
if (F3::get('var') == 'var1') {
   ?>
   <span>var1 is present</span>
   <?php
} else {
   ?>
   <span>var1 not present</span>
   <?php
}
?>

Here both var1 is present and var1 not present is printed irrespective of the value of var. Also, php for loops are not working - so basically all the php code is not working.

However, if I used <F3:check> to write the above PHP code, then everything works fine. Can we not use PHP code in templates. If this is the case, this is a serious limitation.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

茶底世界 2025-01-13 03:27:57

我已经找到了答案,尽管我不太喜欢它。

有两个不同的函数,F3::render()Template::serve()

使用 F3::render() 您可以评估PHP 表达式并使用 F3::get() 检索变量。根据该网站的说法:“在模板中嵌入 PHP 代码的唯一问题是需要有意识地努力遵守 MVC 原则”

Template::serve() 适用于仅模板化。这意味着它只是处理模板语言。

所以基本上,是的,它很糟糕并且没有意义,您可以在 F3::render() 中评估 PHP 代码,并且不能使用模板变量 ({{@var }}) -或者- 您可以使用 Template::serve() 并且您只能调用 PHP 函数,而不能真正评估 PHP 代码。

I have found the answer, although I don't really like it.

There is two different functions, F3::render() and Template::serve()

With F3::render() you can evaluate PHP expressions and use the F3::get() to retrieve variables. According to the website: "The only issue with embedding PHP code in your templates is the conscious effort needed to stick to MVC principles"

The Template::serve() is for templating only. Meaning its simply to process the templating language.

So basically, and yes it sucks and doesn't make sense, you can evaluate PHP code in the F3::render() and you can't use templating variables ({{@var}}) -OR- you can use Template::serve() and you are limited to only calling PHP functions, and not truly evaluating PHP code.

一人独醉 2025-01-13 03:27:57

它没有文档记录,但您可以将代码放在模板中的 {~ ~} 内,当模板编译时,它将转换为 (使用v3.6)。

例如 {~ @color = 'red' ~} 将变为

It is undocumented but you can put code within {~ ~} in a template and it will be converted to <?php ?> when the template is compiled (using v3.6).

e.g. {~ @color = 'red' ~} will become <?php $color = 'red' ?>

请持续率性 2025-01-13 03:27:57

也许尝试使用不同的模板引擎,这将允许您更轻松地定义块变量依赖项?

例如在 PHPTal http://phptal.org/manual/en/split/tal -condition.html 你可以这样做:

<div tal:condition="php: var == 'var1'">
....
</div>

Maybe try to use different template engine which will allow you define easier the blocks variable dependency?

For example in PHPTal http://phptal.org/manual/en/split/tal-condition.html you can do it like that:

<div tal:condition="php: var == 'var1'">
....
</div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文