Blog

Assign variables to a Laravel Blade template using an associative Array can be more code efficient when using PHP's compact function. Therefor i needed a script that allows me to manually replace the code into this shorter format:

Before

return View::make('account.edit', [
    'oUser' => $oUser,
    'sTitle' => $sTitle,
]);

After

return View::make('account.edit', compact(
    'oUser',
    'sTitle'
));
  • In TextMate go to Bundles -> *Edit Bundles... *
  • Press + N
  • Create Bundle
  • Repeat, and create Command
  • Assign a Key Equivalant for quick access (optional)
  • copy-paste the code below:
#!/usr/bin/php
<?php
$a = explode(PHP_EOL, file_get_contents("php://stdin"));
$c = count($a);
$i = 1;
foreach ($a as $s) {
    if ($i == 1) {
        echo str_replace("[", "compact(", $s)."\n";
    } else if ($i == $c) {
        echo str_replace("])", "))", $s);
    } else {
        $pos1 = strpos($s, "'");
        $pos2 = strpos($s, "'", $pos1 + strlen("'"));
        echo substr($s, 0, $pos2)."'";
        if ($i < ($c - 1)) {
            echo ",";
        }
        echo "\n";
    }
    ++$i;
} 
?>
Categorieën
Laravel
Zoeken