• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Parser function

#1
Lightbulb 
I'm wondering why CodeIgniter doesn't provide parser function to grab Parser Class Huh , so we can use it directly anywhere without calling it as instance.  Idea

for example:
before "as documentation":
PHP Code:
/* loading parser class through its service. */
$parser = \Config\Services::parser();
/* or loading it by instantiate it directly. */ 
$parser = new \CodeIgniter\View\Parser();

/* Then we can use it like this. */
$parser->setData($data)->render('index'); 

I wrote this function and placed it in app/Common.php, "actually just copy and paste" Big Grin  system/Common.php view function and do some few changes to it.
PHP Code:
function parser(string $name, array $data = [], array $options = []): string
{
    
/**
     * @var CodeIgniter\View\Parser $renderer
     */
    
$parser = \Config\Services::parser();

    
$saveData null;
    if (
array_key_exists('saveData'$options) && $options['saveData'] === true)
    {
        
$saveData = (bool) $options['saveData'];
        unset(
$options['saveData']);
    }

    return 
$parser->setData($data'raw')->render($name$options$saveData);


after:
PHP Code:
/* now we can use it in one short line. */
parser('index'$data); 

it's works as expected, so can it added officially to CodeIgniter 4?  Exclamation

btw I couldn't pull a request on github it's so difficult there.  Confused
Reply

#2
You can add your code to github then give description there. Most developer have their own style, I think asking request in CI repo is about core of CI
Hi! I am Jamil
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.