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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Archive /

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

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

Coloring part of "Page Name"


Go to End


7 Posts   1997 Views

Avatar
Teng

Community Member, 11 Posts

22 September 2008 at 8:54pm

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

Forum Moderator, 5523 Posts

22 September 2008 at 9:04pm

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

Community Member, 33 Posts

22 September 2008 at 9:09pm

Edited: 22/09/2008 9:10pm

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

Community Member, 11 Posts

24 September 2008 at 2:44pm

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

Community Member, 11 Posts

24 September 2008 at 2:48pm

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

Community Member, 11 Posts

24 September 2008 at 5:09pm

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

Community Member, 33 Posts

24 September 2008 at 7:02pm

Edited: 24/09/2008 7:04pm

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.