-1

I have 8 content types and 4 of them are the same and the difference is just their name. they all have a page view and the way I display them on screen is the same. instead of having 8 pages. (VIEWNAME_i stands for the i-th view.)

  • views-view--VIEWNAME_i--page.tpl
  • views-view-field--VIEWNAME_i--page.tpl

I need a template function to reduce these 8 pages into 2. I want to define a general page for views-view--VIEWNAME_i--page.tpl. I would assign every view's variables in this function. I also want to do the same for fields page.

  • 2
    If all of this is so similar, I'd suggest creating one View, but with multiple page displays, each per content type. Then you could create templates for that View. Plus, it sounds like the only thing you are changing in the View is that content type, so, might make things a bit easier to maintain if you ever needed to change things. – Jimajamma Sep 25 '13 at 10:06
  • unfortunately I can't do this now, because these every view must have a block and this block must be displayed in quicktab content (although blocks same as pages are the same), I did lot's of work to do this, create block.tpl.php – M a m a D Sep 25 '13 at 13:56
7

Same template file for multiple view pages

You don't need to use any preprocess function to call the same template file for multiple view pages.

Just edit the view, click on edit view name/description and add the View tag

enter image description here

Now, create template file views-view--my-custom-view.tpl.php and mark any view with the same tag, so all of them will use the same template file.

Same template file for multiple view pages and displays

If you have multiple views with the number of displays attached to them, and you need to define one template file for any view or display, you can achieve it with the following approaches:

Non-coding solution

You can create a separate view for each view display, and mark the view with the appropriate tag, for example:

view_custom_pages
  View tag: custom-pages
  displays: Page1, Page2, Page3 ...

view_custom_blocks
  View tag: custom-blocks
  displays: Block1, Block2, Block3 ...

Each page from view_custom_pages will use views-view--custom-pages.tpl.php

Each block from view_custom_blocks will use views-view--custom-blocks.tpl.php

Coding solution

If the above mentioned solution does not satisfy your needs, then you can use template_preprocess_views_view() to define theme suggestion for any view display

/**
 * Implements template_preprocess_views_view().
 */
function YOURTHEME_OR_MODULE_preprocess_views_view(&$vars) {

  $view = $vars['view'];

  // an array with views and displays which should use only ONE template file.
  $shared_themes = array(
    'SOME_VIEW_NAME' => array(
      array(
        'displays' => array(
          'page',
        ),
        'theme_suggestion' => 'views_view__custom_pages'
      ),
      array(
        'displays' => array(
          'block',
          'block_1'
        ),
        'theme_suggestion' => 'views_view__custom_blocks'
      ),
    ),
    'ANOTHER_VIEW_NAME' => array(
      array(
        'displays' => array(
          'page',
        ),
        'theme_suggestion' => 'views_view__another_custom_pages'
      ),
      array(
        'displays' => array(
          'block',
          'block_1',
          'block_2'
        ),
        'theme_suggestion' => 'views_view__another_custom_blocks'
      ),
    )
  );

  // use ONE template file for multiple views and displays.
  if (in_array($view->name, array_keys($shared_themes))) {
    foreach ($shared_themes[$view->name] as $key => $data) {
      if (in_array($view->current_display, $data['displays'])) {
        $vars['theme_hook_suggestion'] = $shared_themes[$view->name][$key]['theme_suggestion'];
      } 
    }
  }
}

Override or insert variables into the view template

Create common variables for your custom template using template_preprocess_views_view()

function YOURTHEME_OR_MODULE_preprocess_views_view(&$vars) {
  $view = $vars['view'];
  switch ($view->name) {

    case 'SOME_VIEW_NAME' :
      // here get some field value from view and create a variable
      $vars['some_variable'] = 'some value';
    break;

    case 'ANOTHER_VIEW_NAME' :
      // here get some field value from view and create a variable
      // use the same names in order to use it by one template file.
      $vars['some_variable'] = 'some value for another view';
    break;

  }
}

Notice: It is the same hook as mentioned in the coding solution.

  • Thanks a lot, this method worked but I have a block display of this view as well and in this way that block will be effected as well, I tried views-view--my-custom-view--page.tpl.php but didnt work – M a m a D Sep 29 '13 at 8:41
  • I did that before, I have created a block display for every view, for example views-view--sabk_zendegi--block.tpl; but it is still effected by views-view--my-custom-view--page.tpl.php. – M a m a D Sep 29 '13 at 13:59
  • You asked how to use one template file for multiple view pages and this was answered pretty well. Now you're asking about using the same template for blocks from different views. This is a quite different question, so ask the new one. – Jack-PL Sep 30 '13 at 10:10
  • at this stage it is not important whether block template files are the same or not, I'm just trying to have ONE template file for page display, your answer is right but it effects the block display as well, that would be much more better if your solution was applicable just for pages not the blocks. – M a m a D Sep 30 '13 at 13:49
  • 1
    Yes. You can achieve it by using the same code in template_preprocess_views_view_fields(). I edited the answer and changed $variables to $vars just in order to meet the names conventions. – Jack-PL Oct 2 '13 at 12:30
1

Pretty quick and dirty way would be to simply have one file under multiple names. Main filesystems for Linux, and NTFS in Windows support both soft and hard links. Only this kind of link is called Junction in Windows. When you do that, Drupal will see these as separate files, but you will be able to edit them as one.

  • good idea but how? and is it possible to do such thing on server? – M a m a D Sep 25 '13 at 13:58
  • @Mohammad depends on server. If you have shell access, it's ln -s original.tpl.php copy.tpl.php in the directory that contains original. – Mołot Sep 25 '13 at 14:01
1

You probably can use a theme for that

Use hook_theme() in your custom module and call the theme() function directly in your view's .tpl file

With hook_theme() your can pass arguments to an other .tpl.php file

It's probably lighter to have 8 .tpl.php files associated to hook_theme() than severals view's .tpl suggestions

An example of use of hook_theme()

/**
 * Implements hook_theme().
 */
function yourmodule_theme($existing, $type, $theme, $path) {

return array(

  'my_theme' => array(
      'template' => 'your_theme_file', //your_theme_file.tpl.php in fact
      'variables' => array(
        'your_variable' => array(),
      )
  ),
}     

Your your_theme_file.tpl.php file contains :

<?php

print '<div>'. $your_variable .'</div>

You can use this theme everywhere even in your views templates :

theme('my_theme', array('your_variable' => 'foo'));
  • I'm almost beginner to drupal theme developing would you please explain a bit more with function and parameters inside it and how can I use it in tpl.php ? – M a m a D Sep 25 '13 at 14:02
1

You probably can use a theme for that

Use hook_theme() in your custom module and call the theme() function directly in your view's .tpl file

With hook_theme() your can pass arguments to an other .tpl.php file

It's probably lighter to have 8 .tpl.php files associated to hook_theme() than severals view's .tpl suggestions

An example of use of hook_theme()

function yourmodule_theme($existing, $type, $theme, $path) {

return array(

   'my_theme' => array(
   'template' => 'your_theme_file', //your_theme_file.tpl.php in fact
   'variables' => array(
   'your_variable' => array(),
  )
  ),
}  

Your your_theme_file.tpl.php file contains :

print '<div>'. $your_variable .'</div>

You can use this theme everywhere even in your views templates :

theme('my_theme', array('your_variable' => 'foo'));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.