Forums » Different item show pages for different collections?

RSS feed for this topic

Info

  1. Is it possible to have different item layouts based on the item's Collection? I imagine you can just create a specialized item show page. Can you write a if-then type statement to determine which show page displays? If so, where does it go?

    I have collections with very different types of material: images, video, and links to newspaper databases. I'm using a modified Rhythm theme.

    Thanks.

  2. You can do the kind of switching you describe, if you want to. A simple example (for the show page):

    $collectionName = collection('Name');
    if($collectionName == 'Things') {
        // Things layout (or include a whole script) here
    } else if ($collectionName == 'Stuff') {
        // Stuff layout/include here
    } else {
        // Layout for items from other or no collection.
    }
  3. Thanks. Can you walk me through a bit more. I inserted the code at the top of the page like so:

    <?php head(array('title' => item('Dublin Core', 'Title'),'bodyid'=>'items','bodyclass' => 'show')); ?>

    $collectionName = collection('Name');
    if($collectionName == 'Media Reports and Commentary') {

    <div id="primary">

    <h5><?php echo item('Dublin Core', 'Title'); ?></h5> [with 3 layouts specified]

    But I just get all three layouts regardless of the collection. I assume I'm making a simple mistake in coding, but I'm a novice.

    Thanks!

  4. You need to enclose PHP code like what I posted in <?php and ?> tags. The confusion is partly my fault, the example I posted isn't quite like what you'd normally see in an Omeka view script. This should be a little better:

    <?php $collectionName = collection('Name');
    if ($collectionName == 'Things'): ?>
        // Things layout (or include a whole script) here
    <?php elseif ($collectionName == 'Stuff'): ?>
        // Stuff layout/include here
    <?php else: ?>
        // Layout for items from other or no collection.
    <?php endif; ?>
  5. Okay, now I just get one layout without errors, but I'm not getting the collection specific layouts. Where does the code you gave me go?

    Here's how I'm starting the page:

    <?php head(array('title' => item('Dublin Core', 'Title'),'bodyid'=>'items','bodyclass' => 'show')); ?>

    <?php $collectionName = collection('Name');
    if ($collectionName == 'Media Reports and Commentary'): ?>

    <div id="primary">

    <h5><?php echo item('Dublin Core', 'Title'); ?></h5>

  6. Try replacing the collection('Name'); part with this:

    collection('Name', get_collection_for_item());
  7. This is my code:

    <?php $collection('Name', get_collection_for_item());
    if ($collectionName == 'Media Reports and Commentary'): ?>

    Result: Fatal error. Maybe this can't work?

  8. I wasn't quite specific enough I guess. That first line needs to be:

    $collectionName = collection('Name', get_collection_for_item());
  9. Okay, no error. But not picking up the alternate layout either.

  10. higbie, I think it would help if you could paste the text of your entire show.php file. Github gists is a great way to share chunks of code. You could also use Pastebin.

  11. Jeremy and John:

    Here's a link to the code:
    https://gist.github.com/1019672
    I can embed it too if you like.

    Thanks for your help. I'm probably making a simple error.

    Toby Higbie

  12. For some reason, the code John suggested isn't working. Instead of doing this:

    <?php $collectionName = collection('Name', get_collection_for_item());
    if ($collectionName == 'Media Reports and Commentary'): ?>

    Use the item_belongs_to_collection function in your if statements, like this:

    <?php if (item_belongs_to_collection('Media Reports and Commentary')): ?>
    
    Your template stuff here.
    
    <?php elseif (item_belongs_to_collection('Oral Histories')): ?>
    
    Your template stuff here.
    
    <?php endif; ?>

    You don't need to define $collectionName; Just use the name of the collection as the first argument of the item_belongs_to_collection function. More information on that function is in the codex.

  13. At long last, I've returned to this task and it works! Thanks a million.

  14. One last follow up. If you have more than two templates, your code doesn't quite work. I got it to work with this:

    <?php if (item_belongs_to_collection('Media Reports and Commentary')): ?>
    
    Your template stuff here.
    
    <?php elseif (item_belongs_to_collection('Oral Histories')): ?>
    
    Your template stuff here.
    
    <?php else: ?>
    
    default (or last) template stuff here

    There is no "endif" statement.

    I don't know if that's "right," but it works for now.

  15. Even when adding the else:, if statements like this should always have an endif; at the end.

  16. Is there an easier way to include sub-templates or snippets instead of writing out the whole template in one file?

    So could you do something like..

    <?php if (item_belongs_to_collection('Media Reports and Commentary')): ?>

    <?php SOME CODE TO LOAD AN EXTERNAL Media Reports SUBTEMPLATE ?>

    <?php elseif (item_belongs_to_collection('Oral Histories')): ?>

    <?php SOME CODE TO LOAD AN EXTERNAL Oral Historie SUBTEMPLATE ?>

    <?php else: ?>

    <?php SOME CODE TO LOAD AN EXTERNAL default SUBTEMPLATE ?>

    <?php endif; ?>

  17. Sure. Omeka has a function called common() that loads another PHP file from within your theme, or you could use PHP's built-in include directive.

Reply

You must log in to post.