jhekasoft/html-shortcode

此包已被 放弃 且不再维护。未建议替代包。
此包最新版本(dev-master)没有可用的许可信息。

允许使用HTML代码渲染视图辅助器的ZF2模块。

dev-master 2013-06-16 20:22 UTC

This package is not auto-updated.

Last update: 2020-01-19 15:59:44 UTC


README

允许使用HTML代码渲染视图辅助器的Zend Framework 2模块。您可以与TinyMCE和CKEditor等编辑器一起使用此模块。

安装

php composer.phar require jhekasoft/html-shortcode:dev-master

application.config.php 中向您的 modules 添加以下键

'modules' => array(
    //...
    'HtmlShortcode',
),

使用方法

在控制器中

//...
use HMShortCode\Filter\ShortCodeFilter;

class IndexController
{
    public function showAction()
    {
        //...

        $shortCodeFilter = new ShortCodeFilter();
        $shortCodeFilter->setServiceLocator($this->getServiceLocator());

        $item->text = $shortCodeFilter->filter($item->text);

        return array(
            'item' => $item,
        );
    }
}

在实体中

//...
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use HtmlShortcode\Filter\ShortcodeFilter;

class Pages implements InputFilterAwareInterface, ServiceLocatorAwareInterface
{
    public function exchangeArray($data)
    {
        $this->text = (isset($data['text'])) ? $data['text'] : null;

        // ShortCode filter
        $shortcodeFilter = new ShortcodeFilter();
        $shortcodeFilter->setServiceLocator($this->getServiceLocator());
        $this->filtered_text = $shortcodeFilter->filter($this->text);
    }

    public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
    {
        $this->serviceLocator = $serviceLocator;
        return $this;
    }

    public function getServiceLocator()
    {
        return $this->serviceLocator;
    }
}

在引导阶段(除了编辑动作)

//...
public function onBootstrap($e)
{
    $app = $e->getApplication();
    $em = $app->getEventManager();

    $em->attach(\Zend\Mvc\MvcEvent::EVENT_ROUTE, function($e) {
        $match = $e->getRouteMatch();
        $action = $match->getParam('action');

        if ('edit' != $action) {
            $sm = $e->getApplication()->getServiceManager();
            $view = $sm->get('ViewRenderer');
            $filters = $view->getFilterChain();
            $widgetFilter = new ShortCodeFilter();
            $widgetFilter->setServiceLocator($sm);
            $filters->attach($widgetFilter, 50);
            $view->setFilterChain($filters);
        }
    });
}

HTML代码

示例

<span class="htmlshortcode" data-helper="soundBlock" data-params="[value1]"></div>

<div class="htmlshortcode" data-helper="soundBlock" data-params="[value2]"></div>

<span class="htmlshortcode" data-helper="soundBlock" data-params='[value3][{"size":"small"}]'></span>

最后一个示例中的第二个参数是JSON格式。

上述示例等价于视图脚本中的此PHP代码

echo $this->soundBlock('value1');

echo $this->soundBlock('value2');

echo $this->soundBlock('value3', array("size" => "small"));