有没有像 Twig 这样使用 PHP 语法的模板系统?

发布于 2024-12-27 19:11:50 字数 274 浏览 2 评论 0原文

我想使用像 Twig 这样的模板系统(特别是块功能),但使用纯 PHP。

例如,我发现 http://phpsavant.com/docs/ 但似乎不支持块或类似的东西。

编辑 我发现一些似乎具有常规 PHP 代码的块语法: http://phpti.com/

I would like to use a template system like Twig (specifically the block functionality) but in plain PHP.

For example, I found http://phpsavant.com/docs/ but it seems that doesn't support blocks or anything similar.

Edit
I found something that appears to have block syntax with regular PHP code: http://phpti.com/

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

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

发布评论

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

评论(5

温柔嚣张 2025-01-03 19:11:50

您正在寻找的模板语言称为 PHP!

The templating language you are looking for is called PHP!

岁月如刀 2025-01-03 19:11:50

Laravel 框架中包含一个名为 Blade

您可以将普通的旧 PHP 与 Blade 模板语法混合使用,其中 {{...}} 也可转换为 >

也有你在 Twig 中知道的块,但它们被称为

@section('heading')
    {{ strtoupper("I'm not shouting") }}
@show

<?= strtolower('Shhh!'); ?>

这是在 Illuminate\View 命名空间下 - 参见在 GitHub 上,并且可以使用 Composer 下载,因为它也在 Packagist 上注册 -就在下面给你的项目的composer.json。

{
    "require": {
        "illuminate/view": "4.*"
    }
}

我目前不确定您将如何尝试从自定义项目渲染模板。如果我发现了,我会更新我的答案。

There is one included in the Laravel framework called Blade.

You can mix plain old PHP with the Blade templating syntax, where {{...}} also translates to <?=...?> or <?php echo ... ?>

Also has blocks you know in Twig, but they are called sections.

@section('heading')
    {{ strtoupper("I'm not shouting") }}
@show

<?= strtolower('Shhh!'); ?>

This is under the Illuminate\View namespace - See on GitHub, and can be downloaded with Composer as it is also registered on Packagist - just at the following to your project's composer.json.

{
    "require": {
        "illuminate/view": "4.*"
    }
}

I'm not certain at this point about how you'd attempt to render a template from a custom project. If I find out, I'll update my answer.

眼睛会笑 2025-01-03 19:11:50

Symfony(默认使用 Twig)也有一个基于 php 的模板系统,其工作方式与 Twig 非常相似。它具有模板继承并使用“槽”,相当于 Twig 的块。它是一个独立的库,可以在完整的 Symfony 框架之外使用。

http://symfony.com/doc/2.0/cookbook/templated/PHP.html

Symfony (which uses Twig by default) also has a php-based template system that works very much like Twig. It has template inheritance and uses 'slots', which are equivalent to Twig's blocks. It's a stand-alone library that can be used outside of the full Symfony framework.

http://symfony.com/doc/2.0/cookbook/templating/PHP.html

橘虞初梦 2025-01-03 19:11:50

PHP 是一种模板语言(是的),块可以用缓冲区来实现:

<?php
ob_start();
?>
  this the content of the block for <?= date("Y-m-d") ?>
<?php
$content = ob_get_clean();

然后在主布局中回显块的内容:

echo $head;
echo $content ;
// ....

事实上,这就是大多数模板引擎正在使用的。

使用模板库有巨大的优势(缓存、默认转义等......)

PHP is a templating language(yes it is) , blocks can be implemented with buffers :

<?php
ob_start();
?>
  this the content of the block for <?= date("Y-m-d") ?>
<?php
$content = ob_get_clean();

then in the main layout echo the content of the blocks :

echo $head;
echo $content ;
// ....

in fact that is what most template engines are using.

using template libs have huge advantages though ( caching , escaping by default ,etc ... )

尸血腥色 2025-01-03 19:11:50

据我了解,在 Laravel 中,对视图使用 Blade 是可选的。您可以使用像 view.php 这样的文件名来代替 view.blade.php 并在那里使用普通的旧 PHP 语法。唯一的问题是你不能同时拥有 view.phpview.blade.php 因为它们都会响应

return View::make('view);

As far as I understand use off Blade for views is optional in Laravel. You can use file name like view.php instead of view.blade.php and use plain old PHP syntax there. Only catch is you cannot have both view.php and view.blade.php as they both respond to

return View::make('view);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文