Templated Records
Ensō Core provides traits for easier management of Models with blade templates
Model
Add the HasTemplates trait to the Crud Model.
The Model must have at least a database column to store the name of the template selected. It should also have an identifier if you wish to allow for custom, model-specific templates that don't appear in the standard template list. These may be configured by overriding getTemplatedItemColumnName and getTemplatedItemIdentifierName respectively, and default to template and slug.
Standard blade templates should be stored in resources/views/{plural crud_name}/templates/. Page templates would be in resources/views/pages/templates/. This may be changed by overriding the getCustomTemplateDirectory function on the Model.
Model specific templates should be stored in resources/views/{plural crud_name}/{singular crud_name}-specific-templates/. This may be changed by overriding the getCustomTemplateDirectory function on the Model.
If you want to use model-specific templates, you should ensure that your crud configuration (config/enso/crud/{crud_name}.php) contains the an array key of templated_records. The value paired with this should be an array where each element is either a simple model identifiers (slug, or as defined by your custom setup) or a key value pairs of identifier => template.path, or a mixture of the two. The following is a valid listing for the Page Crud:
return [
...
'templated_records' => [
'about-us',
'home' => 'home',
],
...
]
and will result in:
- The
about-usPage will use the template found inresources/views/pages/page-specific-templates/about-us.blade.php - The
contact-usPage (with atemplateattribute ofdefault) will use the template found inresources/views/pages/templates/default.blade.php - The
homePage will use the template found inresources/views/home.blade.php
Config
Add the ModelHasTemplates trait to your Crud Config.
This will provide a getTemplateList function that can be called to generate a list of template files to populate a SelectField, where the key for each option is the name of the template (without the .blade.php suffix) and the value if human-friendly name string. The template resources/views/pages/templates/has-sidebar.blade.php would become the option:
'has-sidebar' => 'Has sidebar'
You can pass a Model class string into this function if you need to list for a specific datatype, or you can let the Config work out the correct datatype via the crud configuration.
Controller
To allow your controller to automatically remove the template field from the Form when the model being edited has a custom template, you should add the ControlsTemplatedRecords trait to the relevant Crud Controller, and then update/override the configureHooks() function to include a call to $this->configureTemplatedRecords();.
By default, this will remove the field defined by a getTemplatedItemColumnName call on the related Model (as Field names must match the column they are saved into), from the main section.
If your template field is in a different section, you may return the name of a different section by overriding the getTemplatedRecordSectionName function provided by the trait.