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?
This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.
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.
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?
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;
}
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.
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?
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
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>
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);
$parts = explode(" ", Model::TitleFunction());
Anyway, adding the HTML-Code to your Template is even better regarding to the MVC Design.