Page 1 of 1

Using $this->Lang() inside of a template

Posted: Wed Oct 27, 2010 5:27 pm
by Wishbone
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

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']));
}

?>
Usage:

Code: Select all

{lang module='mymodule' name='mytext'}
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!

Re: Using $this->Lang() inside of a template

Posted: Wed Oct 27, 2010 7:03 pm
by vilkis
Nice idea!
vilkis

Re: Using $this->Lang() inside of a template

Posted: Wed Oct 27, 2010 8:46 pm
by calguy1000
CGExtensions just does a:

$smarty->assign($this->GetName(),$this); 
which means that I can call module methods within the template.

like:
{$Orders->Lang('some_key')}

Re: Using $this->Lang() inside of a template

Posted: Wed Oct 27, 2010 8:49 pm
by Wishbone
I like that idea too... When passing $this, I'm guessing it passes a pointer, and not passing the entire content of $this to smarty? I wasn't aware that you could call PHP from smarty without resorting to using {php}

Re: Using $this->Lang() inside of a template

Posted: Thu Oct 28, 2010 11:34 am
by vilkis
calguy1000 wrote: $smarty->assign($this->GetName(),$this);   
which means that I can call module methods within the template.
I like the solution of wishbone as it assigns only Lang method of modules and not all methods.

vilkis

Re: Using $this->Lang() inside of a template

Posted: Thu Oct 28, 2010 6:22 pm
by calguy1000
In PHP5 everything is passed by reference by default, and 'lazy copying' is done if an object is modified.
Thereby:

a:  $smarty->assign_by_ref  is only needed if the object/array/variable may be modified and you want  to save those changes.

b: passing the entire object to smarty only accounts for another element in the smarty variables array that contains a 'reference' to the object (very little overhead). 

c: passing the entire object to smarty allows you to do funky things like:

Code: Select all

{assign value=$ModuleName->SomeMethodToGetData()  var='data'}
and then parse it in smarty for display purposes.    Best practices means that the individual methods 'should' be doing permissions checks of the appropriate types anyways... if they are... there's no security issue, only alot greater flexibility.

As an aside: I'm also doing this in CGExtensions:

Code: Select all

$smarty->assign('actionid',$id)
which allows me greater control in templates that create forms.
i.e:

Code: Select all

<input type="submit" name="{$actionid}submit" value="{$Orders->Lang('submit')}" class="foo"/>
This is particularly valuable on frontend form templates where customization of classes and adding funky javascript is required.

Re: Using $this->Lang() inside of a template

Posted: Thu Oct 28, 2010 6:36 pm
by Wishbone
Thanks for the info. :) It opens up new possibilities for me.