Another option would be to hook wp and check for a 404 that contains your dynamic pagename:
function wpse27761_dynamic_page() {
global $wp, $wp_query;
if($wp_query->is_404 ):
if( strpos($wp->request, 'mypagename') !== false ):
$wp_query->is_404 = false;
//create a post object with your desired output
endif;
endif;
}
add_action('wp', 'wpse27761_dynamic_page');
but it’s arguably more complex, I would personally just do it the way you’re doing it now, though I usually just create a page-details.php template so you don’t have to explicitly set a template for the page.
as for the rewrite, with your current scheme it’s pretty simple:
function wpse27657_rewrite(){
add_rewrite_rule('details/([^/]*)/?', 'index.php?pagename=details&pid=$matches[1]', 'top');
}
add_action( 'init', 'wpse27657_rewrite' );
function wpse27657_query_var( $query_vars ){
$query_vars[] = 'pid';
return $query_vars;
}
add_filter( 'query_vars', 'wpse27657_query_var' );
this will route /details/whatever/
to your pagename details with the pid query var set. just be sure to visit your permalink settings page and save so these new rules are added. get_query_var('pid')
will return your query var in the template.