承接 nabeghe/nohtml 相关项目开发

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

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

nabeghe/nohtml

Composer 安装命令:

composer require nabeghe/nohtml

包简介

Say no to HTML — write views in PHP with a clean, simple DSL.

README 文档

README

Say no to HTML — write views in PHP with a clean, simple DSL.

There are many PHP-based DSLs for HTML out there but NoHTML takes a different route in both behavior and structure. At its core, there's a class where each method corresponds to an HTML element. These methods can be used in both static and instance contexts. Each method accepts a variadic list of arguments representing the element’s children. The return value of every method is an instance of the Nabeghe\NoHtml\Nodes\Element class, whose methods map to the element's attributes, following a Fluent Builder pattern.

🫡 Usage

🚀 Installation

You can install the package via composer:

composer require nabeghe/nohtml

Example

In the example below, the NoHtml class is made available via an alias named N. You can also access the shared instance using the nohtml() function; however, the below code proceeds in a static manner.

In this code, an HTML element is created right from the start, and then the rest of the page is built around it.

use Nabeghe\NoHtml\NoHtml as N;

require 'vendor/autoload.php';

$app = N::html(
    N::head(
        N::meta()->charset('utf-8'),
        N::meta()->name('viewport')->charset('width=device-width, initial-scale=1.0'),
        N::title('No HTML for PHP by Nabeghe'),
        N::script()->src('https://cdn.tailwindcss.com'),
    ),
    N::body(
        N::header(
            N::h1('No HTML for PHP by Nabeghe')->class('text-3xl font-bold'),
            N::nav(
                N::a('Intro')->href('#intro')->class('over:underline'),
                N::a('Table')->href('#table')->class('hover:underline'),
                N::a('Form')->href('#form')->class('hover:underline'),
            )->class('mt-2 space-x-4'),
        )->class('bg-gray-900 text-white py-6 text-center'),
        N::main(
            N::section(
                N::h2('Introduction')->class('text-2xl font-semibold mb-4'),
                N::p(<<<EOD
                    There are many PHP-based DSLs for HTML out there  but **NoHTML** takes a different route in both behavior and structure.
                    At its core, there's a class where each method corresponds to an HTML element.
                    These methods can be used in both static and instance contexts.
                    Each method accepts a variadic list of arguments representing the element’s children.
                    The return value of every method is an instance of the `Nabeghe\NoHtml\Element` class,
                    whose methods map to the element's attributes, following a **Fluent Builder** pattern.
                    EOD
                )->class('mb-4'),
                N::p('This items generated by callback'),
                N::ul([Items::class, 'generate'])->data('template', 'Item {n}')->class('list-disc list-inside mb-4'),
            )->id('intro'),
            N::section(
                N::h2('Details')->class('text-2xl font-semibold mb-4'),
                N::div(
                    N::table(
                        N::tr(
                            N::td('Github')->class([Css::class, 'tableCell']),
                            N::td(
                                N::a('https://github.com/nabeghe/nohtml-php')->href('https://github.com/nabeghe/nohtml-php')->title('No HTML'),
                            )->class([Css::class, 'tableCell']),
                        )->class([Css::class, 'tableRow']),
                        N::tr(
                            N::td('License')->class([Css::class, 'tableCell']),
                            N::td('MIT')->class([Css::class, 'tableCell']),
                        )->class([Css::class, 'tableRow']),
                    )->class('min-w-full border border-gray-300 rounded-md'),
                )->class('overflow-x-auto'),
            )->id('table'),
            N::section(
                N::h2('Contact Us')->class('text-2xl font-semibold mb-4'),
                N::form(
                    N::div(
                        N::label('Name:')->for('name')->class('block font-medium'),
                        N::input()->type('text')->id('name')->name('name')->placeholder('Enter your name')->class('w-full border border-gray-300 rounded px-3 py-2'),
                    ),
                    N::div(
                        N::label('Email:')->for('email')->class('block font-medium'),
                        N::input()->type('text')->id('email')->name('email')->placeholder('Your email')->class('w-full border border-gray-300 rounded px-3 py-2'),
                    ),
                    N::div(
                        N::label('Nessage:')->for('message')->class('block font-medium'),
                        N::textarea()->id('message')->name('message')->placeholder('Type your message here...')->class('w-full border border-gray-300 rounded px-3 py-2'),
                    ),
                    N::div(
                        N::input()->type('checkbox')->id('agree')->name('agree')->placeholder('Your email')->checked(true)->class('h-4 w-4 text-blue-600 rounded'),
                        N::label('I agree to the terms')->for('agree')->class('select-none'),
                    )->class('flex items-center space-x-2'),
                    N::button('Submit')->type('submit')->class('bg-blue-600 text-white rounded px-5 py-2 hover:bg-blue-700 transition'),
                )->action('#')->method('post')->class('space-y-4'),
            )->id('form'),
        )->class('max-w-4xl mx-auto p-6 bg-white mt-6 rounded-lg shadow-md space-y-10'),
        N::footer(
            new Unescapable('&copy; No HTML - Built with ❤️ by <a href="https://elatel.ir">https://elatel.ir</a>'),
        )->class('bg-gray-900 text-white text-center py-6 mt-10')
    ),
)->lang('en');

echo '<!DOCTYPE html>';
echo $app;

You can define a child element or an attribute value as a callable, so the actual value can be retrieved at the appropriate time.

Children can be plain strings or anything else — they will be converted to strings. If they are plain strings, they will be escaped, unless the Nabeghe\NoHtml\Nodes\Unescapable class is used. Also, a callback defined for children can return a string, an element, or a list of elements. If it returns a string, that string will not be escaped.

As for attributes, a callback can return the value of the attribute. If the value is null, that attribute won’t be set at all. So, if you want an empty value, use an empty string — null means the attribute doesn't exist. Also, if an attribute is set to true or false, no value will be assigned — true simply means the attribute will be present, and false means it won't be there.

Notice: Setting children for elements like <img> or <br> has no effect.

Callbacks

The class related to the callbacks used in the example above:

class Css
{
    public static function tableRow(string $name, El $el)
    {
        return 'hover:bg-gray-100';
    }

    public static function tableCell(string $name, El $el)
    {
        return 'border border-gray-300 px-4 py-2';
    }
}
class Items
{
    public static function generate(El $el)
    {
        $template = $el->data('template');

        $items = [];
        for ($i = 0; $i < 10; $i++) {
            $n = $i + 1;
            if ($n < 10) {
                $n = '0'.$n;
            }

            $items[] = N::li(str_replace('{n}', "$n", $template));
        }

        return $items;
    }
}

📖 License

Copyright (c) 2025 https://elatel.ir

Licensed under the MIT license, see LICENSE.md for details.

nabeghe/nohtml 适用场景与选型建议

nabeghe/nohtml 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2 次下载、GitHub Stars 达 2, 最近一次更新时间为 2025 年 06 月 14 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-06-14