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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Create file from Assets


Go to End


3 Posts   2879 Views

Avatar
Mo

Community Member, 541 Posts

30 April 2011 at 9:15am

Hi All,

Quick question, does anyone know if it is possible to generate a file (that will be put in the assets folder) from a template?

I want to create an XML file with content from a form and generate the XML file from a template.

I suppose I could use SimpleXML, would prefer to use a template if I can.

Cheers in advance,

Mo

Avatar
swaiba

Forum Moderator, 1899 Posts

3 May 2011 at 1:03am

Hi Mo,

I think these two things may help...

form the Filetest.php


		$folder = Folder::findOrMake('/FileTest/');
		$testfilePath = 'assets/FileTest/CreateWithFilenameHasCorrectPath.txt'; // Important: No leading slash
		$fh = fopen(BASE_PATH . '/' . $testfilePath, "w");
		fwrite($fh, str_repeat('x',1000000));
		fclose($fh);
				
		$file = new File();
		$file->Filename = $testfilePath;
		// TODO This should be auto-detected
		$file->ParentID = $folder->ID;
		$file->write();

to render something without a page controller...

			$vd = new ViewableData();
			$ad = new ArrayData(array(
				'Data' => $dos
			));
			$strHTML = $vd->customise($ad)->renderWith('MyTemplate');

Avatar
Mo

Community Member, 541 Posts

3 May 2011 at 1:39am

Oh cool, hopefully that will do what I want :-). I was thinking about renderWith but wasn't sure if it could be used like that. Fingers crossed!

Cheers,

Mo