Search by

contenir / contenir-view-helper

peptolab

Contenir View components

Package info

github.com/contenir/contenir-view-helper

Homepage

Issues

Documentation

pkg:composer/contenir/contenir-view-helper

Statistics

Installs: 1 332

Dependents: 0

Suggesters: 0

Stars: 0

v2.0.0 2026-10-05 09:29 UTC

README

Continuous Integration codecov

laminas-view helpers shared by Contenir CMS sites: text formatting, links, media embeds, BEM form groups, and access to the application's ACL, settings and view cache from view scripts.

Requirements

  • PHP 8.3, 8.4 or 8.5
  • laminas-view 2.35+, laminas-form 3.21+, laminas-cache 3.12+
  • laminas/laminas-i18n when you use formGroup(), as the laminas-form view helpers it renders through need it

The 1.x releases, which support PHP 8.0 to 8.2, remain available from the 1.x branch and v1.* tags; see UPGRADE-2.0.md.

Installation

composer require contenir/contenir-view-helper

With the Laminas component installer, the Contenir\View module (laminas-mvc) or Contenir\View\ConfigProvider (Mezzio) registers itself. Otherwise add Contenir\View to config/modules.config.php, or the ConfigProvider to your config aggregator.

Usage

Every helper is registered under its class name and a camelCase and PascalCase alias, so view scripts call $this->truncate(...) or $this->Truncate(...).

Helper Purpose Docs
dateFormat($date = null, $format = null) Format a date string (d M Y by default); null when unparseable Text
truncate($text, $length = 125, $strip = false, $etc = '...', $breakWords = false, $middle = false) Shorten text at a word boundary Text
fileSize($bytes, $decimals = 2, $system = FileSize::SYSTEM_METRIC) 1.5MB, or 1.5MiB in the binary system Text
fileType($mimeType) PDF or Document Text
richContent($html, $template = []) Lay out __SECTION__ / __COL__ markers as grid sections and columns Text
urlFormat($url = null, $format = null) Normalise a URL and render chosen parts Links
resourceLink($value = null, $defaultCta = null) Normalise a CMS link field into link objects Links
socialLink($network, $handleOrUrl, $options = []) Link to a social network profile Links
escapeEmail($email, $mailto = false) Obfuscate an email address against harvesters Links
image($path, $attributes = [], $pictureAttributes = []) Lazy-loaded <picture> served from a CDN Media
srcset($path, $sizes = []) srcset value for pre-resized derivatives Media
video($path, $options = [], $controls = false) Vimeo/YouTube embed or <video> element Media
icon($name, $options = []) Inline an SVG icon Media
formGroup($element, $displayAttributes = [], $groupAttributes = [...]) Render a laminas-form element as a BEM form group Forms
acl(), settings(), cache() The application's ACL, settings and view cache Application services
<h2><?= $this->escapeHtml($this->truncate($entry->title, 60)) ?></h2>
<time><?= $this->dateFormat($entry->published) ?></time>
<?= $this->image($entry->image, ['alt' => $entry->title, 'resizeWidth' => 800]) ?>
<?php foreach ($this->resourceLink($entry->links) as $link): ?>
    <a href="<?= $this->escapeHtmlAttr($link->url) ?>"><?= $this->escapeHtml($link->cta) ?></a>
<?php endforeach ?>

All helper classes are final. To customise one, register your own helper under the same alias; see Customising a helper.

Configuration

Key Used by Default
view_cdn.host, view_cdn.method image() no host; https
view_helper_config.icon.class icon() default wrapper class none (icon output bare)
settings settings() []
service ViewCache (a laminas-cache StorageInterface) cache() required to use cache()
service Application\Acl\Acl (an AclInterface) acl() required to use acl()

The laminas-mvc Module also sets shared view_manager defaults: an HTML5 doctype, error/404, error/403 and error/index templates, and the JSON view strategy. See Configuration.

Development

The QA toolchain is contenir/contenir-qa-tools. Mago is a standalone binary, installed separately (brew install mago).

composer check             # everything below
composer cs-check          # mago format --check && mago lint
composer static-analysis   # mago analyze
composer test              # unit suite: helpers and factories with doubled collaborators, no I/O
composer test-integration  # integration suite: a real PhpRenderer and plugin manager, temp-directory files
composer test-coverage     # both suites, clover.xml for Codecov
composer mutation-test     # Infection mutation testing over both suites (not part of check: about 2 minutes)

License

BSD-3-Clause. See LICENSE.md.