定制 nepada/form-renderer 二次开发

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

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

nepada/form-renderer

Composer 安装命令:

composer require nepada/form-renderer

包简介

Latte template based form renderer for Nette forms with full support for Bootstrap 3, 4 & 5.

README 文档

README

Build Status Coverage Status Downloads this Month Latest stable

Installation

Via Composer:

$ composer require nepada/form-renderer

Register the extension in config.neon:

extensions:
    formRenderer: Nepada\Bridges\FormRendererDI\FormRendererExtension

Usage

Nette gives you two options how to render a form:

  1. Render the whole form manually in Latte template using form macros. This way you have complete control over the rendering, but writing all the templates quickly gets repetitive.
  2. Render it using a form renderer, e.g. DefaultFormRenderer from nette/forms. DefaultFormRenderer is very customizable, but it's hard to setup special rendering rules for only some controls of a form, or add support for rendering of new form control types.

nepada/form-renderer is built on top of Latte templates and their powerful blocks, thus combining strengths of manual rendering with DRY principle of form renderers.

TemplateRenderer

You can use TemplateRendererFactory service to create the renderer with preconfigured default template. It renders a form more or less the same way as DefaultFormRenderer.

Customizing rendering

You can customize rendering by importing blocks from a template file - blocks imported later override the previously imported ones of the same name. You can also pass custom variables to the template.

/** @var Nepada\FormRenderer\TemplateRendererFactory $factory */
$renderer = $factory->create();
$renderer->importTemplate(__DIR__ . '/custom-form-rendering-blocks.latte');
$renderer->getTemlate()->foo = 'bar'; 
$form->setRenderer($renderer);

Tips:

  • You can define special rendering for a specific control of a form in #control-name-* block (e.g. #control-name-container-subcontainer-foocontrol).
  • If you need special rendering for both a control and its label, define it in #pair-name-* block.
  • Rendering of different control types (based on the value of $control->getOption('type')) is controlled by blocks #control-type-* and #pair-type-*. The default template actually uses this for buttons (rendering of consecutive buttons in one row).
  • You can also specify template files to be imported in your config.neon:
    formRenderer:
        default:
            imports:
                - %appDir%/templates/@form-extras1.latte
                - %appDir%/templates/@form-extras2.latte

For a complete overview of supported blocks and better understanding how the renderer works, please read the code of the template.

Creating custom TemplateRenderer setup from scratch

You don't need to use the default template, you can create one from scratch with blocks tailored to your needs. You can define factory for your custom setup like this:

services:
    customRenderer:
        implement: Nepada\FormRenderer\TemplateRendererFactory
        setup:
          - importTemplate('%appDir%/templates/@form.latte')
          - importTemplate('%appDir%/templates/@form-extras.latte')

Just make sure that one of your template files defines block named #form - this is used as a starting point for the rendering.

Filter safeTranslate in templates

For translations the templates may use custom safeTranslate filter. The key differences from standard translate filter are:

  1. It avoids translating instances of Nette\Utils\IHtmlString and Latte\Runtime\IHtmlString.
  2. It uses a translator from the form instance that is being rendered.
  3. If the form has no translator set, than it simply returns the passed string untranslated.

Improved version of n:class macro

In all form templates there is also available an improved version of n:class macro that supports merging of classes from Nette\Utils\Html instances. You can do stuff like <input n:name="$control" n:class="$control->controlPrototype->class, form-control"> and don't need to worry if the class attribute is really represented as a string or array, it all just works.

Bootstrap5Renderer

Form renderer compatible with Bootstrap 5, it internally uses TemplateRenderer with custom template.

The template supports three rendering modes:

/** @var Nepada\FormRenderer\Bootstrap5RendererFactory $factory */
$renderer = $factory->create();
$renderer->setBasicMode(); // Basic form
$renderer->setInlineMode(); // Inline form
$renderer->setHorizontalMode(4, 8); // Horizontal form (you can optionally set the size of label and control columns)

Use $renderer->setRenderValidState(true) to enable/disable rendering of "valid" form control state for filled inputs after form submission.

In inline mode the error messages are always rendered as tooltips. In the other modes you can switch between standard and tooltip rendering by calling $renderer->setUseErrorTooltips(true).

You can enable floating labels by $renderer->setUseFloatingLabels(true) (ignored in horizontal mode only). By default, all controls of text, datetime, textarea and select type are rendered with floating label, but you can manually override this on a specific control by setting $input->setOption('floatingLabel', false).

To render a checkbox as a switch, you need to set type option: $checkboxInput->setOption('type', 'switch').

To render radio or checkbox as a toggle button, add btn class (and any desired button styling class) to label prototype: $radio->getItemLabelPrototype()->addClass('btn btn-outline-primary').

Bootstrap5Renderer makes a couple of adjustments to the form before it is passed over to TemplateRenderer:

  1. It adds btn btn-primary classes to the control prototype of first SubmitButton in the form, unless there already is such a control in the form.
  2. It adds btn btn-secondary classes to the control prototype of every Button control, unless it already has btn class set.
  3. Changes type option on all Checkbox, CheckboxList, RadioList controls setup to be rendered as toggle buttons from checkbox/radio to togglebutton/togglebuttonlist.
  4. Changes type option on all CheckboxList controls from checkbox to checkboxlist.
  5. When floating labels are enabled, it sets boolean floatingLabel option (unless already set) on all controls to indicate whether the floating label should be rendered.

You can change the default renderer configuration from your config.neon:

formRenderer:
  bootstrap5:
    mode: horizontal
    renderValidState: true
    useErrorTooltips: true
    imports:
      - %appDir%/templates/@form-extras.latte

Bootstrap4Renderer

Form renderer compatible with Bootstrap 4, it internally uses TemplateRenderer with custom template.

The template supports three rendering modes:

/** @var Nepada\FormRenderer\Bootstrap4RendererFactory $factory */
$renderer = $factory->create();
$renderer->setBasicMode(); // Basic form
$renderer->setInlineMode(); // Inline form
$renderer->setHorizontalMode(4, 8); // Horizontal form (you can optionally set the size of label and control columns)

Use $renderer->setRenderValidState(true) to enable/disable rendering of "valid" form control state for filled inputs after form submission.

In inline mode the error messages are always rendered as tooltips. In the other modes you can switch between standard and tooltip rendering by calling $renderer->setUseErrorTooltips(true).

You can enable the use of custom form controls by $renderer->setUseCustomControls(true).

To render a checkbox as a switch, you need to set type option: $checkboxInput->setOption('type', 'switch').

Bootstrap4Renderer makes a couple of adjustments to the form before it is passed over to TemplateRenderer:

  1. It adds btn btn-primary classes to the control prototype of first SubmitButton in the form, unless there already is such a control in the form.
  2. It adds btn btn-secondary classes to the control prototype of every Button control, unless it already has btn class set.
  3. Changes type option on all CheckboxList controls from checkbox to checkboxlist.

You can change the default renderer configuration from your config.neon:

formRenderer:
    bootstrap4:
        mode: horizontal
        renderValidState: true
        useErrorTooltips: true
        useCustomControls: true
        imports:
            - %appDir%/templates/@form-extras.latte

Bootstrap3Renderer

Form renderer compatible with Bootstrap 3, it internally uses TemplateRenderer with custom template.

The template supports three rendering modes:

/** @var Nepada\FormRenderer\Bootstrap3RendererFactory $factory */
$renderer = $factory->create();
$renderer->setBasicMode(); // Basic form
$renderer->setInlineMode(); // Inline form
$renderer->setHorizontalMode(4, 8); // Horizontal form (you can optionally set the size of label and control columns)

Use $renderer->setRenderValidState(true) to enable/disable rendering of "valid" form control state for filled inputs after form submission.

Bootstrap3Renderer makes a couple of adjustments to the form before it is passed over to TemplateRenderer:

  1. It adds btn btn-primary classes to the control prototype of first SubmitButton in the form, unless there already is such a control in the form.
  2. It adds btn btn-default classes to the control prototype of every Button control, unless it already has btn class set.
  3. Changes type option on all CheckboxList controls from checkbox to checkboxlist.

You can change the default renderer configuration from your config.neon:

formRenderer:
    bootstrap3:
        mode: horizontal
        renderValidState: true
        imports:
            - %appDir%/templates/@form-extras.latte

nepada/form-renderer 适用场景与选型建议

nepada/form-renderer 是一款 基于 HTML 开发的 Composer 扩展包,目前已累计 253.48k 次下载、GitHub Stars 达 11, 最近一次更新时间为 2017 年 08 月 06 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 nepada/form-renderer 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 253.48k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 11
  • 点击次数: 32
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 11
  • Watchers: 1
  • Forks: 2
  • 开发语言: HTML

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2017-08-06