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.

Template Questions

Pass HTML to Template


Reply

4 Posts   797 Views

Avatar
leafsfan41

17 September 2011 at 2:39am Community Member, 4 Posts

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.

Avatar
dgaussin

17 September 2011 at 3:41am Community Member, 4 Posts

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.

Avatar
Devlin

17 September 2011 at 3:49am (Last edited: 17 September 2011 3:54am), Community Member, 215 Posts

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

Avatar
leafsfan41

17 September 2011 at 3:54am Community Member, 4 Posts

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