I'm desperate to style $content in my own way without erroneous DIV tags. It seems that it is delivered as is - no chance to select which fields, what order with/without headers.

It seems a fairly simple requirement but I can't see it (after 4 weeks of building a site with Drupal).

I could do it as another view - but the info is there waiting for me already, just bundled up into $content.

Anybody solved this?

Ian

Comments

nevets’s picture

Seriously $content can be one of many things so depending what you are looking at the answer may/will be different. A common example is the output from a node which is themed by node.tpl.php which you can read more about here: http://drupal.org/node/11816

cinquetooty’s picture

All I've ever found in a node.tpl.php is the ability to plonk $content in a different location - not the ability to alter the content of $content!

I'll look at the link later - for now I've done it with a view, which I'm sure has an overhead.

thanks again

Ian

seanxian’s picture

I have recently discovered the joy and power of the node.tpl.php file. I highly recommend investigating further. Basically, this file controls the output of the $content variable. So you have your theme template (in which you place your $content tag, nested inside of a div or table or whatever). Then, using the node.tpl.php, you can further overide the exact output of all of the node component's - if you want to put the $title in a div, an h2, h3, h4 etc. Markup the entire layout however you would like, omit thing if you wish (for example, submitted by info, labels, etc) and fully realize the power of Drupal's modular framework, especially if you are using the phptemplate engine (the default for Blue Marine theme).

Also, say you want to create a custom node (using CCK), and template that differently than other node/content of the site. For example, a press release section. If the name of the custom content is Press Release, the database name , because it's a CCK custom type, would be node-content_press_release.tpl.php. Put that in your themes folder and theme away to your heart's desire - Drupal and phptemplate will automatically detect that node and apply your custom node template. (You can also do the same thing with the contemplate module, but I like working with the files in the theme folder because it gives me a better grasp on what is happening behind the scenes.)

These methods are great if you are setting up a site for a client and want to control the output of the content that they will be creating, and not have to worry about them messing around with wysiswyg editors, etc.

I have worked with Joomla for almost a year and dabbled with Drupal for many months and am only just beginning to get a grasp on the power available in the Drupal system. Along with an outstanding new 5.x beta that just completely kicks butt, I would strongly encourage the developers and community to raise the ante by improving the documentation. True, the learning curve here is steep, but that could be greatly reduced if we can organize and present the proper information in a more efficient, user-friendly manner.

Hope this helps, although it sounds like you are getting along with the views module.
Hoorah for Drupal!

cinquetooty’s picture

When I look in node.tpl.php all I can do is move $content and add other features - not control how the values are put together that make up $content - which in the particular case are from a CCK content type.

I did try putting a custom node type - but it PHPtemplate wouldn't notice it.

I've only just moved to Drupal having always lashed together my own CMSs figuring that this would ease my workload. Possibly I've penalised myself by not 'going with the flow' and building it all with modules with an 'out of the box' interface - I was one of those kids that always took things apart to see how they work. It's just frustrating looking for two hours to find a way to reorder two text fields and an image!

Thanks for your input

Ian

seanxian’s picture

I definetly know that the node.tpl.php file controls all of the parts of the node, whereas the page.tpl.php is the file that consists of the entire page template, including the $content tag. For example, here is a snippet of code that I placed in a custom node.tpl.php file - this was for a CCK type named press release, so the name of the file is node-content_press_release.tpl.php.

<div class="field field-type-text field-field-month">

  <h3 class="field-label" style="display:none;">Month</h3>
  
<h4>posted by <?php print $name ?> </h4> <div class="field-items">
    <?php foreach ((array)$field_month as $item) { ?>
      <div class="field-item"><?php print $item['view'] ?></div>
    <?php } ?>
  </div>
</div>

<div class="field field-type-image field-field-image" style="float:left;">
  <h3 class="field-label">Image</h3>
  <div class="field-items">
    <?php foreach ((array)$field_image as $item) { ?>
      <div class="field-item"><?php print $item['view'] ?></div>
    <?php } ?>
  </div>
</div>

<div class="field field-type-text field-field-body">
  <h3 class="field-label">Body</h3>
  <div class="field-items">
    <?php foreach ((array)$field_body as $item) { ?>
      <div class="field-item"><?php print $item['view'] ?></div>
    <?php } ?>
  </div>
</div>

You will notice a couple of quick inline styless I placed, a display:none; for the h3 tag for the month, and float:left; that I placed on the div containing the image tag. This was just to get an idea of how to manipulate the various elements' layout. I found that using this method I have great flexibility in styling and laying out all of these seperate components of the individual node. This of course will all be output in my page template, the page.tpl.php file, wherever I choose to put the $content variable hook.

I hope this helps, also reccomend that contemplate module, which allows you to do the same thing from within the admin settings - and it automatically generates a list of the variables available to you that make up the actual node content.

Good luck!

cinquetooty’s picture

There's a variable scope issue here!

BOTH node and page templates contain $content - but they 'aint the same $content!

I very nearly got here - but I didn't know how to break $node apart to get the array-within-array values of the actual components I wanted to style (which are combined automatically in $content).

I see now that:

foreach ((array)$field_body as $item)

cuts straight to the chase!

Surely though, there will only be ONE occurance of $field-body within any given $node? Or is this the only way to find it?

You've been a great (and patient) mentor!

Ian

nevets’s picture

Regarding "Surely though, there will only be ONE occurance of $field-body within any given $node? Or is this the only way to find it?", CCK also you to have more than one of a particular field so it depends on how the CCK type was defined.

cinquetooty’s picture

...isn't there a way to get the view value out of the $node object in one hit?

This works....

$field_job_title[0]['view']

Jeff Burnz’s picture

Theres a whole entire section in the handbooks about building templates for CCK, and this paticular section answers your questions precisely, and more - http://drupal.org/node/62466