项目作者: eth8505

项目描述 :
Module for Theming Zend\Mvc apps
高级语言: PHP
项目地址: git://github.com/eth8505/zend-mvc-themer.git
创建时间: 2018-03-12T18:53:53Z
项目社区:https://github.com/eth8505/zend-mvc-themer

开源协议:MIT License

下载


Eth8505\ZendMvcThemer - Module for Theming Zend\Mvc apps

!!!IMPORTANT!!! This package is no longer maintained, as Zendframework was renamed to laminas. Please use eth85055/laminas-mvc-themer instead.

The Eth8585\ZendMvcThemer moduole adds theming support to Zend\Mvc apps.

How to install

Install eth8505/zend-mvc-themer package via composer.

  1. $ composer require eth8505/zend-mvc-themer

Load the module in your application.config.php file like so:

  1. <?php
  2. return [
  3. 'modules' => [
  4. 'Eth8585\ZendMvcThemer\\',
  5. // ...
  6. ],
  7. ];

How to use

1 Creating a theme

As described above, themes need to be registered with the theme plugin manager. A theme must implement the
ThemeInterface. All methods exept for getName may return an empty array. Check the DefaultTheme class
for an empty implementation of a theme specifying only a name.

1.1 Specifying custom stylesheets

Implement the getStylesheet method in your theme, and return an array. Note that all paths will be pushed through
the BasePath view helper and hence must be relative to your public/ directory.

  1. <?php
  2. use Eth8505\ZendMvcThemer\Theme\ThemeInterface;
  3. class MyTheme implements ThemeInterface {
  4. // ...
  5. public function getStylesheets() : array {
  6. return [
  7. 'css/theme/my/custom/file.css'
  8. ];
  9. }
  10. // ...
  11. }

All stylesheets are injected using the appendStylesheet method of the HeadLink viewhelper.

1.2 Specifying custom javascripts

Implement the getScripts method in your theme, and return an array. Note that all paths will be pushed through
the BasePath view helper and hence must be relative to your public/ directory.

  1. <?php
  2. use Eth8505\ZendMvcThemer\Theme\ThemeInterface;
  3. class MyTheme implements ThemeInterface {
  4. // ...
  5. public function getScripts() : array {
  6. return [
  7. 'css/theme/my/custom/file.js'
  8. ];
  9. }
  10. // ...
  11. }

All scripts are injected using the prependFile method of the HeadScript viewhelper.

1.3 Specifying custom variables

Implement the getVariables method in your theme, and return an array. Note that all paths will be pushed through
the BasePath view helper and hence must be relative to your public/ directory.

  1. <?php
  2. use Eth8505\ZendMvcThemer\Theme\ThemeInterface;
  3. class MyTheme implements ThemeInterface {
  4. // ...
  5. public function getVariables() : array {
  6. return [
  7. 'heading1' => 'one',
  8. 'heading2' => [
  9. 'key1' => 'test'
  10. ]
  11. ];
  12. }
  13. // ...
  14. }

Theme variables are not automatically injected into your view models, as this could interfere with whatever you
set in your view models. However, the module provides a theme() view helper allowing access to the theme variables.

  1. <html>
  2. <body>
  3. <h1><?= $this->theme()->var('heading1') ?></h1>
  4. <h2><?= $this->theme()->var('heading2/key1') ?></h2>
  5. </body>
  6. </html>

1.4 Specifying custom meta tags

Meta tags are a little more complicated than scripts, styles or variables, as there are two basic types, name and
http-equiv. With this module, we use the same basic-syntax for both of them, specifying the type as a key in the
definition array.
To implement custom meta tags, implement get getMetaTags method in your theme.

  1. <?php
  2. use Eth8505\ZendMvcThemer\Theme\ThemeInterface;
  3. class MyTheme implements ThemeInterface {
  4. // ...
  5. public function getMetaTags() : array {
  6. return [
  7. [
  8. 'type' => 'name',
  9. 'name' => 'robots',
  10. 'content' => 'noindex,nofollow'
  11. ],
  12. [
  13. 'type' => 'http-equiv',
  14. 'name' => 'refresh',
  15. 'content' => '30'
  16. ]
  17. ];
  18. }
  19. // ...
  20. }

2. Register with service manager

You can either register your themes with the service manager via the config in your module.config.php:

  1. <?php
  2. return [
  3. 'zend-mvc-themes' => [
  4. 'invokables' => [
  5. MyTheme::class
  6. ]
  7. ]
  8. ];

or register it in your module class using the ThemeProviderInterface:

  1. <?php
  2. namespace MyModule;
  3. use Eth8505\ZendMvcThemer\Theme\ThemeProviderInterface;
  4. class Module implements ThemeProviderInterface {
  5. /**
  6. * @inheritdoc
  7. */
  8. public function getThemeConfig() {
  9. return [
  10. 'invokables' => [
  11. MyTheme::class
  12. ]
  13. ];
  14. }
  15. }

3. Resolving themes

Per default, theme resolving is done using the ConfigResolver class, that simply checks the config
zend-mvc-themer/resolver config, and injects the theme as the theme service.

3.1 Custom theme resolvers

In addition to the default config-based theme resolver, you can also specify a custom resolver class. This can be any
implementation of ThemeResolverInterface of your choosing, reading the theme from the session (if you want to provide
a selection of themes to your users).

Example implementation of a simple hostname-based theme resolver:

  1. <?php
  2. namespace MyModule;
  3. use Eth8505\ZendMvcThemer\Resolver\ThemeResolverInterface;
  4. use Eth8505\ZendMvcThemer\Theme\ThemeInterface;
  5. use Eth8505\ZendMvcThemer\Theme\ThemePluginManager;
  6. use Zend\Http\Request;
  7. class Module implements ThemeResolverInterface {
  8. private $request;
  9. private $pluginManager;
  10. public function __construct(Request $request, ThemePluginManager $pluginManager) {
  11. $this->request = $request;
  12. $this->pluginManager = $pluginManager;
  13. }
  14. public function resolve() : ThemeInterface {
  15. if ($this->request->getUri()->getHost() === 'my.awesome.host') {
  16. $theme = ThemeOne::class;
  17. } else {
  18. $theme = ThemeTwo::class;
  19. }
  20. return $this->pluginManager->get($theme);
  21. }
  22. }