返回介绍

Views and Blade

发布于 2024-06-22 20:04:58 字数 4704 浏览 0 评论 0 收藏 0

Although the Flarum UI you know and love is powered by our Mithril frontend, server-side generated templates are still used throughout Flarum. Most notably, the HTML skeleton of the forum, which includes various SEO meta tags, as well as the no-js view of the forum, is implemented through the Views and Blade systems.

Blade is Laravel's templating engine, which allows you to conveniently generate HTML (or other static content) from PHP. It's the same idea as Jinja or EJS. Views are Laravel's system for organizing/registering Blade templates, and also includes utilities for rendering them and providing them with variables.

For our purposes, views are directories containing .blade.php template files (possibly contained in subdirectories).

Adding Views

You will need to tell the view factory where it can find your extension's view files by adding a View extender to extend.php:

use Flarum\Extend;
use Illuminate\Contracts\View\Factory;

return [
    (new Extend\View)
        ->namespace('acme.hello-world', __DIR__.'/views'),
];

Blade Templates

To learn about the syntax for Blade templates, read Laravel's documentation.

Once you've set up your views, you can render them to strings:

// Here, $view is of type `Illuminate\Contracts\View\Factory`
$renderedString = $view->make('acme.hello-world::greeting')->render();

// You can also pass variables to the view:
$renderedString = $view->make('acme.hello-world::greeting', ['varName' => true])->render();

You can obtain the view factory instance through dependency injection.

The format is "VIEW_NAMESPACE::VIEW_NAME". If the view folder is organized as subdirectories, replace / with . in the pack. So if you have a file at "forum/error.blade.php" in a namespace called "custom-views", you would use "custom-views::forum.error".

Note that all Blade templates rendered this way automatically have access to the following variables:

Additionally, templates used by content logic have access to $forum, which represents the Forum API Document's attributes.

Overriding Views

If you want to override templates added by core or extensions, set up a view folder structure matching the one you are trying to override, containing just the files you are trying to override. Then use the View extender's extendNamespace method:

use Flarum\Extend;
use Illuminate\Contracts\View\Factory;

return [
    (new Extend\View)
        ->extendNamespace('acme.hello-world', __DIR__.'/override_views');
];

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文