定制 splashsky/modello 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

splashsky/modello

Composer 安装命令:

composer require splashsky/modello

包简介

A simple, lightweight template compiler

README 文档

README

Modello - simple, lightweight template compiler

Modello is a super-simple, super-lightweight templating engine written in PHP. It's purpose is to be a standalone class that can be included in any project and used to quickly and efficiently parse/compile a template.

Contributing

While I don't intend for this to become a replacement for other templating solutions, I felt it would be fun to make a simple little compiler for small projects. The goal is ease of use and compactness, and hopefully my work reflects that.

If you have ideas, or want to contribute some code, feel more than welcome to open an Issue or a Pull Request. Thanks for dropping by!

Getting Started

The easiest way of using Modello in your project is Composer.

composer require splashsky/modello

Otherwise, you can download the .zip or clone the project using Git. After that, move the Modello.php class file to wherever you want!

How do I start up the compiler?

Modello is easy to get started with.

// require() and/or 'use' Modello, depending on your environment
$modello = new Modello('path/to/views', 'path/to/cache');

The first argument for the constructor tells Modello where your template directory is, and this will be the root from which Modello looks for template files. The second argument tells Modello where to create the compiled views.

$modello->setViews(string $viewPath) and $modello->setCache(string $cachePath) are available to change the views and cache directories at runtime.

Using $modello->setExtension(string $extension) allows you to change the extension Modello looks for on template files - by default this is .mllo.php.

You can use $modello->setCacheEnabled(bool $enabled) to enable or disable caching - disabled means that Modello will re-compile the view on every render.

How do I compile a template?

Simply call $modello->view()!

/**
 * File Directory
 * | cache/views/
 *   example.php (will be created at render)
 * | views/
 *   example.mllo.php
*/

echo $modello->view('example', ['foo' => 'bar']);

When telling Modello what template to render, ensure you're not adding the extension - only use the name of the template. Modello will use whatever extension it has set when it looks for your file. You can use dot notation as well, so 'foo.bar' is as valid as 'foo/bar'.

The second argument in the view() function is your data array - these key => value pairs will be extracted into the resulting template.

When a template is compiled for the first time - or if Modello detects the original template file has changed - it will generate a newly compiled version of the template and store it in the cached directory for faster subsequent renders.

What syntax can I use in the template?

Modello uses template syntax very similar (basically identical) to Laravel Blade. Here's the directives it currently supports:

// This is the echo syntax
{{ $foo }}
{{ bar() }}

// This is the if-else syntax
@if(condition)
    // ...
@elseif(condition)
    // ...
@else
    // ...
@endif

// This is the foreach syntax
@foreach($array as $key => $value)
    {{ $key }} equals {{ $value }}
@endforeach

// This is a comment
{{-- I won't show up in the HTML or in the compiled template file! --}}

// This is the almighty include directive!
@include('path.to.template')

// We also support blocks and yielding
@block('foo')
    something here
@endblock

@yield('foo') // Outputs: something here

// Add block conditionals
@hasblock('foo')
if block foo exists, this will be output!
@endif

@blockmissing('bar')
if the bar block doesn't exist, this will be put out!
@endif

As long as the data you provide the directives is valid PHP, it will work!

For the include directive, it will look for the template you specify relative to the template directory you gave to Modello. If it cannot find the template you specify, it will return "path/to/template.php could not be found", which will show up in your HTML.

What if I just need to parse?

Modello still has the classic parsing functionality from before - in the static method parse().

echo Modello::parse('Hello, {{ name }}!', ['name' => 'Jerry']);

Of course, you won't have access to any of the directives or other features of a compiled template.

License

Modello is completely free, open source software. It's covered under the MIT license, and you can read the details in the license.md.

splashsky/modello 适用场景与选型建议

splashsky/modello 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 20 次下载、GitHub Stars 达 15, 最近一次更新时间为 2020 年 09 月 02 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 splashsky/modello 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 20
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 15
  • 点击次数: 4
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 15
  • Watchers: 2
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-09-02