How can i use multiple template files for a Joomla Module
I am trying to create my own module, i followed the following tutorial: LINK
in the file "/modules/mod_hello_world2/mod_hello_world2.php" resides the code below.
// get the items to display from the helper
$items = ModHelloWorld2Helper::getItems($userCount);
// include the template for display
require(JModuleHelper::getLayoutPath('mod_hello_world2'));
This code is displaying the content of '/modules/mod_hello_world2/tmpl/default.php' with the data of $items parsed in there.
Now I am wondering if and how I can call upon different template files. I'd like to create one for a form and another for the result.开发者_如何学运维 Since it is best practice to keep code and HTML seperated I'd like to do so.
Any help is very much welcome!
JModuleHelper::getLayoutPath has an optional, further parameter for the layout.
http://api.joomla.org/Joomla-Framework/Application/JModuleHelper.html#getLayoutPath
So …
require(JModuleHelper::getLayoutPath('mod_hello_world2', 'mylayout'));
Use in your mod_your_module.php the folowing:
$layout = $params->get('layoutChoice');
require(JModuleHelper::getLayoutPath('mod_your_module', $layout ));
Then in your mod_your_module.xml write this:
<field name="layoutChoice" type="list" default="default" label="mod_layout_choice" description="mod_layout_choice_description" >
<option value="default">default</option>
<option value="default2">default2</option>
<option value="default3">default3</option>
<option value="default4">default4</option>
</field>
Then make sure that corresponding files default.php ,default2.php, default3.php, default3.php are in your module /tmpl directory.
That configuration work for Joomla 2.5.
Doing it in that way, you can choose your different template from administration menu of module in advanced or basic section depend where you write above in your_module.xml file.
You can prolong above configuration writing in mod_your_module.xml file the flowing:
<field name="layoutChoice" type="list" default="default" label="MOD_LAYOUT_CHOICE" description="MOD_LAYOUT_CHOICE_DESC" >
<option value="default">MOD_LAYOUT_CHOICE_DEFAULT</option>
<option value="default2">MOD_LAYOUT_CHOICE_DEFAULT2</option>
<option value="default3">MOD_LAYOUT_CHOICE_DEFAULT3</option>
<option value="default4">MOD_LAYOUT_CHOICE_DEFAULT4</option>
</field>
And then write in your en-GB.mod_your_module.ini the folowing:
MOD_LAYOUT_CHOICE="Choose which Template to Use"
MOD_LAYOUT_CHOICE_DESC="Your discription for each template is written hire"
MOD_LAYOUT_CHOICE_DEFAULT="First Template"
MOD_LAYOUT_CHOICE_DEFAULT2="Second Template"
MOD_LAYOUT_CHOICE_DEFAULT3="Third Template"
MOD_LAYOUT_CHOICE_DEFAULT4="Forth Template"
精彩评论