Ive created an asses for my site in the content type tpl file. Its mainly HTML but also some simple PHP. What are the steps to move this to a theme function?
-
What do you mean by "asses"?– roobyOct 21, 2014 at 15:54
-
Its a section of the page, so basically some HTML.– EvanssOct 21, 2014 at 17:23
-
Based on your other comment on an answer it seems like what you want to do is "How to create a theme function for custom HTML markup and then call it in my node template?".– roobyOct 21, 2014 at 17:47
3 Answers
You should be able to use the template_preprocess_node function to move your logic from your tpl files into the theme function. In order to use the function, replace template with the name of your theme.
function mytheme_preprocess_node(&variables){
if($variables['node']->type == 'my_content_type'){
//change what you want to the node here.
}
}
You could do the logic you need to inside this functionn, save the output to a variable in $variable (it's a reference var, so it dosn't need to be returned) and then just print out that variable in your tpl.php file.
-
I think you've misunderstood me. I have some HTML written directly into the node tpl. I want to move this into a new tpl file and load that file into the node tpl. I would also like a theme function so down the line I can add some configuration options.– EvanssOct 21, 2014 at 17:24
Why not just put what you need in a regular PHP file in your theme directory. In the tpl file just do a PHP include call.
<?php include("your_html.php"); ?>
-
I would like to be able to add configuration later, eg chose from 2 similar layouts.– EvanssOct 21, 2014 at 18:03
-
Okay well for the HTML inclusion I'd recommend PHP inclusion. Look at template_preprocess_node and other preprocessor functions for your configuration. Oct 21, 2014 at 18:45
In template.php:
THEMENAME_theme() {
$themes = array (
'THEMENAME_FEATURENAME' => array(
'template' => 'templates/TPL-FILE-NAME',
'arguments' => array('id' => null),
),
);
return $themes;
}
In content type tpl file:
<?php print theme('THEMENAME_FEATURE'); ?>
In my templates folder is TPL-FILE-NAME.tpl.php
This does pull in HTML from the tpl.php file however PHP such as <?php print $title; ?>
and <?php print $GLOBALS['$base_url'] . $node_url ?>
no long work (they did when they were in the content type tpl file).
Also a paid video here: https://drupalize.me/videos/strategies-keeping-template-files-clean?p=1146