wonderboymusic/mustache-css-modules 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

wonderboymusic/mustache-css-modules

Composer 安装命令:

composer require wonderboymusic/mustache-css-modules

包简介

Use CSS Modules in Mustache templates.

README 文档

README

Use CSS Modules in mustache templates

The example folder contains an example implementation.

Install

$ composer require wonderboymusic/mustache-css-modules

Move the contents of the js/ folder in this repository to the root of your project, then:

$ npm i

Environment

You must set MUSTACHE_CSS_DIR in your application's environment. The value will be used to look for Mustache templates and the SCSS files that reside next to them.

JavaScript files check for this directory relative to project root:

const cssDir = path.join(__dirname, process.env.MUSTACHE_CSS_DIR);

PHP files look for the absolute path the folder containing your Mustache templates:

$cssDir = getenv('MUSTACHE_CSS_DIR');

Before autoloading, you can set this value directly in your bootstrap file:

putenv('MUSTACHE_CSS_DIR=' . __DIR__ . '/src/templates');

require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/src/app.php';

Usage

Let's say you have a bunch of Mustache templates in a folder named src/templates. If you have a footer.mustache file, add a footer.scss file in the same folder.

src/templates/footer.mustache
src/templates/footer.scss

Styles declared in footer.scss will be scoped to footer.mustache, so you don't need to worry about global name collision. All of the class names you declare can be available to your view logic, and will be unique, so you also don't need to worry about the CSS cascade or nesting selectors.

First, declare your styles:

/* footer.scss */

.wrap {
  display: block;
  margin: 0 auto;
  width: 780px;
}

Use the value of .wrap inside of your Mustache template:

<footer class="{{ css.footer.wrap }}">Copyright &copy; 2019</footer>

Use footer as a template in a Mustache file (e.g. home.mustache):

<!doctype html>
<html>
<head>
  <title>Mustache + CSS Modules = 🤯</title>
</head>
<body>
  <main class="{{ css.main.wrapper }}">
  Some body content on a page that has a footer.
  </main>
  {{> footer }}
</body>
</html>

Before all of this will actually work, your PHP view logic needs to expose css to the template:

use Wonderboy\Mustache\ModulesLoader;
use Wonderboy\Mustache\Template\Utils;

class App {
  use ModulesLoader;

  public function run() {
    $css = Utils::getCSSMap();
    $html = $this->render('main', [ 'css' => $css ]);
    // call after rendering to get collected styles
    // only partials that are part of the render have their styles collected
    $styles = $this->getCssModules();
    // at this point, you can do whatever you want with the output and with the styles
    // I prefer to insert them directly into the <head>, at the end
    $output = str_replace( '</head>', $styles . PHP_EOL . '</head>', $html );

    // probably unnecessary, but you can reset the CSS Modules
    // $this->resetCssModules();

    // more logic?
    echo $output;
  }
}

$app = new App();
$app->run();

Development

Run Gulp to automatically generate the build artifacts that make this all work:

$ npm run dev

wonderboymusic/mustache-css-modules 适用场景与选型建议

wonderboymusic/mustache-css-modules 是一款 基于 JavaScript 开发的 Composer 扩展包,目前已累计 5 次下载、GitHub Stars 达 6, 最近一次更新时间为 2019 年 11 月 14 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 wonderboymusic/mustache-css-modules 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 6
  • Watchers: 1
  • Forks: 0
  • 开发语言: JavaScript

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-11-14