Support the ongoing development of Laravel.io →
Views Blade
Last updated 1 year ago.
0

Hey domeinznl,

I plugged this in to the previous code we had.

A couple things,

This won't work, it generates the wrong pattern

$pattern = $compiler->createPlainMatcher('@dmz_route');

/(?<!\w)(\s*)@@dmz_route(\s*)/

The other two options in the compiler won't work either, createOpenMatcher or createPlainMatcher

So a custom pattern to match the @dmz_route('string')

$pattern = '/(?<!\w)(\s*)@dmz_route(\s*\((.*)\))/';

/* - should return 
            	 array (size=4)
				  0 => string '@dmz_route('login')' (length=22)
				  1 => string '' (length=3)
				  2 => string '('login')' (length=9)
				  3 => string ''login'' (length=7)
*/

I removed the extra param $compiler, don't think it would have worked as the calling function in the compiler only passes in one var, the view string.

If you need to do that on a closure try this,

$this->blade->getCompiler()->extend(function($view)  use ($compiler)  { ...

Here is what I got working,

$this->blade->getCompiler()->extend(function($view) 
            {            	
            	
            	// custom regex
            	$pattern = '/(?<!\w)(\s*)@dmz_route(\s*\((.*)\))/';
            	
            	/* - should return in a preg_match
            	 array (size=4)
				  0 => string '@dmz_route('login')' (length=22)
				  1 => string '' (length=3)
				  2 => string '('login')' (length=9)
				  3 => string ''login'' (length=7)
            	 */
            	
            	// adjusted to use #3 var
             	$replace = '<?php global $router; echo $router->generate($3); ?>';            	            	            	
            
            	// should replace @dmz_route('login') with  
            	/* 
            	 * <?php global $router; echo $router->generate('login'); ?> 
            	 */     
            	return preg_replace($pattern, $replace, $view);
            });

Hope that helps

0

Thank you TerrePorter,

It works just as it should. Do you know where I can find some good articles about regex ? Don't know anything about that.

Greetings, Domeinznl

0

Glad it worked for your.

Actually I don't know of any good resources, I usually just do searches or look at the preg_match reference. Along with a lot of trial and error. The Reg Coach is very useful in testing regex, http://weitz.de/regex-coach/

0

Sign in to participate in this thread!

Eventy

Your banner here too?

domeinznl domeinznl Joined 7 Sep 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.