我正在尝试将Twig集成到codeigniter HMVC中。我能够做到以下几点:
但是我无法弄清楚我在codeigniter中如何调用我的模板中的控制器功能。
目前在Codeigniter中我们
Modules::run('function path',$data]);
在symfony,我们确实喜欢
{{ render(controller(
'AppBundle:Default:register'
)) }}
答案 0 :(得分:0)
呵呵,这是每个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>
享受,
答案 1 :(得分:0)
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);
}
}
然后自动加载帮助程序以在控制器和视图中使用