开发者

custom page.tpl.php for a drupal view

How can I create a custom page.tpl.php for a specific view?

I'm not talking about styling 开发者_JAVA技巧the view itself, just the page where that view gets rendered.

Thank you.

@Keith Morgan - It's page.


Per Allartk's solution
In Drupal 7 template.php:

function <theme>_preprocess_page(&$variables) {
    if (($views_page = views_get_page_view()) && $views_page->name === "galleries") {
      $variables['theme_hook_suggestions'][] = 'page__views__galleries';
    }
}

Then in your theme directory, create the page--views--galleries.tpl.php file. Clear drupal cache and template should work. I used 'galleries' as and example views name.


You can create a template file to theme almost any aspect of views output. In your case you want to create a custom template for your page display.

On the view designer, click the Theme link in Basic Settings. You'll see some template file naming options depending on if you want to theme the whole view (e.g., views-view--example--page.tpl.php), each row (e.g., views-view-fields--example--page.tpl.php) and so on.

Copy the appropriate template you want to customize from /sites/all/modules/views/theme to your theme and customize as you wish.

Once you create your custom template file, you can go back to the view designer Theme link to make sure it is being used. Your template should be bolded.

Hope this helps.


There's some documentation on template suggestions, and you might be interested in page.tpl.php suggestions. If it's a page view, you could use a path suggestion. So if your view is at http://www.example.com/photos, the page.tpl.php file would be named page-photos.tpl.php.


Had a similar problem, which was remedied by putting a .tpl.php in my theme's template folder. The naming convention for a page display of views in drupal 7 is page--path-to-view.tpl.php


In a drupal 7 theme I added a template suggestion in template.php:

function sovon_preprocess_page(&$variables) {   
if(views_get_page_view())   {
    $variables['theme_hook_suggestions'][] = 'page__view';      
}
}

See http://api.drupalize.me/api/drupal/function/views_get_page_view/7 and http://drupal.org/node/223440#custom-suggestions for more information.


Assuming a view as a page, you can use the results of page_manager_get_current_page() in your preprocess to determine if your view is active, then take the appropriate steps (tack on body classes, add a template suggestion, etc).


using sillygwailo's suggestion I got this to work for me very nicely:

function YOUR-THEME-NAME_preprocess_page(&$vars) {

 //allow template suggestions based on url paths.  
 $alias = drupal_get_path_alias(str_replace('/edit','',$_GET['q'])); 
 if ($alias != $_GET['q']) { $suggestions = array();
 $template_filename = 'page'; 
 foreach (explode('/', $alias) as $path_part) {
 $template_filename = $template_filename . '-' . $path_part; 
 $suggestions[] = $template_filename;
 } 
 $alias_array = explode('/', $alias);
 $variables['template_files'] = $suggestions; 

Then if your view is at www.example.com/photos , create a page-photos.tpl.php in your theme directory and drupal will use that as your template.


If that checked answer doesnt work(because it didnt for me) you can create a page--(urlname).tpl.php and then style your theme however you want it. Then you can import your view in one of several ways ...

       print render($page['content']);  

or

$view = views_get_view('viewname'); print $view->execute_display('default', $args);

$args is just an array and can be blank..you can just leave it off entirely

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜