[solved] Get value of {title} tag and use it in PHP script

The place to talk about things that are related to CMS Made simple, but don't fit anywhere else.
Post Reply
jpage101
New Member
New Member
Posts: 9
Joined: Wed Nov 16, 2011 2:53 pm

[solved] Get value of {title} tag and use it in PHP script

Post by jpage101 »

Hello All,

First, I will apologize for my lack of knowledge on the subject. I was basically given a project to work on and told to get it to work. 2 days ago I had absolutely zero experience with PHP or CMSMS. I am doing my best but need some help here. I think it may just be something simple.

In our template we want to have the title of the page be displayed at the top of our content section. We were using the {title} tag for this. However, we needed the title to be in 2 colors. The first half of the title in one color, the second half in another. With the help of some code writers, I now have the following code which I have turned in to a User Defined Tag: {split_title}

Code: Select all

function multiColorHeading($input,$class1 = 'res-heading-black',$class2 = 'res-heading-golden') {
   $temp = explode(' ',$input);
   $half = count($temp) / 2;
   $first = array_slice($temp,0,$half);
   $second = array_slice($temp,$half);
   return  '<span class="'.$class1.'">'.implode(' ',$first).'</span>&nbsp;<span class="'.$class2.'">'.implode(' ',$second).'</span>';
}

$myTitle = 'Title Goes Here'; {
   echo multiColorHeading($myTitle);
}
As you can see, I have the variable $myTitle set to be a static string right now. When I test the page, this works. I need to be able to set the variable $myTitle to be equal to the value the {title} tag produces. I tried using the following but it did not work

Code: Select all

$myTitle = $smarty->get_template_vars('title');
I think maybe because "title" is a UDT and not a template variable? Any help here is very much appreciated.
Last edited by jpage101 on Thu Dec 01, 2011 3:17 pm, edited 1 time in total.
Wishbone
Power Poster
Power Poster
Posts: 1368
Joined: Tue Dec 23, 2008 8:39 pm

Re: Get value of {title} tag and use it in PHP script

Post by Wishbone »

{title} is a smarty tag. So are UDTs. get_template_vars() only returns smarty variables.

There is a way to get smarty tag output directly from a UDT, but I always forget and have to figure it out each time....

You can do {title assign=title} which will place the title in {$title}. Place this in your template before your UDT call. You can then pass this variable to your UDT, or use get_template_vars('title') to get this value.
tomgsd
Forum Members
Forum Members
Posts: 74
Joined: Tue Feb 12, 2008 10:00 am

Re: Get value of {title} tag and use it in PHP script

Post by tomgsd »

You can use the cms_utils class in your UDT. It has a method called get_current_content that will return a ref to the content object for the current page. If you look in "lib/classes/class.ContentBase.php" you'll find all the accessor methods you need to get at the content properties.

Something like this:

Code: Select all

$contentobj = cms_utils::get_current_content();
$page_title = $contentobj->Name();
jpage101
New Member
New Member
Posts: 9
Joined: Wed Nov 16, 2011 2:53 pm

Re: Get value of {title} tag and use it in PHP script

Post by jpage101 »

wishbone,

That worked exceptionally well. Another question if I may...How do I apply this same concept to global content blocks I have created?

For example, lets say I have a global content block called "MainSlogan". The below doesn't seem to work

Code: Select all

{global_content name='MainSlogan' assign='MainSlogan'}
Wishbone
Power Poster
Power Poster
Posts: 1368
Joined: Tue Dec 23, 2008 8:39 pm

Re: Get value of {title} tag and use it in PHP script

Post by Wishbone »

According to the help, it does support assign:

Code: Select all

[b]What does this do?[/b]
Inserts a global content block into your template or page.

[b]How do I use it?[/b]
Just insert the tag into your template/page like: {global_content name='myblob'}, where name is the name given to the block when it was created.

[b]What parameters does it take?[/b]
name - The name of the global content block to display.
(optional) assign - The name of a smarty variable that the global content block should be assigned to.

If it doesn't work, you can always use:

Code: Select all

{capture assign=MainSlogan}{global_content name=MainSlogan}{/capture}
Wishbone
Power Poster
Power Poster
Posts: 1368
Joined: Tue Dec 23, 2008 8:39 pm

Re: Get value of {title} tag and use it in PHP script

Post by Wishbone »

..or you can reference the GCB directly from your UDT

Code: Select all

$myGCB = $smarty->fetch('globalcontent:MainSlogan');
If you want to be really fancy, you can pass your GCB as a parameter to make your UDT flexible and portable.

use:

Code: Select all

{myUDT gcb=MainSlogan}
.. and in the UDT:

Code: Select all

$myGCB = $smarty->fetch('globalcontent:' . $params['gcb']);
jpage101
New Member
New Member
Posts: 9
Joined: Wed Nov 16, 2011 2:53 pm

Re: Get value of {title} tag and use it in PHP script

Post by jpage101 »

Thanks Wishbone! i was able to go the fancy route and get it to work (surprisingly with my lack of php programming.) Your examples were spot on, much appreciated.
Post Reply

Return to “The Lounge”