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.

We've moved the forum!

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.

Template Questions /

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

Pass HTML to Template


Go to End


4 Posts   3523 Views

Avatar
leafsfan41

Community Member, 4 Posts

17 September 2011 at 2:39am

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

Community Member, 4 Posts

17 September 2011 at 3:41am

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

Community Member, 344 Posts

17 September 2011 at 3:49am

Edited: 17/09/2011 3:54am

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

Community Member, 4 Posts

17 September 2011 at 3:54am

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