dream_wu2015 2014-07-08 02:10
浏览 52
已采纳

Laravel扩展刀片并传递数组作为参数

I have searched a lot regarding this topic and didn't find much on the web so I started to research and create a complete article on this topic but I am unable to understand some things here, making basic custom tags in blade is easy like

@search @endsearch or @title('something')

but what if I want to do something like below

  1. @cache('sidebar',10,[$silver,$gold,$platinum])
  2. html tags come here
  3. @endcache

At present I am doing it like this

  1. @cache('sidebar_',10,function() use ($silver_sidebar,$gold_sidebar))
  2. @endcache
  3. $pattern = Blade::createOpenMatcher('cache');
  4. $replace = "<?php echo PageCache::cache$2 { ?>";
  5. $view = preg_replace($pattern, $replace, $view);
  6. // Replace closing tag
  7. $view = str_replace('@endcache', '<?php }); ?>', $view);

How to parse it to separate three parameters and get content between end and start tag? Your help is appreciated. Thanks for your responses.

  • 写回答

1条回答 默认 最新

  • duanban4769 2015-08-10 11:50
    关注

    This question is more than 1 year old now, but I'm sharing a solution if somebody else need it in the future.

    Using the first example:

    1. @cache('sidebar', 10, [ $silver, $gold, $platinum ])
    2. html tags come here
    3. @endcache

    It's possible to do something like this:

    1. Blade::extend(function ($view) {
    2. $pattern = Blade::createOpenMatcher('cache');
    3. $pattern = rtrim($pattern, '/') . '(.*?)@endcache/s';
    4. $matches = [];
    5. preg_match($pattern, $view, $matches);
    6. $content = '';
    7. if (count($matches) > 3) {
    8. $content = addslashes($matches[3]);
    9. }
    10. $replace = "<?php echo PageCache::cache$2, '{$content}'); ?>";
    11. $view = preg_replace($pattern, $replace, $view);
    12. return $view;
    13. });

    Explaining the code:

    1. We extend the pattern to match the content between @cache and @endcache. Note the use of s modifier in the expression. This way we can match multiple lines with a . (dot).
    2. Check if the expression matched something using count($matches) and assign it to $content.
    3. Finally we replace the blade tag by the function call. Since our pattern already matches the open and close tags and all content between them, we don't need to worry about replace the closing tag or something else.

    This way you can get the content between tags (@cache and @endcache) in your function:

    1. class PageCache {
    2. public static function cache($name, $num, $args, $content) {
    3. return stripslashes($content);
    4. }
    5. }

    Based on the example above you'll have:

    1. $name = 'sidebar';
    2. $num = 10;
    3. $args = [ $silver, $gold, $platinum ];

    I'm also simply returning the content in my example, but you can do something more interesting there.

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部