Default Symfony1 templates and backups or sharing templates between modules

I am working on a Symfony1 application.

For the application part, we are using the symfony admin generator, which builds default actions and templates for a specific module.

Auto-generated action methods can be overridden in the child actions class, and templates can be overridden by making a single name with the name in the module's template folder. Local templates are used instead of auto-generated ones, which are stored in the cache folder (I assume this is normal symfony behavior).

apps/
    my_app
        modules/
            post/
                actions/
                    actions.class.php
                lib/
                templates/
...
cache/
    my_app/
        environment/
            modules/
                autoPostcd /
                    actions/
                        actions.class.php
                    lib/
                    templates/
                        indexSuccess.php
                        _part_1.php
                        _part_2.php

      

I am currently working on a standalone application that does not use the admin generator.

But I have 3 modules that do very similar things that I would like to share.

I have all 3 activities extending the same custom activity class so that they all implement the same methods and share the ones that are identical.

The problem I am facing is template sharing. Master templates and most partial elements can be reused as they are.

apps/
    other_app/
        lib/
            printingActions.class.php
        modules/
            ticket/
                actions/
                    actions.class.php
                lib/
                templates/
                    printSuccess.php //exactly the same for all 3
                    _part_1.php
                    _part_2.php      //exactly the same for all 3
                    _part_3.php
            receipt/
                actions/
                    actions.class.php
                lib/
                templates/
                    printSuccess.php //exactly the same for all 3
                    _part_1.php
                    _part_2.php      //exactly the same for all 3
                    _part_3.php
            voucher/
                actions/
                    actions.class.php
                lib/
                templates/
                    printSuccess.php //exactly the same for all 3
                    _part_1.php
                    _part_2.php      //exactly the same for all 3
                    _part_3.php

      

What I would like to do is pull out the common ones so that each module and any future modules with a similar interface should only have partial data with specific information about the module.

This would be my ideal setup:

apps/
    other_app/
        lib/
            printingActions.class.php
            printingCommonTemplates/
                printSuccess.php //exactly the same for all 3
                _part_2.php      //exactly the same for all 3
        modules/
            ticket/
                actions/
                    actions.class.php
                lib/
                templates/
                    _part_1.php
                    _part_3.php
            receipt/
                actions/
                    actions.class.php
                lib/
                templates/
                    _part_1.php
                    _part_3.php
            voucher/
                actions/
                    actions.class.php
                lib/
                templates/
                    _part_1.php
                    _part_3.php

      

I know it can be done since the admin generator does it, but after much tearing I can't find exactly where it does it.

Can anyone point me in the right direction for this? Ideally, if there is a fallback template parameter that I can set for a specific module or filter class that I can extend to do what I need?

+3


source to share


2 answers


If you want to use a generic module action class with a default layout, you can use the following approach:

class printingActions extends sfActions {

  public function preExecute() {
    $this->setLayout('print_success_common');
  }

  public function executeIndex() {

  }
}

      

Then, in your module action, you can:



class ticketActions extends printingActions
{
   public function executePrint(sfWebRequest $request)
   {
      $this->txt = 1234;
      return $this->renderPartial('part_1', array('txt' => $this->txt));
   }
}

      

You can use a different (generic) layout from your activity using:

class ticketActions extends printingActions
{
   public function executePrint(sfWebRequest $request)
   {
      $template = $this->getContext()->getConfiguration()->getTemplateDir('printing', 'print_success_common_alt.php');
      $this->setLayout($template . '/print_success_common_alt');
      ...

      $this->txt = 1234;
      return $this->renderPartial('part_1', array('txt' => $this->txt));
   }
}

      

+2


source


I found a partial solution, at least to be able to share templates.

The Partials section of the docs for symfony1 mentions that there are 3 places you can call partial:

  • From the current module
  • From another module in the same application
  • In the global templates folder for the current application

So, I can make shared partials in one place and reference them from every module that needs to use them.

I can also pass a variable with the current module name to dynamically call special module templates from common ones.

I considered putting the shared partial data directly into the global template directory, but it would get messy and confusing if it was more than one type of module that did it.

The awkward job was creating a directory inside the template directory where I could put these files.

apps/
  other_app/
    lib/
      printingActions.class.php
    modules/
      ticket/
        actions/
          actions.class.php
        lib/
        templates/
          printSuccess.php //common parts extracted to partial
          _part_1.php
          _part_3.php
      receipt/
        actions/
          actions.class.php
        lib/
        templates/
          printSuccess.php //common parts extracted to partial
          _part_1.php
          _part_3.php
      voucher/
        actions/
          actions.class.php
        lib/
        templates/
          printSuccess.php //common parts extracted to partial
          _part_1.php
          _part_3.php
    templates/
      _printing_common/
        print_success_common.php //common parts of PrintSuccess extracted as partial
        part_2.php      //exactly the same for all 3

      



He created a less ideal problem to underline the new directory and remove the underline from the partial elements inside.

But using this method I was able to share the common bits and specify the specific module code to be listed in the module template directories.

Here is an example of the contents of some of these files:

Applications / other_app / modules / tickets / templates / printSuccess.php

<?php
  include_partial(
    'global/printing_common/print_success_common',
    array(
      'moduleNameText' => $moduleNameText
    )
  );

      

Applications / other_app / templates / _printing_common / print_success_common.php

...
<?php include_partial($moduleNameText.'/part_1',array('moduleNameText' => $moduleNameText)); ?>
...
<?php include_partial('global/printing_common/part_2',array('moduleNameText' => $moduleNameText)); ?>
...
<?php include_partial($moduleNameText.'/part_3',array('moduleNameText' => $moduleNameText)); ?>

      

This is not a perfect solution, so I'll leave this question open for a better solution, but this is my job so far.

+1


source







All Articles