i have created a views filtring contents. i want to use custom tpl and get values of $row variable. How can i extract these valuse?

for example:

<?php foreach ($rows as $id => $row): ?>
   <div class="<?php print $classes_array[$id]; ?>"><?php print $row; ?></div>
<?php endforeach; ?>

i have tried

<?php print_r($row); ?>

but no solution.

  • 1
    Use $view->style_plugin->rendered_fields to get the rendered fields in views TPL – Sibiraj PR Mar 1 '13 at 12:13
  • 1
    you can use the function get_defined_vars() in a .tpl file to see what variables are available. Install the devel module and put dpm(get_defined_vars()); in your .tpl file – 2pha Mar 1 '13 at 16:33
  • One work-araound might be to use Views PHP. You can then set each of your fields as excluded from display, except the PHP field, and code your row formatting in that field. – Triskelion Mar 1 '13 at 17:43
  • What tpl file are you trying to override? Also, is your tpl file in use by Views? (on the View edit screen, click Advanced → Other → Theme → Information… the tpl files currently in use are shown in bold. If your file isn't selected, try clicking the "Rescan template files" button just below the list) – thirdender Mar 5 '13 at 19:24
  • i am using views unformatted tpl. result are showing correctly, i just want to get array from $row var – roma sabina Mar 6 '13 at 5:34

I am facing same problem and get solution as below. Create a tpl as give in Style output under theme information.

$arr_rows = $view->style_plugin->rendered_fields;
<?php foreach($arr_rows as $key=>$arr_row) : ?>
<?php print $arr_row['field1']; ?>
<?php print $arr_row['field2']; ?>
<?php print $arr_row['field3']; ?>
<?php endforeach; ?>

Why don't you use views_get_view_result() ?

https://api.drupal.org/api/views/views.module/function/views_get_view_result/7

Just get results and go trough array - it doesn't matter are you using template or not.

  • This returns the results of the first page if using a pager. Is there a way to get all the results (without any pagination limits)? – W.M. Feb 19 '17 at 19:40
  • Hmm...can you just clone the display and remove pagination on that clone? – MilanG Feb 21 '17 at 7:13

It sounds like perhaps you need to use a different tpl.php.

If you're using a variant of unformatted.tpl.php, try using fields.tpl.php which will give you access to each field output by views.

Ie views-view-fields--view-name.tpl.php

Or I suppose, at a push, you could try work with $view->result[$id]

You should checkout this answer for the question Create template file for views block which steps through how to identify which templates are "suggested" (or available to be overridden) as well as the common mistake of not re-scanning view template files (which is required when views tpl files are added/deleted)

The process for creating templates for views is the same regardless of your view display (page, node, attachment etc)

Following the linked answer's process also provides you with the "default" tpl files which includes a large block of comments outlining available fields in template and how to use them.

If you just want the data of your view (vs. rendering the data as a template) you should revise your question and provide more details (and I'll do the same)

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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