Call Global Content Block from User Defined Tag

Do something cool with CMS? Show us ...
This board is for 'Answers', and the discussion of answers... Not for questions.
Post Reply
KingManon

Call Global Content Block from User Defined Tag

Post 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
cyberman

Re: Call Global Content Block from User Defined Tag

Post by cyberman »

Thanks for your post - have added it to wiki :).

http://wiki.cmsmadesimple.org/index.php ... efined_Tag
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm

Re: Call Global Content Block from User Defined Tag

Post 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.
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.
Pokky
New Member
New Member
Posts: 2
Joined: Tue Oct 05, 2010 2:24 pm

Re: Call Global Content Block from User Defined Tag

Post 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}'}");
}
User avatar
Dr.CSS
Moderator
Moderator
Posts: 12711
Joined: Thu Mar 09, 2006 5:32 am

Re: Call Global Content Block from User Defined Tag

Post 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...
Pokky
New Member
New Member
Posts: 2
Joined: Tue Oct 05, 2010 2:24 pm

Re: Call Global Content Block from User Defined Tag

Post 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.
lume

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

Post 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'
Post Reply

Return to “Tips and Tricks”