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.
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.
}
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!
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; ?>
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>
Try replacing the collection('Name');
part with this:
collection('Name', get_collection_for_item());
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?
I wasn't quite specific enough I guess. That first line needs to be:
$collectionName = collection('Name', get_collection_for_item());
Okay, no error. But not picking up the alternate layout either.
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.
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
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.
At long last, I've returned to this task and it works! Thanks a million.
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.
Even when adding the else:
, if statements like this should always have an endif;
at the end.
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; ?>
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.
I am looking to do this with the latest release Omeka 2.3. I haven't been able to get the codes suggested here to work. Have there been syntax changes that someone could point me towards that might affect this?
Thanks.
Here's a basic code I am trying that is not responding:
<?php if (item_belongs_to_collection('Metropolitan Design Center'): ?>
<?php echo link_to_item(item_image('thumbnail', array('alt' => $itemTitle))); ?>
<?php else: ?>
<?php echo link_to_item(item_image('fullsize', array('alt' => $itemTitle))); ?>
<?php endif; ?>
Maybe I need to "get" the collection name first?
It is a little different in Omeka 2.3, but your instinct to get the collection name is right.
I haven't tested this directly, but I think it'd be something like:
<?php
$currentItem = get_current_record('items');
$collectionName = metadata($currentItem, 'collection_name');
// do your check against the collection name and write the link. I _think_ that will work as you have it.
Thanks patrickmj! I am still tweaking my chosen display settings, but the code itself is now working:
<?php
$currentItem = get_current_record('items');
$collectionName = metadata($currentItem, 'collection_name');
if ($collectionName == 'Metropolitan Design Center'): ?>
<div id="itemfiles" class="element">
<h3><?php echo __('Images'); ?></h3>
<?php echo item_image_gallery(
array(
'link' => array('class' => 'link'),
'image' => array('class' => 'image')),
'fullsize');
?>
</div>
<?php else: ?>
<?php echo link_to_item(item_image('fullsize', array('alt' => $itemTitle))); ?>
<?php endif; ?>
<?php echo all_element_texts('item'); ?>