Using $this->Lang() inside of a template
Posted: Wed Oct 27, 2010 5:27 pm
I found it unintuitive to have to pass $this->Lang() references from PHP into a module template when no PHP processing is necessary. For example, a template designer might want to place some text, or a label to a form inside a template, and either have to hard-code the text into the template, or communicate with the programmer to pass this data to the template. Even if the template designer and the programmer is the same person, it's a pain and should be unnecessary to edit code to place some text in a template. I did a search on the forum for this, and came up with the following link to a very old post.
http://forum.cmsmadesimple.org/index.ph ... 325.0.html
I came up with a 'lang' plugin, and posted on this thread, but thought that it warranted a 'tips and tricks' thread. I've expanded it a tiny bit since that post.
function.lang.php
Usage:
The above smarty example will look up Lang('mytext') in the 'mymodule' module and display the text. If you have a lot of these in your template, you can assign a smarty variable called 'module' set to your module name in all your action php files, and can eliminate the " module='mymodule' " parameter.
Now templates can access Lang() without having to make changes to php code!
http://forum.cmsmadesimple.org/index.ph ... 325.0.html
I came up with a 'lang' plugin, and posted on this thread, but thought that it warranted a 'tips and tricks' thread. I've expanded it a tiny bit since that post.
function.lang.php
Code: Select all
<?php
function smarty_cms_function_lang($params) {
global $gCms;
$cmsmodules = $gCms->modules;
$smarty =& $gCms->GetSmarty();
$module = $params['module'] ? $params['module'] : $smarty->get_template_vars('module');
if (!$module) {
return("no module specified");
}
return($cmsmodules[$module]['object']->Lang($params['name']));
}
?>
Code: Select all
{lang module='mymodule' name='mytext'}
Now templates can access Lang() without having to make changes to php code!