tips:template_chooser
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
tips:template_chooser [2008-10-24 16:11] – 58.6.0.205 | tips:template_chooser [2013-06-10 02:10] (current) – old revision restored (2009/06/21 15:39) ach | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Template Chooser ====== | ||
+ | |||
+ | :!: Even if different, you should consider [[: | ||
+ | |||
+ | FIXME please update this tip to take new version in account | ||
+ | |||
+ | It did bother me, that in our DokuWiki I had to write the same stuff for certain types of documentation over and over again or copy it to my clipboard and then back into DokuWiki. | ||
+ | |||
+ | That's why I made this simple thing. If you hate JavaScript, you should certainly run away and hide now, because this will be gruesome... | ||
+ | |||
+ | The " | ||
+ | |||
+ | I have two files in my template-directory. The first one includes a function to display the template-selectbox, | ||
+ | |||
+ | So put these two files into your template-path (normally ' | ||
+ | |||
+ | The first one's called " | ||
+ | |||
+ | <code php> | ||
+ | <?php | ||
+ | if(!defined(' | ||
+ | if (is_null($conf[' | ||
+ | $conf[' | ||
+ | } | ||
+ | if (is_null($lang[' | ||
+ | $lang[' | ||
+ | } | ||
+ | function read_files($path, | ||
+ | $templates = array(); | ||
+ | $dh = opendir($path); | ||
+ | while (($file = readdir($dh)) !== false) { | ||
+ | if ((is_dir($path.'/' | ||
+ | $tmp = read_files($path.'/' | ||
+ | $templates = array_merge($templates, | ||
+ | } | ||
+ | if (preg_match($preg, | ||
+ | // Read in file | ||
+ | $contents = file($path.'/' | ||
+ | // Template name is in the first line and cutted out | ||
+ | $templatename = array_shift($contents); | ||
+ | if (preg_match('/ | ||
+ | $templates[$matches[1]] = $contents; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | closedir($dh); | ||
+ | return $templates; | ||
+ | } | ||
+ | function tpl_template_chooser() { | ||
+ | global $conf, | ||
+ | $template_dir = DOKU_INC.' | ||
+ | if (!file_exists($template_dir)) { | ||
+ | return true; | ||
+ | } | ||
+ | $templates = read_files($template_dir, | ||
+ | ?> | ||
+ | <script type=" | ||
+ | < | ||
+ | <?php | ||
+ | foreach ($templates as $key => $value) { | ||
+ | print "var newitem = new template_chooser_template(' | ||
+ | foreach ($value as $line) { | ||
+ | $line = addslashes(rtrim($line)); | ||
+ | print " | ||
+ | } | ||
+ | print " | ||
+ | } | ||
+ | ?> | ||
+ | //--> | ||
+ | </ | ||
+ | <?php | ||
+ | if (count($templates) > 0) { | ||
+ | ksort($templates); | ||
+ | print '< | ||
+ | print $lang[' | ||
+ | print '< | ||
+ | foreach ($templates as $key => $value) { | ||
+ | print '< | ||
+ | } | ||
+ | print '</ | ||
+ | print '</ | ||
+ | } | ||
+ | } | ||
+ | ?> | ||
+ | </ | ||
+ | |||
+ | The second one's called " | ||
+ | |||
+ | <code javascript> | ||
+ | var template_chooser_templates = new Array(); | ||
+ | |||
+ | function template_chooser_template(template_name, | ||
+ | { | ||
+ | this.template_name = template_name; | ||
+ | this.template_content = template_content; | ||
+ | } | ||
+ | |||
+ | function template_chooser_set_template(selector) | ||
+ | { | ||
+ | tpl_name = selector[selector.selectedIndex].value; | ||
+ | |||
+ | if (tpl_name == '' | ||
+ | return true; | ||
+ | } | ||
+ | |||
+ | textbox = document.getElementById(' | ||
+ | |||
+ | tpl_content = ''; | ||
+ | // Get Content | ||
+ | for (var i=0; i< | ||
+ | if (template_chooser_templates[i].template_name == tpl_name) { | ||
+ | tpl_content = template_chooser_templates[i].template_content; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | if (tpl_content != '' | ||
+ | textbox.value = textbox.value + tpl_content; | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Now, in your main.php, add these lines: | ||
+ | |||
+ | In the HTML-head area, add: | ||
+ | |||
+ | <script src="<? | ||
+ | |||
+ | and in the point where you'd like to have the selectbox add a: | ||
+ | |||
+ | <code php> | ||
+ | <?php | ||
+ | // Include template chooser | ||
+ | if (($ACT == ' | ||
+ | include(dirname(__FILE__).'/ | ||
+ | tpl_template_chooser(); | ||
+ | } | ||
+ | ?> | ||
+ | </ | ||
+ | |||
+ | After that, nothing should happen. That's because you probably haven' | ||
+ | |||
+ | If you create the namespace and put a document in it, you'll have to add a certain line before the content starts: | ||
+ | |||
+ | $$TEMPLATE:< | ||
+ | |||
+ | If you leave this line out, the document won't be show inside the selectbox. This line will be used to determine what to show inside the selectbox. This line will be dropped in the content, of course. | ||
+ | |||
+ | Now, there are two more things you'd like to do. In the lang.php-file of your chosen language, you should add " | ||
+ | |||
+ | And you should add the id "# | ||
+ | |||
+ | Append file ' | ||
+ | <code css> | ||
+ | /* --------------- | ||
+ | |||
+ | # | ||
+ | | ||
+ | | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | That should be all. After you have set up all this, you should have a nice selectbox showing you templates out of your template-namespace, | ||
+ | |||
+ | ===== Discussion ===== | ||
+ | |||
+ | * I've updated the PHP script to support subnamespaces under the templates-namespace. -- // [[develop@dieploegers.de|Dennis Ploeger]] 2006-05-02 14:03 // | ||
+ | |||
+ | ---- | ||
+ | I can get everything to work except the template names don't show up in the dropdown box. There are just empty lines added. When I choose a " | ||
+ | |||
+ | > I contacted the author and he was kind enough to help solve my problem. The template-chooser needs you to put " | ||
+ | ---- | ||
+ | * Where would be the best place to place the PHP code in my main.php:?: | ||
+ | |||
+ | * 2007-12-19: I've updated the PHP code to simplify the embedded JavaScript code. This should hopefully make the pages significantly smaller and solve some problems with backslashes. | ||
+ | |||
+ | * 2008-03-08: I made a small change in the tpl_template_chooser() function in order allow this " | ||
+ | :!: --- // | ||
+ | |||
+ | <code php> | ||
+ | |||
+ | function tpl_template_chooser() { | ||
+ | |||
+ | global $conf, | ||
+ | //My changes | ||
+ | $path=""; | ||
+ | if(empty($conf[' | ||
+ | $path=DOKU_INC; | ||
+ | $path.=" | ||
+ | } | ||
+ | else{ | ||
+ | $path=$conf[' | ||
+ | } | ||
+ | | ||
+ | $template_dir = $path.'/ | ||
+ | //End of my changes. | ||
+ | if (!file_exists($template_dir)) { | ||
+ | return true; | ||
+ | } | ||
+ | |||
+ | $templates = read_files($template_dir, | ||
+ | .... | ||
+ | |||
+ | </ | ||
+ | |||
+ | * 2008-10-24: My first attempt at PHP, so bear with me. I changed the '' | ||
+ | :!: --- // | ||
+ | |||
+ | <code php> | ||
+ | function read_files($path, | ||
+ | global $INFO, $conf, $ID; | ||
+ | $data = array($ID); | ||
+ | $id = $data[0]; | ||
+ | $templates = array(); | ||
+ | $dh = opendir($path); | ||
+ | while (($file = readdir($dh)) !== false) { | ||
+ | if ((is_dir($path.'/' | ||
+ | $tmp = read_files($path.'/' | ||
+ | $templates = array_merge($templates, | ||
+ | } | ||
+ | if (preg_match($preg, | ||
+ | // Read in file | ||
+ | $contents = file($path.'/' | ||
+ | // Replace macros | ||
+ | $contents = str_replace(' | ||
+ | $contents = str_replace(' | ||
+ | $contents = str_replace(' | ||
+ | $contents = str_replace(' | ||
+ | $contents = str_replace(' | ||
+ | $contents = str_replace(' | ||
+ | $contents = str_replace(' | ||
+ | // Template name is in the first line and cutted out | ||
+ | $templatename = array_shift($contents); | ||
+ | if (preg_match('/ | ||
+ | $templates[$matches[1]] = $contents; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | closedir($dh); | ||
+ | return $templates; | ||
+ | } | ||
+ | </ | ||
+ | \\ | ||
+ | ---- | ||
+ | \\ | ||
+ | 2009-06-21: I want to use the template chooser not only on new blank pages, but also on existing pages with existing code. But it doesn' | ||
+ | I remember that this worked before, but after upgrading to the new version " | ||
+ | --- // | ||
tips/template_chooser.txt · Last modified: 2013-06-10 02:10 by ach