Header Meta Title If else elseif statements

Hi there, I have question about conditional php if else statement. Lately I try with this code for meta title, but on third condition, it return blank. I’m no good with php.

Where did I do wrong? is there better solution for this?
Thank You for any help.

  <?php if($page->isHomepage()): ?>
<title><?php echo $site->title()?></title>

<?php elseif($page->template('page')): ?>
    <title><?php echo $page->metatitle()->html() ?></title>  
<?php else: ?>
<title><?php echo $page->title()->html() ?></title>
<?php endif ?>

Sorry I’m solve it with this:

  <?php if($page->isHomepage()): ?>
<title><?php echo $site->title()?></title>
<?php elseif( $page->metatitle()->isEmpty() ) : ?>
<title><?php echo $page->title()->html() ?></title>
<?php else: ?>
<title><?php echo $page->metatitle()->html() ?></title>   
<?php endif ?>