Skip to content

Instantly share code, notes, and snippets.

@nerdmom
Created June 3, 2012 10:46
Show Gist options
  • Save nerdmom/2863000 to your computer and use it in GitHub Desktop.
Save nerdmom/2863000 to your computer and use it in GitHub Desktop.
Language Laravel Blade Extension
<?php
/**
* Compiles language strings
* Standalone:
* {{"some.string_here"}} = <?php echo \Lang::line("some.string_here")->get(); ?>
* Not Standalone (ie, within a function,etc)
* {"some.string_Here"} = \Lang::line('some.string_here')->get()
* @param string Template
* @return string Template
**/
Blade::extend(function($value) {
$func = function($template,$regexed,$l=null,$r=null) {
$c = count($regexed[0]);
// 0 = matched string 1 = language line
for ($i=0; $i < $c; $i++)
{
$str = '\\Lang::line('.$regexed[1][$i].')->get()';
$template = str_replace($regexed[0][$i], $l.$str.$r, $template);
}
return $template;
};
$regex = '([\"|\'].+[\"|\'])';
preg_match_all('/\{\{'.$regex.'\}\}/mU', $value, $matches);
$value = $func($value,$matches,'{{','}}');
preg_match_all('/\{'.$regex.'\}/mU', $value, $matches1);
$value = $func($value,$matches1);
return $value;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment