📜 ⬆️ ⬇️

Create your own theme for the symfony CRUD generator

Problem


We like the Symfony MVC Framework for its set of ready-made tasks that allow us to speed up application development. There is a useful command in symfony - CRUD (from create, read, update, delete) generator. But, to be honest, the HTML code generated by it has several disadvantages:

I propose to get rid of these shortcomings by creating your own theme for a CRUD generator.

Decision


I suggest that you use symfony 1.4 and Doctrine ORM 1.2

Step 1

Copy the standard CRUD generator theme from %sf_symfony_lib_dir%/plugins/sfDoctrinePlugin/data/generator/sfDoctrineModule/default to
data/generator/sfDoctrineModule/mytheme to the root directory of the project.

Step 2

Edit the template files.
//data/generator/sfDoctrineModule/mytheme/template/templates/_form.php
[?php use_stylesheets_for_form($form) ?]
[?php use_javascripts_for_form($form) ?]

<? php $ ticket $ form = $ this- > getFormObject() ? >
<? php if ( isset ($ this- > params['route_prefix']) && $this- > params['route_prefix']): ? >
[?php echo form_tag_for($form, '@ <? php echo $ this- > params['route_prefix'] ? > ') ?]
<? php else: ? >
< form action ="[? php echo url_for ('&# 60 ;? php echo $ this- > getModuleName() ? > /'.($form- > getObject()- > isNew() ? 'create' : 'update').(!$form- > getObject()- > isNew() ? '? <? php echo $ this- > getPrimaryKeyUrlParams('$form- > getObject()', true) ? > : '')) ?]" method="post" [?php $form- > isMultipart() and print 'enctype="multipart/form-data" ' ?] >
[?php if (!$form- > getObject()- > isNew()): ?]
< input type ="hidden" name ="sf_method" value ="put" />
[?php endif; ?]
<? php endif ;? >
< div >
<? php if ( isset ($ this- > params['non_verbose_templates']) && $this- > params['non_verbose_templates']): ? >
[?php echo $form ?]
<? php else: ? >
[?php echo $form- > renderGlobalErrors() ?]
<? php foreach ($ form as $ name = > $field): if ($field- > isHidden()) continue ? >
< div >
[?php echo $form[' <? php echo $ name ? > ']- > renderLabel() ?]
[?php echo $form[' <? php echo $ name ? > ']- > renderError() ?]
[?php echo $form[' <? php echo $ name ? > '] ?]
</ div >
<? php endforeach ; ? >
<? php endif ; ? >
< div >
<? php if (! isset ($ this- > params['non_verbose_templates']) || !$this- > params['non_verbose_templates']): ? >
[?php echo $form- > renderHiddenFields(false) ?]
<? php endif ; ? >
<? php if ( isset ($ this- > params['route_prefix']) && $this- > params['route_prefix']): ? >
  < a href ="[? php echo url_for ('&# 60 ;? php echo $ this- > getUrlForAction('list') ? > ') ?]" > Back to list </ a >
<? php else: ? >
  < a href ="[? php echo url_for ('&# 60 ;? php echo $ this- > getModuleName() ? > /index') ?]" > Back to list </ a >
<? php endif ; ? >
[?php if (!$form- > getObject()- > isNew()): ?]
<? php if ( isset ($ this- > params['route_prefix']) && $this- > params['route_prefix']): ? >
  [?php echo link_to('Delete', ' <? php echo $ this- > getUrlForAction('delete') ? > ', $form- > getObject(), array('method' = > 'delete', 'confirm' = > 'Are you sure?')) ?]
<? php else: ? >
  [?php echo link_to('Delete', ' <? php echo $ this- > getModuleName() ? > /delete? <? php echo $ this- > getPrimaryKeyUrlParams('$form- > getObject()', true) ? > , array('method' = > 'delete', 'confirm' = > 'Are you sure?')) ?]
<? php endif ; ? >
[?php endif; ?]
< input type ="submit" value ="Save" />
</ div >
</ div ></ code >
</ form >


* This source code was highlighted with Source Code Highlighter .

This is just an example. You can also modify other template files in the data/generator/sfDoctrineModule/mytheme/template/templates folder.
')
Step 3

Create your own form formatting class. Create a sfWidgetFormSchemaFormatterDiv.class.php and put it in the root directory of the project.
//lib/widet/sfWidgetFormSchemaFormatterDiv.class.php
class sfWidgetFormSchemaFormatterDiv extends sfWidgetFormSchemaFormatter
{
protected
$rowFormat = "<div>\n %error%%label%\n %field%%help%\n%hidden_fields%</div>\n" ,
$nestedFormFormat = "%field%" ,
$errorRowFormat = "<div>\n%errors%</div>\n" ,
$helpFormat = '<br />%help%' ,
$decoratorFormat = "\n %content%" ;
}


* This source code was highlighted with Source Code Highlighter .

Now we need to set up a project or application to use the created form formatting class. Depending on whether you want to set up a project ( config/ProjectConfiguration.class.php ) or an application ( apps/myapp/config/myappConfiguration.class.php ), change the corresponding configuration file.
public function configure()
{
sfWidgetFormSchema::setDefaultFormFormatterName( 'div' );
}

* This source code was highlighted with Source Code Highlighter .

Step 4. Last

Run the symfony doctrine:generate-module task.

php symfony doctrine:generate-module myapp mymodule MyModel --theme=mytheme

Replace myapp with the name of your application (for example, frontend ), mymodule with the desired module name (for example, post ) and MyModel with the name of your model (for example, Post ).

Voila! Now you have a clean, non-fixed layout.

All the best!

Source: https://habr.com/ru/post/99095/


All Articles