Page 1 of 1

Call Global Content Block from User Defined Tag

Posted: Mon Sep 24, 2007 6:50 pm
by KingManon
So why should you want to >>Call Global Content Block from User Defined Tag<<?

Well, my users find it hard to remember to write "{global_content name='newsbox'}" and have been asking me if they couldn't just write "{newsbox}"...Now they can, thanks to Calguy1000.

1 - Make a new Global Content Block and enter the content you want shown and call it eg. newsbox
2 - Make a new User Defined Tag also called newsbox and enter the following code:

Code: Select all

global $gCms;
$news = $gCms->modules['News']['object']; 
return $news->ProcessTemplateFromData('{global_content name=\'newsbox\'}');
Now your users simply can write just "{newsbox}"  :)

Thanks go to Calguy1000 as I have nothing to thank for this except asking the question and posting the answer here  ;D

Re: Call Global Content Block from User Defined Tag

Posted: Mon Sep 24, 2007 9:53 pm
by cyberman
Thanks for your post - have added it to wiki :).

http://wiki.cmsmadesimple.org/index.php ... efined_Tag

Re: Call Global Content Block from User Defined Tag

Posted: Mon Sep 24, 2007 10:44 pm
by calguy1000
for perpetuity, the use of 'News' in the $gCms->modules... line doesn't mean much.  it's only 'getting' a varaible that 'should' be installed/.  I could do a loop around $gCms->modules to find the first installed, and active module, but it solved the problem.

If you don't have 'News' installed, then the rest is left as an excercise to the user.

Re: Call Global Content Block from User Defined Tag

Posted: Tue Oct 05, 2010 2:29 pm
by Pokky
Hi,

I had disabled my news module, and rather than re-enabling it I wrote this little function. You can tag it on to config.php, or as I do, my config.php loads a custom_functions.php script with my extra functions:

Code: Select all

function getGlobalContentBlock($blockName) {
  global $gCms;
  $modules = array_keys($gCms->modules);
  $modObj = $gCms->modules[$modules[0]]['object'];
  return $modObj->ProcessTemplateFromData("{global_content name='{$blockName}'}");
}

Re: Call Global Content Block from User Defined Tag

Posted: Wed Oct 06, 2010 7:00 pm
by Dr.CSS
The next time you upgrade it will get overwritten...

Hacking core files is never a good idea and if you do you lose all support from the dev. team...

Re: Call Global Content Block from User Defined Tag

Posted: Wed Oct 06, 2010 7:55 pm
by Pokky
Hmm. Not sure where the hacking of a core file comes into play? I have the function defined in config.php which doesn't get overwritten.

Alternatively, create custom_functions.php and in config.php do an "include" on it - won't ever be touched by an upgrade.

Re: Call Global Content Block from udt (with parameters)

Posted: Sat Oct 08, 2011 12:39 pm
by lume
We stick with the example 'newsbox'
A cleaner Version of this (without Access to the $gCms->modules) Variable lookls like this. In Addition I added some more smarty lines, to allow for global_content with parameters

Code: Select all

global $gCms;
// any module will do, but if we have a newsbox example News is probably involved anyway
$news =& $gCms->GetModuleInstance('News'); 
// Prepare the template to process via the Modules ProcessTemplateFromData function
// inject the parameter via smarty into smarty and call gc
$template = '{assign var=\'paramname\' value=\''.$params['paramname'].'}\''.
'{global_content name=\'NameOfGlobalContentBlock\'}';

echo $news->ProcessTemplateFromData($template);
Now your users simply can write just "{newsbox}"  :)
If you want to pass param 'paramname' you can call this like "{newsbox paramname='somevalue'}"

In your global_content you can use {$paramname} to use the value passed as argument to the udt 'newsbox'