Hello, I have to make a bunch of different feeds in order to get real estate listings to other sites such as zillow and trulia. Each of these sites has their own specifications as to how the data should be presented. For example on page 13 (http://www.zillow.com/static/pdf/feeds/ZillowBrokerFeedsTechnicalSpecV1....) is how zillow wants the feed sent to them. I am comfortable with tpl.php and have views installed but I am not sure how to go about it, do I use Views Node Feed or Views RSS modules and what do I name my template file. Any push in the right direction would be appreciated. Thank you.

Comments

thollback’s picture

Check out the Contemplate module: http://drupal.org/project/contemplate

hampshire’s picture

I create my templates in notepad and upload them as .tpl.php files but I have had contemplate installed before and if I remember correctly it only lets me customize based on content type so I could only have one template for properties. I need one template for for each view. So if I have a view called feed_zillow and a view called feed_trulia I would need a completely different feed template for each. Can this be done using contemplate? I believe I know how to code the template file, just not how to name it and perhaps not what modules I need enabled.

Thank you.

hampshire’s picture

Does anyone have any other suggestions, can a feed even be customized using a tpl.php file? If so is there any documentation about how to name the file when it is a view?

Thanks.

WorldFallz’s picture

Views, regardless of whether or not they use the rss display, are themed the same way. Checkout views advanced help and Views 2 theming. You should be able to get the output for your views just about any way you want with theming.

hampshire’s picture

Thank you for the link, I am using drupal 5 so I have Views 1.6 installed but have the theming guide to that as well. I have already coded the tpl.php file I just do not know what to name it and can not seem to find that info anywhere.

WorldFallz’s picture

Sorry about that-- i didnt catch the d5 tag on your op. It's been a while since I've themed views1 -- doesn't the theming wizard provide you the list of file names?

hampshire’s picture

The wizard provides me with views-list-view_name.tpl.php. I put my code in that file and nothing changes, my feed still looks the exact same as it did before I created a template.

hampshire’s picture

Am I going about this wrong? Is there more than just writing the code and making the template file? If so could someone fill me in on what those things are, if not can someone help me figure out home to name my tpl.php file? I need about 15 custom feeds for one content type and have the code written to provide these feeds, I just can not figure out how to get that code to override what views outputs automatically. Thank you.

WorldFallz’s picture

Sorry-- I don't even have a d5 site handy to play around with this. iirc, the theming wizard provides 3 things: code for the template.php file, code for the tpl.php along with the suggested filename, and some css. There's more info at Views 1 theming.

thollback’s picture

...since I used the Views Theme Wizard, but in looking at my code I see that I did have to insert a function in the template.php file called phptemplate_views_view_list_viewname, and there is a line in that function that does a phptemplate_callback to the views-list-viewname tpl file. This has to be the mechanism that is used to override the automatic views output by pointing to your themed tpl file.

My memory is vague, but hope this helps.

hampshire’s picture

Thank you, yeah the views-list-viewname.tpl.php file and code from the theme wizard works but then I have to select list in views. If I select Views RSS: RSS Feed then the template file is nolonger used and the theme wizard only gives me the list option no rss option. Does anyone know what the template naming structure is for a feed? Is it RSS, Feed, syndication, etc, I can find nothing that says what its equivalent is to say block or list, etc.

Thank again for the reply.

modul’s picture

subscribe

WorldFallz’s picture

I'm not sure you need the type-- based on the views theming handbook page try the following function in the tempate.php:

function phptemplate_views_view_VIEWNAME($view, $type, $nodes, $level = NULL, $args = NULL) {
  //them your view here

}
hampshire’s picture

Perhaps I am using your code wrong but entering the code you gave, changing VIEWNAME to feed_trulia and creating a file called views-view-feed_trulia.tpl.php returned header and footer and all that but nothing from the views-view-feed_trulia.tpl.php file.

On a side note is there any way to keep drupal from printing header, footer, etc. That way I could just theme a list view which already works into a xml file.

Thanks.

WorldFallz’s picture

Not sure what else to try-- I setup a temp d5 site and verified the above works (i simply put print "This is the views_view_testfeed.tpl.php file and it displayed).

As for header, footer, etc-- if you mean the site header & footer (as opposed to those of the view) it should happen automatically if the code in the views_view_VIEWNAME.tpl.php file is specified correctly.

Specifying the correct filename just makes sure your tpl.php file gets picked up as the template file to use to display the view-- it doesn't provide any default data or formatting. Once you override the default, you are responsible for providing the complete and correct code to specify the view. All of it-- including the xml definition and all the code required to display the view.

hampshire’s picture

Thank you, I really appreciate all your help and will try this again today and then update everyone.

Also where did you put print "This is the views_view_testfeed.tpl.php?

Thanks

WorldFallz’s picture

Right in the views-view-feed_trulia.tpl.php file-- just to make sure the file was being correctly picked up.

hampshire’s picture

Oh, I see what you were doing, no it doesn't print unless I change

function phptemplate_views_view_feed_trulia($view, $type, $nodes, $level = NULL, $args = NULL) {
  //them your view here

}

to

function phptemplate_views_view_feed_trulia($view, $type, $nodes, $level = NULL, $args = NULL) {
  require 'views-view-feed_trulia.tpl.php';

}

Also was this code above going into template.php in addition to the code the theme wizard generated or instead of the code the theme wizard generated?

Anyway once I add the require 'views-view-feed_trulia.tpl.php'; it will print "Hello World" but <?php print $node->field_county[0]['view'] ?> (that should list the county) fails to print. So assuming I am supposed to use require 'views-view-feed_trulia.tpl.php'; there must still be something wrong with my tpl.php file. I also think there is still something wrong with my tpl.php file because after it prints "Hello World" the rest of the page is then displayed, by rest of page I mean the sites header, footer, navigation, etc.

WorldFallz’s picture

I think I'm totally mangling my explanation, lets start over with a simpler method:

  1. open the views_rss.module in the /views/modules directory
  2. find the theme_views_rss_feed function
  3. copy the ENTIRE function to your template.php file
  4. rename the function to phptemplate_views_rss_feed_trulia
  5. ensure this is the ONLY function in the template.php file with that name or anything close to it (in other words, get rid of anything else you've created while trying to theme this view).
  6. forget about creating a tpl.php file for this view, it's not necessary (and delete any you may have already created).
  7. change the first occurrence (around line 10 of the function) of 'title' => views_get_title($view, 'page'), in the function to 'title' => "HELLO WORLD",
  8. navigate to the URL of your view and verify a) that it's a feed and b) that HELLO WORLD appears as the title.
  9. NOW, you can edit that function to theme the view any way you wish.

Let me know how it goes!

hampshire’s picture

I do not get hello world as the title. This is what I have (I changed the name of the view from feed_trulia to feedtrulia to rule out that causing a problem so that is not a typo).

/**
 * plugin that actually displays an RSS feed
 */
function phptemplate_views_rss_feedtrulia($view, $nodes, $type) {
  if ($type == 'block') {
    return;
  }
  global $base_url;

  $channel = array(
    // a check_plain isn't required on these because format_rss_channel
    // already does this.
    'title' => "HELLO WORLD",
    'link'        => url($view->feed_url ? $view->feed_url : $view->real_url, NULL, NULL, true),
    'description' => $view->description,
  );

  $item_length = variable_get('feed_item_length', 'teaser');
  $namespaces = array('xmlns:dc="http://purl.org/dc/elements/1.1/"');

  // Except for the original being a while and this being a foreach, this is
  // completely cut & pasted from node.module.
  foreach ($nodes as $node) {
    // Load the specified node:
    $item = node_load($node->nid);
    $link = url("node/$node->nid", NULL, NULL, 1);

    if ($item_length != 'title') {
      $teaser = ($item_length == 'teaser') ? TRUE : FALSE;

      // Filter and prepare node teaser
      if (node_hook($item, 'view')) {
        node_invoke($item, 'view', $teaser, FALSE);
      }
      else {
        $item = node_prepare($item, $teaser);
      }

      // Allow modules to change $node->teaser before viewing.
      node_invoke_nodeapi($item, 'view', $teaser, FALSE);
    }

    // Allow modules to add additional item fields
    $extra = node_invoke_nodeapi($item, 'rss item');
    $extra = array_merge($extra, array(array('key' => 'pubDate', 'value' =>  date('r', $item->created)), array('key' => 'dc:creator', 'value' => $item->name), array('key' => 'guid', 'value' => $item->nid . ' at ' . $base_url, 'attributes' => array('isPermaLink' => 'false'))));
    foreach ($extra as $element) {
      if ($element['namespace']) {
        $namespaces = array_merge($namespaces, $element['namespace']);
      }
    }
    
    // Prepare the item description
    switch ($item_length) {
      case 'fulltext':
        $item_text = $item->body;
        break;
      case 'teaser':
        $item_text = $item->teaser;
        if ($item->readmore) {
          $item_text .= '<p>'. l(t('read more'), 'node/'. $item->nid, NULL, NULL, NULL, TRUE) .'</p>';
        }
        break;
      case 'title':
        $item_text = '';
        break;
    }

    $items .= format_rss_item($item->title, $link, $item_text, $extra);
  }

  $channel_defaults = array(
    'version'     => '2.0',
    'title'       => variable_get('site_name', 'drupal') .' - '. variable_get('site_slogan', ''),
    'link'        => $base_url,
    'description' => variable_get('site_mission', ''),
    'language'    => $GLOBALS['locale'],
  );
  $channel = array_merge($channel_defaults, $channel);

  $output = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
  $output .= "<rss version=\"". $channel["version"] . "\" xml:base=\"". $base_url ."\" ". implode(' ', $namespaces) .">\n";
  $output .= format_rss_channel($channel['title'], $channel['link'], $channel['description'], $items, $channel['language']);
  $output .= "</rss>\n";

  drupal_set_header('Content-Type: text/xml; charset=utf-8');
  print $output;
  module_invoke_all('exit');
  exit; 
}

Hopefully I am doing something obvious wrong. also the file views_rss.module was in modules/views on my site, assuming that was just a typo but thought I would point it out incase it is not. Oh and thanks for the new explination as I was going in a completely different direction.

Thanks again.

WorldFallz’s picture

If the name of the view is 'feedtrulia' then rename the function to phptemplate_views_rss_feed_trulia. The base name to which you add the name of the view is 'phptemplate_views_rss_feed_'.

hampshire’s picture

Okay, so I was naming it wrong the whole time. It does seem to work now and I will soon try to figure out how to customize the output.

Thank you.

WorldFallz’s picture

excellent-- good luck.

modul’s picture

I’m not sure if I understand the problem correctly, but modifying Views based RSS output isn’t too difficult (although it took me a good deal of digging to find out how it’s done :-). All you need to do is write a very short module. What I write below, works for Drupal 5.x . I’m not sure how the module business works in 6.x, but I suppose the ‘philosophy’ is still the same. Let’s call the thing “yourrss”.

The yourrss.info file could look like this:

; $Id$
name = "yourrss"
description = "Modify RSS stuff."

And now the actual module in yourrss.module:

<?php
// $Id: yourrss.module
// Stuff for RSS modification
// 

function yourrss_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL)
{
    switch($op) {
        case 'rss item':

            //$v = var_export($node, true);
            //echo $v;

            $the_argument = arg(2);
            switch($the_argument) {
                      case 100:
                              $node->title = "<strong>" . $node->title . "</strong>";
                              break;
                      case 200:
                              $node->title = "<em>" . $node->title . "</em>";
                              break;
                      default:
                              break;
            }
        break;
    }
}

Install the module in the usual way, and activate it.

Then, depending on the taxonomy term, your feed item’s title will be either bold or italic. Obviously, you could modify everything to your heart’s content, depending on the taxonomy term.

Notice the commented-out var_export lines, which show you all variables of the $node object at hand. Very useful!

I didn’t try this actual code, so be a bit careful. But the idea works on my site. Good luck. And if I didn’t understand your problem in the first place, oh well :-).

Anonymous’s picture

subscribing.