silber/client-templates

该包最新版本(v0.1)没有可用的许可证信息。

将客户端模板从单独的文件渲染到应用程序的主要视图中

v0.1 2014-12-03 00:15 UTC

This package is auto-updated.

Last update: 2024-09-04 16:26:02 UTC


README

此包为您提供了一个简单的机制,可以将所有客户端模板视图加载到主应用程序视图中。

什么?为什么?

单页应用程序(SPA)通常通过在客户端渲染模板来工作。对于较大的应用程序,这些模板可能会在需要时异步加载。对于较小的应用程序,在初始页面加载时一次性加载所有模板可能更合理。

由于 HTML 模板 还相当新颖并且 尚未得到广泛支持,大多数框架依赖于在 script 标签中提供模板,并使用一些特殊的 type 属性来标记它是模板。

使用此包,您可以保持模板的良好组织,每个模板都有自己的单独文件。通过一个简单的调用,您可以然后注入所有模板到主应用程序视图中。

文档

这是一个与框架无关的包。它还附带特殊的Laravel集成类,但这些是可选的。

首先通过Composer安装此包。打开您的终端到项目的根目录,并在提示符中输入以下内容

composer require "silber/client-templates:dev-master"

Laravel 集成

Composer安装完成后,您可以添加服务提供者和别名。打开 app/config/app.php,并进行以下更改

  1. providers 数组添加新项
'Silber\Templates\Laravel\TemplatesServiceProvider'
  1. aliases 数组添加新项
'Templates' => 'Silber\Templates\Laravel\TemplatesFacade'

这样就完成了!您已经准备就绪。

用法

现在您已经设置好了,您可以开始创建模板。在 views 目录中,创建一个新的 templates 目录。这就是您将保留所有模板的地方。

以下是一个示例目录结构

A sample template directory structure

在这些文件中的每一个,您将只有该特定模板的HTML。

然后,在您的主 index.blade.php 文件中 - 在关闭的 <body> 标签之前 - 添加以下代码片段

{{ Templates::render('templates') }}

这将自动渲染所有模板,如下所示

<script type="text/ng-template" id="orders/index">
	<!-- ...the contents of the template... -->
</script>
<script type="text/ng-template" id="orders/show">
	<!-- ...the contents of the template... -->
</script>
<script type="text/ng-template" id="products/index">
	<!-- ...the contents of the template... -->
</script>
<script type="text/ng-template" id="products/show">
	<!-- ...the contents of the template... -->
</script>

现在您可以在您喜欢的JS框架中使用这些模板!

配置

如果您想更改包的任何默认选项,您首先需要将配置文件发布到应用程序的 config 目录。您可以通过运行以下Artisan命令来完成此操作

php artisan vendor:publish --provider="Silber\Templates\Laravel\TemplatesServiceProvider"

这将创建一个新的配置文件 config/templates.php。您现在可以编辑此文件以更改默认选项。

模板类型

默认情况下,type 属性设置为 text/ng-template,这是AngularJS使用的。您可以在包的配置文件中更改此设置。例如

'type' => 'text/x-handlebars-template'

去除扩展名

默认情况下,模板文件的扩展名将去除,从HTML的 id 属性中移除。如果您希望将其保留,将其设置为 false

'strip' => false

然后,它将输出带有文件扩展名的模板。例如

<script type="text/ng-template" id="orders/index.html"></script>

排除模式

有时您可能希望排除某些文件或目录,不将其作为自己的模板渲染

'exclude' => '_includes'

视图目录

默认情况下,模板的基本目录在视图目录中。如果您想更改它,您可以通过在这里设置来完成

'views' => public_path('html')

贡献

感谢您考虑贡献!本项目遵循 Laravel 的编码规范

许可协议

client-templates 包是开源软件,遵循 MIT 许可协议