I'm looking to insert some similar tags like {{widget.social.facebook}} into an instance of ckeditor.
The plan is to run them through either Modules::run() or widgets::run() method.
My initial approach is to start out with preg_replace_callback and a closure.
I have done something similar in the past, however this was just a simple string replace from array data. I am thinking of a similar approach possibly.
$callback = function ($matches) use ($data)
{
return ( isset($data[$matches[1]]) )
? $data[$matches[1]]
: $matches[0];
};
return preg_replace_callback(
'/\{(.*?)\}/',
$callback,
$this->template);
I just cant get my brain around the best approach right now(brain melt), hopefully you guys might be able to shed some light
Ok so I managed to get it working...
public function _tags( $html ){
$callback = function( $matches ){
list($module, $method, $param) = explode('.', $matches[1]);
return isset( $matches[1] )
? Modules::run("${module}/_${method}", $param)
: $matches[0] ;
};
return preg_replace_callback('/\{{module.(.*?)\}}/', $callback, $html);
}
public function _test($id=null){
echo "Test Replace is done ${id}";
}
public function _test2($id=null){
echo "Test2 Replace is done ${id}";
}
-
Test data
<section>
{{module.pages.test.25}}
</section>
<aside>
//{{module.sidebar}}
{{module.pages.test2.25}}
Blaa I'm not sure I like this html scraping method.
Open to suggestions/improvements