尝试将Twig与Codeigniter HMVC集成


Trying to integrate Twig with Codeigniter HMVC

我正在尝试将Twig集成到代码点火器HMVC中。我能够做以下事情:

  1. 自定义细枝扩展
  2. 扩展父模板
  3. 创建自定义筛选器
  4. 创建自定义函数

但我无法弄清楚我是如何在代码点火器中调用模板中的控制器函数的。

目前在Codeigniter我们做

Modules::run('function path',$data]); 

在symfony中,我们确实喜欢

{{ render(controller(
        'AppBundle:Default:register'
    )) }}

呵呵,这是每个Symfony2开发人员都想首先实现的:-)。

以下是我(快速而肮脏)实现它的方式:

1) 创建以下帮助程序:

<?php
if (!function_exists('twig_render'))
{
    function twig_render()
    {
        $args = func_get_args();
        $route = array_shift($args);
        // @TODO this way do not handle controllers in subfolders
        $controller = APPPATH . 'controllers/' . substr($route, 0, strrpos($route, '/'));
        $explode = explode('/', $route);
        if (count($explode) < 2)
        {
            show_error("twig_render: A twig route is made from format: path/to/controller/action.");
        }
        if (!is_file($controller . '.php'))
        {
            show_error("twig_render: Controller not found: {$controller}");
        }
        if (!is_readable($controller . '.php'))
        {
            show_error("twig_render: Controller not readable: {$controller}");
        }
        require_once($controller . '.php');
        $class = ucfirst(reset(array_slice($explode, count($explode) - 2, 1)));
        if (!class_exists($class))
        {
            show_error("twig_render: Controller file exists, but class not found inside: {$class}");
        }
        $object = new $class();
        if (!($object instanceof CI_Controller))
        {
            show_error("twig_render: Class {$class} is not an instance of CI_Controller");
        }
        $method = $explode[count($explode) - 1];
        if (!method_exists($object, $method))
        {
            show_error("twig_render: Controller method not found: {$method}");
        }
        if (!is_callable(array($object, $method)))
        {
            show_error("twig_render: Controller method not visible: {$method}");
        }
        call_user_func_array(array($object, $method), $args);
        $ci = &get_instance();
        return $ci->output->get_output();
    }
}

2) 将以下函数添加到库中(注意:Ci已经给出了site_url):

$this->_twig_env->addFunction(new Twig_SimpleFunction('site_url', 'site_url')));
$this->_twig_env->addFunction(new Twig_SimpleFunction('render', 'twig_render', array('is_safe' => array('html'))));

现在你已经准备好出发了,例如,如果你有这样的路线:

$route['list-partners-on-country-(:num)'] = 'list/partners/$1';

要创建链接,可以使用:

<a href="{{ site_url() }}/{{ render('list/partners', country.id) }}" class="btn btn-small">{{ country.name }}</a>

享受,

CodeIgniter+HMVC+Twig+条令部分项目动态cms(空白可构建cms)

你可以克隆这个https://github.com/dandisy/dynamicCMS

您可以创建帮助程序内容

// for module return string
if(!function_exists('module_run'))
{
    function module_run($module, $param = array())
    {
        echo modules::run($module, $param);
    }
}
// for module return array
if(!function_exists('module_array'))
{
    function module_array($module)
    {
        return modules::run($module);
    }
}

然后自动加载帮助程序以在控制器和视图中使用