Jump to:

3435 Posts in 1026 Topics by 866 members

Template Questions

SilverStripe Forums » Template Questions » Pass HTML to Template

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

Page: 1
Go to End
Author Topic: 766 Views
  • leafsfan41
    Avatar
    Community Member
    4 Posts

    Pass HTML to Template Link to this post

    Hi Guys,

    I am hoping someone can help me, because I'm going nuts right now. I am trying to create a section on the home page that shows files that have been uploaded to specific pages. I run through the three tables I am using to house the information, and I create a $html variable that I then pass to the template. The template, in theory, would simply run the HTML and problem is solved. However it appears as though the template file is escaping the HTML automatically and when I view the source I see:

    <dt>Public Works & Enviromental Services </dt><dd><a href="assets/Uploads/Metro-CollaborateProduct-Brochure.pdf">Metro CollaborateProduct Brochure</a></dd>

    I am new to Silverstripe, as I am used to building custom systems so I've been struggling with some of the logic/reasoning behind why certain things work certain ways.

    Here is my code:

    Template:

       <% control Resources %>
        $html
       <% end_control %>

    Here is the function being used:

    function Resources()
       {
          $resource = DataObject::get("SiteTree", "ClassName = 'ResourcePage'");
          $html = '';
          
          foreach ($resource as $docData)
          {
             $html .= '<dt>'.$docData->Title.'</dt>';
             $fileListing = DataObject::get("Resource", "ResourcePageID = $docData->ID");
             
             foreach ($fileListing as $singleFileList) {      
                $files = DataObject::get("File", "ID = $singleFileList->AttachmentID");
                
                foreach ($files as $single)
                {         
                   $html .= '<dd><a href="'.$single->FileName.'">'.$single->Title.'</a></dd>';
                   
                }
                
             }
             
          }
          
       
          return $html;
    }

    Any help with this would be great as I'm going crazy trying to understand what's happening.

  • dgaussin
    Avatar
    Community Member
    4 Posts

    Re: Pass HTML to Template Link to this post

    Not sure why you're building your html in your Resources function, could be more simple to let your html tags in your template.

    However, you could try $html.Raw in your template to avoid escaping, I think it might work.

  • Devlin
    Avatar
    Community Member
    215 Posts

    Re: Pass HTML to Template Link to this post

    Resources() returns no DataObject, so your iteration won't work in your template. Just call $Resources instead.

    Template

    $Resources

    Further, I suggest you read the docs for advanced templating and datamodel.
    http://doc.silverstripe.org/sapphire/en/reference/advanced-templates
    http://doc.silverstripe.org/sapphire/en/topics/datamodel

  • leafsfan41
    Avatar
    Community Member
    4 Posts

    Re: Pass HTML to Template Link to this post

    @Devlin - Thanks so much Devlin! That worked. Being new with Silverstripe I made the mistake to assume that <% control Resources %> called the function. This is great. Thanks so much!

    @dgaussin - Thanks for your help as well. I know the way I am building it right now might not be the greatest, but I'm not sure how to pull information from 3 tables and display it on a single page. I'm sure there is a way to do it, but being on such a tight deadline I had to go to what I knew after failing to have success in other routes. I've bought a SS book, so hopefully once it gets here it, along with the online documentation, will allow me to grow and get better with the system.

    Thanks again both you.

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