Drupal 7:从挂钩菜单打印主题,性能良好
Drupal 7: print theme from hook menu with good performance
我在自定义路线的挂钩菜单中工作,如下所示:
function mymodule_menu() {
$items = [];
$items['myroute/%'] = array(
'page callback' => 'my_callback',
'page arguments' => array(1),
'access arguments' => array('access content'),
);
return $items;
}
在 theme_hook 中,我添加了一个新的模板功能,如下所示:
function mymodule_theme($existing, $type, $theme, $path) {
$default = array(
'path' => drupal_get_path('module', 'mymodule') . '/templates',
'variables' => array(),
);
return array(
'product_tile' => array_merge($default, array(
'template' => 'product-tile',
)),
);
}
我创建了一个名为 'product-tile.tpl.php' 的模板文件,它适用于所有情况并且是一个部分模板。
在回调函数中,我需要return一个特定的.tpl.php模板,像这样:
function my_callback($parameter) {
$template_data = 'lorem ipsum';
$output = theme('product_tile', array('content' => $template_data ));
echo ($output);
}
重点是:'theme()' 函数渲染数据的时间太长,它不仅渲染模板,还渲染整个 html 结构,这是不需要的,也不是 oof 的一部分模板。
例如:模板是:
<div id="my structure></div>
但是当我收到对“/myroute/myparameter 的回复时,它不会打印我的模板,而是打印所有 html 结构,如下所示:
<html>
<body>......lots of stuff here +js +css + all drupal stuff
<div id="my structure></div>
</body>
</html>
打印需要很长时间(比如 10 秒或更长时间)。
我尝试使用 cache_get 和 cache_set 来缓存它,但是奇怪的事情发生了,比如随机的空响应。
有谁知道在 drupal 7 的挂钩菜单中打印部分模板的更高效的方法?我这样慢得要命。
提前致谢
您的自定义路由映射到 page callback
, 必须 return 内容 的函数在页面内渲染 以交付 (不仅仅是打印一些东西)。
此内容可以是:
- 一个HTML字符串
- 一个 drupal 渲染数组
- 或菜单状态代码(
MENU_ACCESS_DENIED
、MENU_NOT_FOUND
等)
该内容(一旦呈现,如果不是 HTML 字符串)是您在传递给活动主题的 的 $content
或 $page['content']
变量中实际获得的内容page.tpl.php。
现在,您的问题可能只是由于使用此模板引起的(例如,如果它包含繁重的 PHP 或错误的实现,无论是什么原因),但它可能完全不同:
'theme()' 函数呈现数据的时间过长
传递该页面所花费的时间并不一定意味着它是由该主题功能引起的。您可以检查呈现该特定模板的实际时间 - 而不是整个 HTML 页面 - 它可能 而不是 10 秒:
$start = microtime(true);
$output = theme('product_tile', array('content' => 'lorem ipsum'));
$end = microtime(true);
$time = $end - $start;
# dpm() is brought by devel module
dpm('product_tile rendering: ' . $time . 'sec.');
由于呈现自定义模板时涉及一些繁重的 drupal 挂钩和预处理函数,因此页面交付时间可能较长,但也可能是呈现页面其他区域(侧边栏、页眉、页脚等)的成本.).
当我收到对 /myroute/myparameter 的回复时,它不会打印我的模板,而是打印所有 html :
如上所述,路由路径 的响应是 一个完整的 HTML 页面,其中包含 'content' 'page callback' 在相应的菜单项中定义。 drupal 页面回调的预期行为恰恰是 return 要在该页面上显示的内容,加载完整的 HTML 页面。这就是为什么需要 return
语句(如果不是则不要假设任何东西),不要在页面回调中使用 print
或 echo
函数,以查看变量的内容使用像 dpm
) :
这样的调试函数
function my_callback($parameter) {
$template_data = 'lorem ipsum';
$output = theme('product_tile', array('content' => $template_data ));
# debug the output of 'product_tile'
dpm($output);
# return what should be displayed on that page
return output;
}
我在自定义路线的挂钩菜单中工作,如下所示:
function mymodule_menu() {
$items = [];
$items['myroute/%'] = array(
'page callback' => 'my_callback',
'page arguments' => array(1),
'access arguments' => array('access content'),
);
return $items;
}
在 theme_hook 中,我添加了一个新的模板功能,如下所示:
function mymodule_theme($existing, $type, $theme, $path) {
$default = array(
'path' => drupal_get_path('module', 'mymodule') . '/templates',
'variables' => array(),
);
return array(
'product_tile' => array_merge($default, array(
'template' => 'product-tile',
)),
);
}
我创建了一个名为 'product-tile.tpl.php' 的模板文件,它适用于所有情况并且是一个部分模板。
在回调函数中,我需要return一个特定的.tpl.php模板,像这样:
function my_callback($parameter) {
$template_data = 'lorem ipsum';
$output = theme('product_tile', array('content' => $template_data ));
echo ($output);
}
重点是:'theme()' 函数渲染数据的时间太长,它不仅渲染模板,还渲染整个 html 结构,这是不需要的,也不是 oof 的一部分模板。
例如:模板是:
<div id="my structure></div>
但是当我收到对“/myroute/myparameter 的回复时,它不会打印我的模板,而是打印所有 html 结构,如下所示:
<html>
<body>......lots of stuff here +js +css + all drupal stuff
<div id="my structure></div>
</body>
</html>
打印需要很长时间(比如 10 秒或更长时间)。
我尝试使用 cache_get 和 cache_set 来缓存它,但是奇怪的事情发生了,比如随机的空响应。
有谁知道在 drupal 7 的挂钩菜单中打印部分模板的更高效的方法?我这样慢得要命。
提前致谢
您的自定义路由映射到 page callback
, 必须 return 内容 的函数在页面内渲染 以交付 (不仅仅是打印一些东西)。
此内容可以是:
- 一个HTML字符串
- 一个 drupal 渲染数组
- 或菜单状态代码(
MENU_ACCESS_DENIED
、MENU_NOT_FOUND
等)
该内容(一旦呈现,如果不是 HTML 字符串)是您在传递给活动主题的 的 $content
或 $page['content']
变量中实际获得的内容page.tpl.php。
现在,您的问题可能只是由于使用此模板引起的(例如,如果它包含繁重的 PHP 或错误的实现,无论是什么原因),但它可能完全不同:
'theme()' 函数呈现数据的时间过长
传递该页面所花费的时间并不一定意味着它是由该主题功能引起的。您可以检查呈现该特定模板的实际时间 - 而不是整个 HTML 页面 - 它可能 而不是 10 秒:
$start = microtime(true); $output = theme('product_tile', array('content' => 'lorem ipsum')); $end = microtime(true); $time = $end - $start; # dpm() is brought by devel module dpm('product_tile rendering: ' . $time . 'sec.');
由于呈现自定义模板时涉及一些繁重的 drupal 挂钩和预处理函数,因此页面交付时间可能较长,但也可能是呈现页面其他区域(侧边栏、页眉、页脚等)的成本.).
当我收到对 /myroute/myparameter 的回复时,它不会打印我的模板,而是打印所有 html :
如上所述,路由路径 的响应是 一个完整的 HTML 页面,其中包含 'content' 'page callback' 在相应的菜单项中定义。 drupal 页面回调的预期行为恰恰是 return 要在该页面上显示的内容,加载完整的 HTML 页面。这就是为什么需要
这样的调试函数return
语句(如果不是则不要假设任何东西),不要在页面回调中使用print
或echo
函数,以查看变量的内容使用像dpm
) :function my_callback($parameter) { $template_data = 'lorem ipsum'; $output = theme('product_tile', array('content' => $template_data )); # debug the output of 'product_tile' dpm($output); # return what should be displayed on that page return output; }