Welcome Guest, Not a member yet? Register   Sign In
Custom Generators using Spark?
#1

Hello, 
I've tried making my own make:view generator for rest of my group, who find it "too difficult" to add bit of conditional template loading in every view file. Since this is best way we've figured to make it so that we can load just view, with conditionally loading whole template around it. 
TL;DR: I would like to request bit more documentation and/or clarification on how to make own generators with spark. 
I tried looking into make:command's documentation, but in the official document I can only find to use "--type generator". This creates new command with Generator trait. But nowhere -- that I can find -- is explained where should I put my own tpl.php files so the CI4 can find them when needed. Obviously system/ files are dangerous place, but should I put them in App/Generators/ or App/Commands/ or is there specific other place this feature looks into?
So if you could clarify these three things:
1. Where am I supposed to put my template files, so my custom command can find it?
2. Additional documentation how the tpl files are used would be nice. I can see it uses {} templating syntax, and guess that words inside the curly brackets are pulled from parameters and what I set manually in the command. But you know, at least would be nice to confirm this. 
3. On the generator page, I would recommend adding one example use of options, I found the syntax very unintuitive, and was trying with --type=generator etc for a while before realizing there should be whitespace instead. Having example on the page would help figuring this out.
Reply
#2

Hello,
Use the command below to build yours command.

Code:
php spark make:command viewGenerator --command make:view --type generator --group "My Custom Command"

After that, change the app\Commands\ViewGenerator.php file to the following:

PHP Code:
          
$this
->component 'View';
$this->directory 'Views';
$this->template 'myview.tpl.php'

Create a template file in the following path.
app\Commands\Generators\Views\myview.tpl.php

To introduce the template(myview.tpl.php), add the following code through the app\Config\Generators.php file.

PHP Code:
public array $views = [
//...
'make:view' => '\App\Commands\Generators\Views\myview.tpl.php',
]; 


We have a good example at shield:model, if you want to take a look.
Reply
#3

@datamweb Thank you, very useful.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB