Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Archive

Our old forums are still available as a read-only archive.

Moderators: martimiz, Sean, biapar, Willr, Ingo, simon_w

Coloring part of "Page Name"


Reply

7 Posts   1499 Views

Avatar
Teng

22 September 2008 at 8:54pm Community Member, 11 Posts

Hello,

I need to change the first word of Page name to what would be the best way of doing this? Is it possible to access the data, explode it and use a span to color it before it is passed to the view?

Avatar
Willr

22 September 2008 at 9:04pm Forum Moderator, 5511 Posts

Do you mean part of the title field on each page?

You can do this on your page_controller class in page.php

// override title from the database
function Title() {
$this->Title = ""; // explode , strip do whatever to add the <span> </span>
return $this->Title;
}

Avatar
FlorianH

22 September 2008 at 9:09pm (Last edited: 22 September 2008 9:10pm), Community Member, 33 Posts

You could write a function in your Controller and call this function in your template.

function coloredPageName()
{
$parts = explode(" ", $this->Title);
$i = 1;
$newTitle = "<span>" . $parts[0] . "</span>";
while($i <= ($parts->Count() - 1))
{
$newTitle = $newTitle . " " . $parts[$i];
$i++;
}
return $newTitle;
}

I didn't checked this code but it should work. You should be able to call the new Title with "$coloredPageName" in your .ss Template.

Avatar
Teng

24 September 2008 at 2:44pm Community Member, 11 Posts

Thanks for your help guys,
I'm having a few problems with the actual span output and with Count(),

function Title(){
      $parts = explode(" ", $this->Title);
      $this->Title = "<span>" . $parts[0] . "</span>";
      $i = 1;
      while($i <= ($parts->Count() - 1))
      {
         $this->Title = $this->Title . " " . $parts[$i];
         $i++;
      }
      return $this->Title;
   }

When I run this i get "Call to a member function Count() on a non-object", if I remove the while loop, to test if the first word is getting colorized, I get the output of <span>FirstWord</span> displayed on the page?

Avatar
Teng

24 September 2008 at 2:48pm Community Member, 11 Posts

Also when I tried adding color, "<span style="color:red;">" . $parts[0] . "</span>";
I get unexpected T_STRING, tried using different variations of " and ' to no avail

Avatar
Teng

24 September 2008 at 5:09pm Community Member, 11 Posts

Thought I would post the solution we used for future reference

...controller...

   function FirstTitle(){
      $parts = explode(" ", $this->Title);
      $startTitle = $parts[0];
      
      return $startTitle;
   }
   
   function RestOfTitle(){
      $parts = explode(" ", $this->Title);
      $rest = array_shift($parts);
      
      return implode(" ", $parts);
   }

...template...
<h1><span style="color:red;">$FirstTitle</span> $RestOfTitle</h1>

Avatar
FlorianH

24 September 2008 at 7:02pm (Last edited: 24 September 2008 7:04pm), Community Member, 33 Posts

I get the output of <span>FirstWord</span> displayed on the page?

Use $coloredPageName.RAW instead. The "non-Object" problem should be fixed by writing a function into your model which returns the Title and change the

$parts = explode(" ", $this->Title);


with

$parts = explode(" ", Model::TitleFunction());

Anyway, adding the HTML-Code to your Template is even better regarding to the MVC Design.