简体   繁体   English

刀片视图模板中使用的所有变量的请求列表

[英]Request list of all variables used in blade view template

We have a series of blade view templates that are out going emails and would like to create a simple way for our in house administration to preview these emails without actually sending them.我们有一系列用于发送电子邮件的刀片式视图模板,并希望为我们的内部管理创建一种简单的方法来预览这些电子邮件而无需实际发送它们。

I could keep tract of, and provide filler variables but am very curious if there is a way to request a list of variables used in a view?我可以保留并提供填充变量,但我很好奇是否有办法请求视图中使用的变量列表?

For example, I have a basic view "greeting.blade.php" that says:例如,我有一个基本视图“greeting.blade.php”,上面写着:

Dear {{$customerFirstName}} {{$customerLastName}},

I'd like to:我想:

$usedVariablesArray = getVariablesFromView("greeting");

And have it return:让它返回:

['customerFirstName', 'customerLastName']

Is there anything built into laravel that provides this kind of functionality? laravel 中是否内置了提供这种功能的东西?

[EDIT] I'd like to do this from outside the view file in question. [编辑] 我想从有问题的视图文件外部执行此操作。

public function previewEmailTemplate($templateName) {
    $usedVariables = $getArrayOfVariables($template);
    // Would return ['customerFirstName', 'customerLastName']

    foreach($usedVariables as $aUsedVariable) {
        $dummyValues[$aUsedVariable] = $aUsedVariable;
    }

    return view($template, $dummyValues)->render();
}

So this function would render the template with the variable names in the place of the variables.所以这个函数会用变量名代替变量来渲染模板。

Does that make my question clearer?这会让我的问题更清楚吗?

I also need such function but not find on the net.我也需要这样的功能,但在网上找不到。

I write a simple function to do it.我写了一个简单的函数来做到这一点。

May help you可以帮到你

greeting.blade.php

Dear {{$customerFirstName}} {{$customerLastName}},

functions.php

function getVariablesFromView($templatePath)
{
    // $templatePath = '/resources/views/mails/greeting.blade.php'
    $contents = file_get_contents($templatePath);

    $re = '/{{[\ ]*\$[a-zA-Z]+[\ ]*}}/m';
    preg_match_all($re, $contents, $matches, PREG_SET_ORDER, 0);
    $flatternArray = [];
    array_walk_recursive($matches, function ($a) use (&$flatternArray) {$flatternArray[] = $a;});
    $variables = str_replace(['{{', '}}', '$', ' '], '', $flatternArray);

    return $variables;
    // ['customerFirstName', 'customerLastName']
}

ps this function doesn't support advanced blade syntax such like @extend('xxx') ps 此函数不支持高级刀片语法,例如@extend('xxx')

Thank you for the first answer to this question 董承樺, my answer is merely an extension of what you wrote to cover slightly more use cases.谢谢你第一个回答这个问题董承桦,我的回答只是你写的内容的扩展,涵盖了更多的用例。 Feel free to update your answer with this improved code if you like.如果您愿意,请随时使用此改进后的代码更新您的答案。

Specifically, this version support php variable names that contain underscore like $very_good_variable etc. It also supports the "unescaped" Laravel Blade syntax, which is {., $something_something,.} instead of {{ $something_something }}.具体来说,此版本支持包含下划线的 php 变量名称,如 $very_good_variable 等。它还支持“未转义”的 Laravel Blade 语法,即 {., $something_something,.} 而不是 {{ $something_something }}。 Regex is not my strong suit, and I am sure there is way to get this to work with more succint code, but it does seem to work effectively. Regex 不是我的强项,我相信有办法让它与更简洁的代码一起工作,但它似乎确实有效。

function getVariablesFromView(($templatePath){
                        //this version tests for standard blade variables... of the form {{ $something_something }}
                        $re = '/{{[\ ]*\$[a-zA-Z_]+[\ ]*}}/m';
                        preg_match_all($re, $contents, $matches, PREG_SET_ORDER, 0);
                        $flatternArray = [];
                        array_walk_recursive($matches, function ($a) use (&$flatternArray) {$flatternArray[] = $a;});
                        $regular_variables = str_replace(['{{', '}}', '$', ' '], '', $flatternArray);

                        //this version tests for unescaped blade variables... of the form {!! $something_something !!}
                        $re = '/{!![\ ]*\$[a-zA-Z_]+[\ ]*!!}/m';
                        preg_match_all($re, $contents, $matches, PREG_SET_ORDER, 0);
                        $flatternArray = [];
                        array_walk_recursive($matches, function ($a) use (&$flatternArray) {$flatternArray[] = $a;});
                        $unescaped_variables = str_replace(['{!!', '!!}', '$', ' '], '', $flatternArray);

                        $variables = array_merge($regular_variables,$unescaped_variables);
                        return $variables;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM