fys/hook-bundle

在 twig 模板上提供钩子

1.0.1 2016-04-29 07:53 UTC

This package is auto-updated.

Last update: 2024-09-15 05:54:53 UTC


README

描述

FYS Hook Bundle 允许你在 twig 模板引擎中创建钩子并简单地调用它

安装

composer require fys/hook-bundle

创建第一个钩子

首先,创建一个标记为 hook.type 的服务

// services.yml

app.hook.head:
    class: AppBundle\Component\Hooks\HeadHook
    tags:
        - { name: hook.type }

其次,创建你在服务中提到的类

// AppBundle/Component/Hooks/HeadHook.php

namespace AppBundle/Component/Hooks;

use FYS\HookBundle\Component\HookInterface;

class HeadHook implements HookInterface
{
    public function getName()
    {
        // the hook name, for example "head"
        return 'head';
    }
    
    pubilc function getAction()
    {
        // the action when the hook is called
        return 'HELLO THIS IS MY FIRST HOOK';
    }
    
    public function getPriority()
    {   
        // the priority of this hook
        return 1;
    }
}

注意:钩子类应该实现 HookInterface,然后你可以在 twig 模板中调用 {{ call_hook('head') }},它将打印 "HELLO THIS IS MY FIRST HOOK"。