尝试将 Twig 与 Codeigniter HMVC 集成

Trying to integrate Twig with Codeigniter HMVC

我正在尝试将 Twig 集成到 codeigniter HMVC 中。我可以做以下事情:

  1. 自定义 Twig 扩展
  2. 扩展父模板
  3. 创建自定义过滤器
  4. 正在创建自定义函数

但我无法弄清楚如何在 codeigniter 的模板中调用控制器函数。

目前我们在 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) 将以下函数添加到您的库中(注意:site_url 已由 Ci 提供):

$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/';

要创建 link,您可以使用:

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

尽情享受,

CodeIgniter + HMVC + Twig + Doctrine 项目动态 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);
    }
}

然后自动加载助手以在控制器和您的视图中使用