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

Do something cool with CMS? Show us ...
This board is for 'Answers', and the discussion of answers... Not for questions.
Post Reply
Wishbone
Power Poster
Power Poster
Posts: 1368
Joined: Tue Dec 23, 2008 8:39 pm

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

Post 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!
Last edited by Wishbone on Wed Oct 27, 2010 6:45 pm, edited 1 time in total.
vilkis

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

Post by vilkis »

Nice idea!
vilkis
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm

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

Post 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')}
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
Wishbone
Power Poster
Power Poster
Posts: 1368
Joined: Tue Dec 23, 2008 8:39 pm

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

Post 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}
Last edited by Wishbone on Wed Oct 27, 2010 8:56 pm, edited 1 time in total.
vilkis

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

Post 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
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm

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

Post 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.
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
Wishbone
Power Poster
Power Poster
Posts: 1368
Joined: Tue Dec 23, 2008 8:39 pm

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

Post by Wishbone »

Thanks for the info. :) It opens up new possibilities for me.
Post Reply

Return to “Tips and Tricks”