3

I'm doing some PHP code examples for a class in a fancy Drupal site.

I have a "Code" content type. I have a "field_php" that gets processed on the actual node.
I have a view that uses "node/%/code" to display the code formatted via Geshi:

  • View PHP Body: Link back to node/1
  • View PHP footer: PHP code to pull geshi formatted "field_php"

    <?php 
      include_once('sites/all/libraries/geshi/geshi.php');
    
      $page = $view->args[0];
      $node = menu_get_item("node/$page");
    
      $source = $node['page_arguments'][0]->field_php['und'][0]['value'];
      $language = 'php';
      $geshi = new GeSHi($source, $language);
    
      echo $geshi->parse_code();
    ?>
    

LASTLY I have a node--code.tpl.php edited to include a "View Code" link at the top of each "code" page. The include_once/echo at the top is the following:

<div class="<?php print $classes; ?> clearfix"<?php print $attributes; ?>>
  <?php 
    include_once('sites/wernercd.php/public/~functions.php');
    echo "<a href=\"" . curPageURL() . "/code\">Code For You!</a>";
  ?>
  <?php if (!$label_hidden) : ?>
    <div class="field-label"<?php print $title_attributes; ?>><?php print $label ?>:&nbsp;
    </div>
  <?php endif; ?>
  <div class="field-items"<?php print $content_attributes; ?>>
    <div class="field-item even"<?php print $item_attributes[0]; ?>><?php print render($items[0]); ?></div>
  </div>
</div>

http://mysite.com/node/1
Node

http://mysite.com/node/1/code
Code

I don't know why, but I feel like I'm doing it wrong. It works like I want it, but I just feel like its the "wrong" way to go about doing it.

What would the "recommended" way of getting this desired output be? How could I change this to catch issues unforseen by my naivete?

3

I would set up an input format for code and set the field_php to use that. The code input format could use a filter like this http://drupal.org/project/codefilter

1

From reading what I understand is that you want to post code on your site. And when you are trying to post code on your site, instead of showing it as it is, Drupal is parsing it. If this is the problem you are facing, then I think code highlighting or code formatting scripts are what you need.

For example, if you want to place some PHP code in your code but don't want drupal to parse it. Then you can use syntax highlight library which will work great for you. Check this out: http://www.amitavroy.com/justread/content/articles/present-your-code-better-code-syntax-highlighter

0

I'm not really answering your question - more discussing some approaches that might interest you.

Keeping the code in the field seems reasonable to me.

Instead of using a view and templates, I would use hook_nodeapi (or the Drupal 7 equivalent) to add the link to the content object. The link url would be in the node/[nid]/code format.

I'd then create a theme template using hook_theme (something like theme_nodecode)

I would then use hook_menu_alter to add a node/%node/code menu item, with a callback of mymodule_node_display_code or something like that. That callback would do any preflight (like checking for geshi module for example), then call the theme function to render the code.

There's lots of approaches to what you're doing, but hopefully this gives you some ideas.

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.