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:
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?
[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']
}
p.s. this function doesn't support advanced blade syntax such like @extend('xxx')