承接 proai/laravel-handlebars 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

proai/laravel-handlebars

Composer 安装命令:

composer require proai/laravel-handlebars

包简介

A Laravel wrapper for LightnCandy for using the Handlebars (and Mustache) template engine.

README 文档

README

Latest Stable Version Total Downloads Latest Unstable Version License

This package allows you to use Handlebars (and Mustache) templates with Laravel. You can integrate Handlebars templates into Blade templates and you can even use the Blade language directives @lang and @choice in Handlebars templates.

It's the perfect choice, if you want to use the same templates in different languages (i. e. PHP and JavaScript) and/or server- and clientside. The compiling and rendering is veeery fast, because this package wraps the super fast template engine LightnCandy.

Installation

Laravel Handlebars is distributed as a composer package. So you first have to add the package to your composer.json file:

  • For Laravel 6+:

    "proai/laravel-handlebars": "^1.14"
  • For Laravel 5.5 to 5.8:

    "proai/laravel-handlebars": "~1.8"
  • For Laravel 5.1 to 5.4:

    "proai/laravel-handlebars": "~1.5.0"

Then you have to run composer update to install the package. Once this is completed, you have to add the service provider to the providers array in config/app.php:

/*
 * Package Service Providers...
 */
ProAI\Handlebars\HandlebarsServiceProvider::class,

You can publish the package configuration with the following command:

php artisan vendor:publish --tag=laravel-handlebars

Usage

Configuration

Most of the options in config/handlebars.php are also used by LightnCandy. So please have a look at the LightnCandy readme for more information.

Only the basedir option can't be set in this config file. Instead the package uses the paths option in config/view.php to define base directories and also the compiled option in the same file to define the directory for the compiled templates (i. e. the cache directory).

In addition to the LightnCandy options there are the options language_helpers, optional_raw_output and translate_raw_output. These options are described below.

Basics

You can use Handlebars templates the same way you use Blade templates. You can return them with View::make('articles', ['name' => 'Taylor']) or include them with the Blade @include directive, i. e. @include('articles', ['name' => 'Taylor']).

By default all views which have a .hbs or .handlebars file extension are automatically detected as Handlebars templates. You can add more file extensions that should be treated as Handlebars templates in the fileext array in config/handlebars.php.

Language Helpers

If you wish, you can use the Blade language directives @lang and @choice in Handlebars templates, too. You have to set $language_helpers = true in order to use them. Here is an example:

// Blade syntax:
@lang('message', ['firstname' => 'John', 'lastname' => $lastname])
@choice('comment_count', 2, ['item' => 'Article'])
// Handlebars syntax:
{{lang 'message' firstname='John' lastname=lastname }}
{{choice 'comment_count' 2 item='Article' }}

Raw Output

This feature is currently broken. If you want to use it, use v1.1 or below or help to fix it!

If you want to output the raw code of a template (maybe because you want to use the unrendered template clientside), you can set $optional_raw_output = true in the configuration. Then you can pass a variable $raw = true to the template or more comfortable you can use the @raw Blade directive.

// Passing the $raw variable to the view:
View::make('articles', ['raw' => true])
@include('articles', ['raw' => true])
// Blade @raw directive:
@raw('articles')

If you want to output a raw template with compiled and rendered language variables, you can set $translate_raw_output = true.

Partials

This package automatically adds the directory of the current template to the basedir of LightnCandy. By that it is possible to easily include other Handlebars templates in the same directory. Just write {{> comment}} to include comment.hbs from the same directory.

Example Template

{{#each array_variable }}
	{{#if this }}
		{{ output_some_variable }} {{> include_templatename }}
	{{else}}
		{{lang 'language_variable' }}
	{{/if}}
{{/each}}

For more information about the Handlebars syntax see the Handlebars documentation. It does not matter that the examples are for JavaScript, because Handlebars templates are the same for JavaScript and PHP.

Using it with Webpack

If you want to use this package client side with webpack, have a look at this article:

Sharing templates between PHP and JavaScript in Laravel

Support

Bugs and feature requests are tracked on GitHub.

License

This package is released under the MIT License.

proai/laravel-handlebars 适用场景与选型建议

proai/laravel-handlebars 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 207.48k 次下载、GitHub Stars 达 38, 最近一次更新时间为 2015 年 06 月 21 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「mustache」 「template engine」 「laravel」 「handlebars」 「lightncandy」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 proai/laravel-handlebars 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 proai/laravel-handlebars 我们能提供哪些服务?
定制开发 / 二次开发

基于 proai/laravel-handlebars 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 207.48k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 38
  • 点击次数: 17
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 38
  • Watchers: 6
  • Forks: 19
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-06-21