I watched the latest Lullabot video on CCK and Views and I have a theming question as a result. I need to theme a views and print fields out on the views template page I am using. In the CCK Video they showed the format of code for printing out a cck field like such:
?php print check_plain($node-> title) ?> or <?php print $node-> field_job [0] [‘view’] ?>

Is this also how I could print out variables on my views template page?

Also, I was looking at an example of a templated views page recently and the variables looked a lot different. They looked like this:

<?php print $certification; ?> or <?php print $list_price; ?>

Where do you think the programmer generated the code from above?

Which style should I use?

Thanks,
Becky

Comments

justageek’s picture

beckyjohnson’s picture

This was pretty helpful in terms of giving me more knowledge but I tried their technique and it didn't work.

<?php
  //print '<pre>' . htmlentities(print_r($rows, 1)) . '</pre>';
  //If you have devel.module installed, comment the line above and uncomment the line below
  dsm(array_keys($fields)); 
?>

I put this in my template file views-view--technical-documents.tpl.php, the name of my high level view but i just get a blank page.
And yes, I have the devel module installed and enabled.

please help! why would i go white?

Becky

justageek’s picture

Could be php out of memory, that's typically the cause of the white screen.

Try this to rule of devel module:

print '<pre>';
print_r($fields);

// or their suggestion
print '<pre>' . htmlentities(print_r($rows, 1)) . '</pre>';
beckyjohnson’s picture

I checked my php memory and it wasn't giving me an error on the status page.

Used the snippet you have up there and it made my page go blank. It loaded but there was nothing on the page other than the views title.

Becky

justageek’s picture

I can't get dsm() to work.

At the level you are working, you cannot, I do not think, get to individual fields. You need to overide a lower level template like the row style template. What are your Style and Row Style settings?

beckyjohnson’s picture

I have another template for my fields view but the instructions on that article you said, said to use a higher template than a field template. Also, to clarify when I put this on my page i get a white screen:

<?php
  print '<pre>' . htmlentities(print_r($rows, 1)) . '</pre>';
  
?>

as well. But If I put this on my page:

<?php
  print '<pre>' . htmlentities(print_r($rows, 1)) . '</pre>';

I don't get a white screen but I don't get anything helpful. Seems like the closing ?> is doing something.

Anyway, my row settings: Field which title inline. Not sure it that makes a difference.
style settings : unformatted, grouped by taxonomy term.

Becky

justageek’s picture

the overall view template does not appear to provide a $fields variable for me when I choose row style = fields. The $rows variable is there, but it is the complete, formatted output.

I know what that article says, but I don't think it is clear. If you want control of each field, you are going to work inside your fields template, not the overall view template.

beckyjohnson’s picture

Ok I grabbed

<?php
print '<pre>';
print_r(array_keys($fields));
?> 

off your last post there and now it works. It printed out the $field in a plain text way.

justageek’s picture

Here is what I get in my overall view template for defined variables, when I have a row style of fields, I don't see a 'fields' variable at all.

Array
(
    [0] => template_file
    [1] => variables
    [2] => template_files
    [3] => view
    [4] => zebra
    [5] => id
    [6] => directory
    [7] => is_admin
    [8] => is_front
    [9] => logged_in
    [10] => db_is_active
    [11] => user
    [12] => rows
    [13] => css_name
    [14] => name
    [15] => display_id
    [16] => empty
    [17] => exposed
    [18] => header
    [19] => footer
    [20] => more
    [21] => feed_icon
    [22] => attachment_before
    [23] => attachment_after
    [24] => pager
    [25] => admin_links_raw
    [26] => admin_links
    [27] => dom_id
)
     
beckyjohnson’s picture

I think you are right. I added the code you showed me to my fields template and it printed the variables out.

<?php
print '<pre>';
print_r(array_keys($fields));
?>

I got this:

Array
(
    [0] => title
    [1] => body
    [2] => field_stages_value
    [3] => field_dl_number_value
    [4] => field_dl_upload_fid
    [5] => field_dl_filesize_value
    [6] => field_marketing_dl_format_value
)

which are the names of my fields.

Now one more question, how do I put these fields on my actual template. Can you give me an example?

from listening to the CCK video on printing out variables, I thought i would have a number and word or something to print out, like this <?php print $node-> field_job [0] [‘view’] ?> and it made sense at the time becuase they were using the dsm code and looking at it through Krumo but I'm not sure what to do with this particular page......

Thanks for all your help on this, btw. I really appreciate it.
Becky

justageek’s picture

this is from the core fields template in views, in the video was it a node style view?

// $Id: views-view-fields.tpl.php,v 1.6 2008/09/24 22:48:21 merlinofchaos Exp $
/**
 * @file views-view-fields.tpl.php
 * Default simple view template to all the fields as a row.
 *
 * - $view: The view in use.
 * - $fields: an array of $field objects. Each one contains:
 *   - $field->content: The output of the field.
 *   - $field->raw: The raw data for the field, if it exists. This is NOT output safe.
 *   - $field->class: The safe class id to use.
 *   - $field->handler: The Views field handler object controlling this field. Do not use
 *     var_export to dump this object, as it can't handle the recursion.
 *   - $field->inline: Whether or not the field should be inline.
 *   - $field->inline_html: either div or span based on the above flag.
 *   - $field->separator: an optional separator that may appear before a field.
 * - $row: The raw result object from the query, with all data it fetched.
 *
 * @ingroup views_templates
 */
beckyjohnson’s picture

I think it was probably a node style view they were using but in the styling views section, they didn't get very deep; they just talked about how you could generate theme files using views 2 now.

http://beta.wimaxforum.org/technical_release1 this is actually a link to my views if you want to see it so far. I'm trying to mix js and views to get a result like this : http://www.wimaxforum.org/training/courses. Someone else did that page and I'm trying to replicate it.

It's proving rather hard.

Becky

justageek’s picture

Based on your web site sample:

If you set up a view that is row style 'node', then what you get in your view is a complete list of nodes, with all the data for each node. Then, you can create your own node template for your content type. So, lets say you create a content type called 'news'. You can create a template called node-news.tpl.php. This will then handle the output for your 'news' nodes. And, in this template, you can actually have a section of code to handle output for a list of nodes, and a section of code to handle viewing one single node. I would recommend locating your node.tpl.php file for the theme you are using then just making a copy, naming it according to your content type.

Inside this node template, there is a variable called $page. If this value is 0, it means you are NOT viewing a single page for a node, you are on a list type page, so you can do this

if($page == 0){
// your php / html goes here to style the output of each node in your list
} else {

// your php  / html for viewing a single node goes here

}
beckyjohnson’s picture

This is really interesting. If I did it the way you suggest above, could I still use the js effect that is illustrated on the page: http://www.wimaxforum.org/training/courses ? That is proving to be difficult too btw. I was trying to go off of what the other person did in their js but it's not working yet.

For example

<div class="document-controls">
  <a href="javascript:;" class="document-details-control document-details-control-nid-<?php print $nid; ?>">View Details</a>
  <a href="javascript:;" style="display:none;" class="document-details-control course-details-control-nid-<?php print $nid; ?>">Hide Details</a>
   | 
  <?php if ($dl_upload_fid) { ?>
   | 
    <?php print $dl_upload_fid ?>
  <?php } ?>

This part: is supposed to show a download link, so people can download the file, but it's not working

 <?php if ($dl_upload_fid) { ?>
   | 
    <?php print $dl_upload_fid ?>

dl_upload_fid is the variable for my upload widget that i made in CCK but this isn't showing up on the page.

Anyway, I'm going to give it a shot your way. Thanks for the advice!

Becky

justageek’s picture

When their page loads jquery locates all their specific links and applies some custom show / hide functionality. I don't know exactly how they did their pages, probably something with either a custom module or a custom node template. So, the short answer is, yes you can do it, the long answer is, it might not be simple unless you know some javascript / jquery or have a js person handy.

beckyjohnson’s picture

He didn't use a module, it was custom code. For example in the css there is this:

/*training*/
/**********************/
.course-details {
  display:none;
}
/**********************/
div.course-controls{
	font-size: 12px;
}

and in the js there is this:

Drupal.behaviors.crscourses = function() {
  $(".course-details-control").click( function() {
    $(".course-details",this.parentNode.parentNode).toggle("fast");
    $(".course-details-control",this.parentNode).toggle();
  });  
  $(".course-schedule-control").click( function() {
    $(".course-schedule",this.parentNode.parentNode).toggle("fast");
    $(".course-schedule-control",this.parentNode).toggle(); 
  });  
} 

In the view there is this:

<div class="course-controls">
  <a href="javascript:;" class="course-details-control course-details-control-nid-<?php print $nid; ?>">View Details</a>
  <a href="javascript:;" style="display:none;" class="course-details-control course-details-control-nid-<?php print $nid; ?>">Hide Details</a>
   | 
  <a href="javascript:;" class="course-schedule-control course-schedule-control-nid-<?php print $nid; ?>">View Schedule</a>
  <a href="javascript:;" style="display:none;" class="course-schedule-control course-schedule-control-nid-<?php print $nid; ?>">Hide Schedule</a>
  
  <?php if ($download_brochure_link) { ?>
   | 
    <?php print $download_brochure_link; ?>
  <?php } ?>
  <?php 
  global $user;
  if($user->uid) {
    $node->nid = $nid;
    $node->type = 'course';
    if(node_access('edit', $node)) {
      print ' | ' . l('Edit course details','node/'.$nid.'/edit',array('query' => 'destination='.$_GET['q'])); 
    }
  }
  ?>
  
  <?php 
  if(user_access('create course_instance content')) {
    print ' | ' . l('Add an instance of this course','node/add/course-instance',array('query' => 'parent='.$nid.'destination='.$_GET['q'])); 
  }
  ?> 
   
</div>

<div class="course-details course-details-nid-<?php print $nid; ?>">
  <div class="field-item">
    <span class="label">Certification: </span>
    <span class="value"><?php print $certification; ?></span>
  </div>
  
  <div class="field-item">
    <span class="label">Duration: </span>
    <span class="value"><?php print $duration; ?></span>
  </div>
  
  <div class="field-item">
    <span class="label">List Price: </span>
    <span class="value"><?php print $list_price; ?></span>
  </div>
  
  <div class="field-item">
    <span class="label">Course Description: </span>
    <span class="value"><?php print $long_description; ?></span>
  </div>
</div>

Either way, cannibalizing this code is not working.....

Becky

beckyjohnson’s picture

I don't think this is going to work. I am going to try it but, one reason is that I needed everything to group by taxonomy term, under style: unformatted and I don't seem to be able to do this when I'm doing row style as node.... unless you know of an alternative way to achieve this?

Becky

justageek’s picture

You can still get to individual fields by making it Field rather than Node. If you look at the core view for the unformatted row style, you will see that the data is in $fields, and the raw data is in an array called $rows.

Concering the javascript, I see his javascript and CSS, but somewhere there must be a line of code (unless maybe drupal just does this for you) to add his custom "click handler" code to all those link tags.

Anyway, so change to unformatted row style and it will let you choose your grouping field. Then use your original template override for row style to output your individual fields. You can basically use $fields like this:

print $fields['my_field_name']->content;

this is formatted content, meaning if any of your fields contain html, Drupal will have processed it based on your selected input format, made sure it is safe, etc. If you need unformatted data, it is there somewhere, I'd have to hunt to find it.

I'm going to look again, because I think there is a way to do a "group by" with a node view, too.

justageek’s picture

beckyjohnson’s picture

Thanks, I'll try these out today.

karl2011’s picture

??

dalegrebey’s picture

Goal:
Print individual fields in an Views template file (e.g. views-view-fields--today.tpl.php) at the Row Style level to give greater control over theme'ing. The purpose of this is to wrap each individual field in your own custom classes using a single template file for all of your rows.

First... Get your field IDs
This is just a quick and easy way to see what your fields are (they are different between, node, cck, taxonomy, etc. I tend to forget the syntax/which vocabulary ids so I do it this way).

<?php foreach($fields as $id => $field): ?>
  <?php print $id;
<?php endforeach; ?>

In your browser this will generate output similar to this: title field_flyer_fid field_date_value tid_3 tid_4 tid tid_2 tid_1 tid_5 . These are the IDs in your view and what you are going to be comparing against.

Secondly... Write if statements to evaluate which field is being printed.
This is a pretty straight-forward string comparison. It's a bit of extra code, but sense you can't just print $fields['field_name']->content in the row style output template... this is my work-around.

<?php foreach($fields as $id => $field): ?>
  <?php // print $id;

  <?php if ($id == 'field_flyer_fid'): ?>
    <div class="flyer"><?php print $field->content?></div><!-- flyer -->
  <?php endif; ?>
  
  <?php if ($id == 'title'): ?>
    <h2><?php print $field->content; ?></h2>
  <?php endif; ?>

  <?php if ($id == 'field_date_value'): ?>
    <div class="date">Date: <?php print $field->content; ?></div><!-- date -->
  <?php endif; ?>

  <?php // So on and so forth for all of your IDs ?>

<?php endforeach; ?>

To take it one step further
The only reasons I've even done this in the first place (instead of just styling the views classes as is) are: One, I want all my classes to be consistent across the website (views and non-views). More importantly, I wanted to wrap fields in groups to make left and right columns. Something to this effect... .views-left-column: field 1, field 2, field 3 ... .views-right-column: field 4, field 5, field 6.

This involved a bit of creative thinking... Here is an example of how this works:

<?php foreach($fields as $id => $field): ?>
  <?php // print $id;

  <?php if ($id == 'field_name'): ?>  
    <div class="views-left-column"><?php // open left column ------------------------------------ ?>
    <div class="category field_name"><?php print $field->content?></div><!-- flyer -->
  <?php endif; ?>

  <?php if ($id == 'field_name'): ?>
    <div class="category field_name"><?php print $field->content?></div><!-- flyer -->
  <?php endif; ?>

  <?php if ($id == 'field_name'): ?>
    <div class="category field_name"><?php print $field->content?></div><!-- flyer -->

    <div class="clear"></div><!-- clear -->
    </div>!-- views-left-column --><?php // close left column ------------------------------------ ?>
  <?php endif; ?>

  <?php if ($id == 'field_name'): ?>
    <div class="views-right-column"><?php // open right column ----------------------------------- ?>
    <div class="category field_name"><?php print $field->content?></div><!-- flyer -->
  <?php endif; ?>

  <?php if ($id == 'field_name'): ?>
    <div class="category field_name"><?php print $field->content?></div><!-- flyer -->
  <?php endif; ?>

  <?php if ($id == 'field_name'): ?>
    <div class="category field_name"><?php print $field->content?></div><!-- flyer -->

    <div class="clear"></div><!-- clear -->
    </div>!-- views-right-column --><?php // close right column ----------------------------------- ?>
  <?php endif; ?> 

  <?php // So on and so forth for all of your IDs ?>

<?php endforeach; ?>

What I've done is open the column when it detects the correct field name and than close the column when it detects the correct field name; telling it to open and close at the appropriate fields. I than clear to make sure the columns are equal heights (CSS, not Drupal).

Finally...
Note that you'll automatically get the zebra odd/even effect from the Views display output template file. The above code will be wrapped .views-rows-odd, than .views-row-even...

The only last important thing to note about using this method is that... It doesn't matter in what order you put your fields in the template! Huh? Remember how we printed the IDs? title field_flyer_fid field_date_value tid_3 tid_4 tid tid_2 tid_1 tid_5. This is the order that your fields are going to be printed in, no matter what order you put your if statements... All we are doing is seeing what field and printing it. To adjust the order, do this directly in your Views UI.

beckyjohnson’s picture

Thank you for the write up. This was very interesting and helpful!
Becky

Lyndzay_M’s picture

Thanks for that great write up.(not that I completely understand it) it helped be solve my views theming problem.

beckyjohnson’s picture

If you template out cck fields, can you do if statements then, too?