0

I am trying to create a custom view page template which will be made of custom fields, which i am trying to print. I created a view page and added all the needed fields to it.

Next, I created a views-view--my-page-name.tpl.php template, which get's recognised, however when i try to print the fields, i get the errors below:

Notice: Undefined variable: fields in include...

Notice: Trying to get property of non-object in include....

Below is the full code of my custom view page template which i copied from the default view page template and modified it. Im trying to print the field with this code:

<?php print $fields['views_field_field_name']->content; ?>

I tried all variables of field name as: field_name, field_field_name, views_field_field_name, but none of them work, as they all give same error.

<div class="<?php print $classes; ?>">
  <?php print render($title_prefix); ?>
  <?php if ($title): ?>
    <?php print $title; ?>
  <?php endif; ?>
  <?php print render($title_suffix); ?>


  <?php if ($exposed): ?>
    <div class="view-filters">
      <?php print $exposed; ?>
    </div>
  <?php endif; ?>

  <?php print $pager; ?>

  <?php if ($header): ?>
    <div class="view-header">
      <?php print $header; ?>
    </div>
  <?php endif; ?>

  <?php if ($attachment_before): ?>
    <div class="attachment attachment-before">
      <?php print $attachment_before; ?>
    </div>
  <?php endif; ?>


<?php print $fields['views_field_field_name']->content; ?>
<?php print $fields['views_field_field_age']->content; ?>
<?php print $fields['views_field_field_phone']->content; ?>


  <?php print $pager; ?>


  <?php if ($footer): ?>
    <div class="view-footer">
      <?php print $footer; ?>
    </div>
  <?php endif; ?>

  <?php if ($attachment_after): ?>
    <div class="attachment attachment-after">
      <?php print $attachment_after; ?>
    </div>
  <?php endif; ?>

  <?php if ($more): ?>
    <?php print $more; ?>
  <?php endif; ?>  

  <?php if ($feed_icon): ?>
    <div class="feed-icon">
      <?php print $feed_icon; ?>
    </div>
  <?php endif; ?>

</div><?php /* class view */ ?>
1

You are using template for display, here you will do theming for overall display, the $fields variable is not available here.

If you want to change how fields are printed relative to each other, row style, then you should use views-view-fields.tpl.php, in your case the name will be like views-view-fields--yourpagename.tpl.php.

but if you want to theme individual fields then you have to use views-view-field--yourfieldname.tpl.php.

  • Im using the display template beacuse i only want to put a wrapper around the printed fields. I don't want to make any changes to the fields except to put each of them in a wrapper with a different class each, separately, so i can theme them. How could i just print the Label + Content of the fields on the display template? – risha riss Jun 29 '14 at 8:29
  • Create a template file like views-view-fields--yourpagename.tpl.php and copy the contents from views-view-fields.tpl.php and add your wrapper. – wolverine Jun 29 '14 at 8:36
  • But wouldn't that add the wrapper with same class to all fields? I want to add wrapper with different class around each field. – risha riss Jun 29 '14 at 8:37
  • You will be having field name & label based on that you can create your wrapper. – wolverine Jun 29 '14 at 8:54
  • I did as you suggested. Below is the code i have in my view-fields--mypagename.tpl.php <div class="col-sm-12"> <?php foreach ($fields as $id => $field): ?> <?php if (!empty($field->separator)): ?> <?php print $field->separator; ?> <?php endif; ?> <div class="col-sm-3"> <?php print $field->wrapper_prefix; ?> <?php print $field->label_html; ?> <?php print $field->content; ?> <?php print $field->wrapper_suffix; ?> </div> <?php endforeach; ?> </div> But this put class="col-sm-3" wrapp around each field. How could i use different class on each field wrapp. – risha riss Jun 29 '14 at 8:59

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.