Php Laravel延伸叶片和通过阵列作为参数
我已经搜索了很多关于这个主题的内容,但在网上没有找到太多内容,所以我开始研究并撰写一篇关于这个主题的完整文章,但我无法理解这里的一些内容,在blade中制作基本的自定义标记很容易Php Laravel延伸叶片和通过阵列作为参数,php,laravel-4,blade,Php,Laravel 4,Blade,我已经搜索了很多关于这个主题的内容,但在网上没有找到太多内容,所以我开始研究并撰写一篇关于这个主题的完整文章,但我无法理解这里的一些内容,在blade中制作基本的自定义标记很容易 @search @endsearch or @title('something') 但是如果我想做下面的事情呢 @cache('sidebar',10,[$silver,$gold,$platinum]) html tags come here @endcache 目前我是这样做的 @cache('sideba
@search @endsearch or @title('something')
但是如果我想做下面的事情呢
@cache('sidebar',10,[$silver,$gold,$platinum])
html tags come here
@endcache
目前我是这样做的
@cache('sidebar_',10,function() use ($silver_sidebar,$gold_sidebar))
@endcache
$pattern = Blade::createOpenMatcher('cache');
$replace = "<?php echo PageCache::cache$2 { ?>";
$view = preg_replace($pattern, $replace, $view);
// Replace closing tag
$view = str_replace('@endcache', '<?php }); ?>', $view);
@cache('sidebar',10,function()使用($silver\u sidebar,$gold\u sidebar))
@端缓存
$pattern=Blade::createOpenMatcher('cache');
$replace=“”;
$view=preg_replace($pattern,$replace,$view);
//替换结束标记
$view=str_replace(“@endcache”,”这个问题现在已经有一年多的历史了,但是如果将来有人需要,我会分享一个解决方案
使用第一个示例:
@cache('sidebar', 10, [ $silver, $gold, $platinum ])
html tags come here
@endcache
可以这样做:
Blade::extend(function ($view) {
$pattern = Blade::createOpenMatcher('cache');
$pattern = rtrim($pattern, '/') . '(.*?)@endcache/s';
$matches = [];
preg_match($pattern, $view, $matches);
$content = '';
if (count($matches) > 3) {
$content = addslashes($matches[3]);
}
$replace = "<?php echo PageCache::cache$2, '{$content}'); ?>";
$view = preg_replace($pattern, $replace, $view);
return $view;
});
根据上述示例,您将有:
$name = 'sidebar';
$num = 10;
$args = [ $silver, $gold, $platinum ];
我也只是简单地返回示例中的内容,但是您可以在那里做一些更有趣的事情
$name = 'sidebar';
$num = 10;
$args = [ $silver, $gold, $platinum ];