Jump to:

3214 Posts in 848 Topics by 810 members

Template Questions

SilverStripe Forums » Template Questions » &gt; and &lt; Rendered in Template Instead of <>

Moderators: martimiz, Howard, Sean, Ryan M., biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 969 Views
  • Garrett
    Avatar
    Community Member
    245 Posts

    &gt; and &lt; Rendered in Template Instead of <> Link to this post

    Hi,

    I am having a hell of a time rendering a $Title variable in the template which contains HTML. Somewhere between the controller and the template

    < and >

    are being replaced with

    &lt; and &gt;

    as well as many other characters. I've tried absolutely everything to prevent this and to re-replace them but to no avail. The title is plain text in the database, but I have some titles which are Tweets, and so in the controller, in my DataObject::get() result I am parsing them to change the parts that are supposed to be links into links. They come back from the parser perfectly:

    horngroup: <a href="http://twitter.com/#!/search?q=%23Apple" target="_blank">#Apple</a> Announces A $499 Retina-Equipped <a href="http://twitter.com/#!/search?q=%23iPad" target="_blank">#iPad</a> (It’s Just Called iPad) <a href="http://t.co/cFPe1HPg" target="_blank">http://t.co/cFPe1HPg</a>; <a href="http://twitter.com/#!/search?q=%23appleevent" target="_blank">#appleevent</a>


    But when I render $Title in the template, it looks like this:

    horngroup: &lt;a href=&#39;http://twitter.com/#!/search?q=%23Apple&#39; target=&#39;_blank&#39;&gt;#Apple&lt;/a&gt;’s new &lt;a href=&#39;http://twitter.com/#!/search?q=%23iPad&#39; target=&#39;_blank&#39;&gt;#iPad&lt;/a&gt;: 9 features that matter &lt;a href=&#39;http://t.co/IOwFyCii&#39; target=&#39;_blank&#39;&gt;http://t.co/IOwFyCii&lt;/a&gt;

    First of all, why is this happening, and second, how can I stop it? It seems I need a method ion the template itself to render it as HTML but I can't figure it out. Any help out there?

    $stream_items = DataObject::get($table,
                   "`Category`.`CategoryFilterURL` = '".$url."'","Date DESC",
                   "LEFT JOIN `".$table."_Categories` ON `".$table."_Categories`.`".$table."ID` = `".$table."`.`ID`
                   LEFT JOIN `Category` ON `Category`.`ID` = `".$table."_Categories`.`CategoryID`",
                   $limit);
                foreach($stream_items as $stream_item) {
                   
                   $IsTweet = false;
                   // if is Tweet
                   if($stream_item->ClassName=="ExternalBlogEntry") {
                         
                      $IsTweet = true;
                         
                      $text = $stream_item->Title;
                      $text = $this->parseTweet($text);
                      $stream_item->Title = $text;
                      
                   }
                   $stream_item->IsTweet = $IsTweet;
                }
                return $stream_items;

    The Title is a property within the $stream_items object.

    Thanks,
    Garrett

  • Willr
    Avatar
    Forum Moderator
    5171 Posts

    Re: &gt; and &lt; Rendered in Template Instead of <> Link to this post

    Every field you output to the template is run through XML() to make sure it is HTML sure it doesn't cause trouble with XSS. If you field contains HTML which you want to be rendered, ensure it is a 'HTMLText' data type rather than Varchar.

  • Garrett
    Avatar
    Community Member
    245 Posts

    Re: &gt; and &lt; Rendered in Template Instead of <> Link to this post

    Hi @willr, thanks for replying - I had a feeling this was the issue, though I am very reluctant to modify core files. The Title field is declared in SiteTree.php. Is there a way to overload this field in mysite code, in _config.php or otherwise?

    //Garrett

  • Willr
    Avatar
    Forum Moderator
    5171 Posts

    Re: &gt; and &lt; Rendered in Template Instead of <> Link to this post

    Create a new variable for it and add it to the casting list

    static $casting = array(
    'HTMLTitle' => 'HTMLText'
    );

    function getHTMLTitle() {
    return $this->Title;
    }

    Then use $HTMLTitle rather than $Title.

  • Garrett
    Avatar
    Community Member
    245 Posts

    Re: &gt; and &lt; Rendered in Template Instead of <> Link to this post

    Done-zo. Thanks as always @willr.

    //Garrett

    969 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.