Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Coloring part of "Page Name"

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

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

Page: 1
Go to End
Author Topic: 1490 Views
  • Teng
    Avatar
    Community Member
    11 Posts

    Coloring part of "Page Name" Link to this post

    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?

  • Willr
    Avatar
    Forum Moderator
    5508 Posts

    Re: Coloring part of "Page Name" Link to this post

    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;
    }

  • FlorianH
    Avatar
    Community Member
    33 Posts

    Re: Coloring part of "Page Name" Link to this post

    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.

  • Teng
    Avatar
    Community Member
    11 Posts

    Re: Coloring part of "Page Name" Link to this post

    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?

  • Teng
    Avatar
    Community Member
    11 Posts

    Re: Coloring part of "Page Name" Link to this post

    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

  • Teng
    Avatar
    Community Member
    11 Posts

    Re: Coloring part of "Page Name" Link to this post

    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>

  • FlorianH
    Avatar
    Community Member
    33 Posts

    Re: Coloring part of "Page Name" Link to this post

    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.

    1490 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.