Gentlemen,

Documentation says hook_theme "return[s] an array of arrays. The key to each sub-array is the internal name of the hook, and the array contains info about the hook".
Now, I'm coding a module that provides a custom content type (a chess game) and my question is: can I use this to provide multiple templates for my custom content type? For example, I'd like to show by default a list with info about the chess game and a MENU_LOCAL_TASK that when clicked, shows the chessboard. I was thinking in doing something like:

function partida5_theme() {
	return array
	(
		'partida5_info' => array
		(
			'template' => 'partida5_info',
			'arguments' => array
			(
				'titulo' => NULL,
				'descripcion_partida' => NULL,
				'jugador_blancas' => NULL,
				'jugador_negras' => NULL,
				'tiempo_por_participante' => NULL,
				'turno_de' => NULL,
			),
		),
		'partida5_juego' => array
		(
			'template' => 'partida5_juego',
			'arguments' => array
			(
				'titulo' => NULL,
				'tiempo_por_participante' => NULL,
				'turno_de' => NULL,
				'jugador_blancas_uid' => NULL,
				'jugador_negras_uid' => NULL,
				'estado_tablero' => NULL,
			),
		),
	);
}

partida5_info being the info template and partida5_juego being the chessboard's template.
Is this the right way? And how do I select a specific template for each case?

Thanks in advance,
Léster

Comments

lbelloq’s picture

Solved, in case someone was wondering.
1: Declare all the templates you'll need in hook_theme, like this:

function mymodule_theme() {
    return array
    (
        'theme_1' => array
        (
            'template' => 'template_1',
            'arguments' => array
            (
                'argument_1' = NULL,
                'argument_2' = NULL,
                'argument_3' = NULL
            ),
        ),
        'theme_2' => array
        (
            'template' => 'template_2',
            'arguments' => array
            (
                ...
            ),
        ),
        'theme_3' => array
        (
            'template' => 'template_3',
            'arguments' => array
            (
                ...
            ),
        ),
    );
}

2: Create all the template files you'll need. For example, were I to implement the 3 themes I declared in hook_theme, I have to create 3 files: template_1.tpl.php, template_2.tpl.php, and template_3.tpl.php . Note that the filename for each template is the declared 'template' name, plus .tpl.php .

3: Each time you need to call a specific template, use the return theme() magic to do it. For example, when I want to use the first template, do it like this:

function show_template_1(PARAMETERS_YOU'LL_NEED_HERE) {
    //Process the parameters until obtaining the necessary data.
    //The idea is to prepare the arguments you declared in theme_1.
    //Once is done, do the following:
    return theme(
        'theme_1',
        $argument_1,
        $argument_2,
        $argument_3
    );
}

And it's done.