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

[SOLVED] Issue with outputting $has_one and $has_many into template


Go to End


3 Posts   1325 Views

Avatar
Turismo

Community Member, 28 Posts

17 May 2016 at 10:00pm

Edited: 17/05/2016 10:03pm

I'm having trouble outputting a $has_one banner image ($banner), and looping some data objects setup via $has_many ($Sections). I've implemented these before, and just can't work out what I'm doing wrong! Both the image and the data objects have been added and are appearing as intended in the CMS, and are associated with the correct page. I've rerun /dev/build/ and ?flush. Here's my template code for templates/Layout/ServicePage.ss:

<% if $Banner %>
	<header class="page-banner">
		<figure class="media">
			<% with $Banner.SetWidth(1920) %>
		    <img src="$URL" alt="" title="" width="$Width" height="$Height">
		    <% end_with %>
    	</figure>
	</header>
<% end_if %>

<div class="article-content has-sidebar">
	<div class="wrapper">
		<article class="unit article-body nudge-up">
			<nav class="bread">
				<ul>
					<li><a href="$BaseHref" title="$SiteConfig.Title">Home</a> / </li>
					<% with $Level(2) %>
					<li><a href="$Link" title="$Title">$MenuTitle</a> / </li>
					<% end_with %>
					<li class="long">$Title</li>
				</ul>
			</nav>
			<h1 class="heading2">
				<i class="left has-circle">
                    <svg class="icon-bank">
                        <use xlink:href="$ThemeDir/images/icons/icons.svg#icon-bank"></use>
                    </svg>
                </i>
				<span>$Title</span>
			</h1>
			<h2 class="heading3">$Intro</h2>
			<div class="editor-content">
				$Content
			</div>

			<% loop $Sections %>
			<section class="section">
				<div class="column">
					<i class="has-circle">
	                    <svg class="icon-help">
	                        <use xlink:href="$ThemeDir/images/icons/icons.svg#icon-help"></use>
	                    </svg>
	                </i>
				</div>
				<div class="column">
					<h3 class="heading5">$Title</h3>
					<div class="editor-content">
						$Content
					</div>
				</div>
			</section>
		    <% end_loop %>

		</article>
		<aside class="unit article-sidebar nudge-up">
			WIDGETS HEREE
		</aside>
	</div>
</div>

Here's mysite/code/ServicePage.php class:

class ServicePage extends Page {

	private static $db = array (
	    'Intro' => 'Text',
	    'USPStripIntro' => 'Varchar(255)'
	);

	private static $has_one =  array (
		'Banner' => 'Image'
	);

	private static $has_many = array (
        'Sections' => 'ServiceSection'
    );

	public function getCMSFields() {
		$fields = parent::getCMSFields();
		$fields->addFieldToTab('Root.Main', TextareaField::create('Intro','The introduction for the page'),'Content');
		$fields->addFieldToTab('Root.Main', TextField::create('USPStripIntro','USP Intro Text'),'Content');
		$fields->addFieldToTab('Root.Attachments', $banner = UploadField::create(
			'Banner',
			'The banner image for the page'
		));
		$fields->addFieldToTab('Root.Sections', GridField::create(
            'Sections',
            'Sections for this page',
            $this->Sections(),
            GridFieldConfig_RecordEditor::create()
        ));

		$banner
	        ->setFolderName('page-banners')
	        ->getValidator()->setAllowedExtensions(array('jpg', 'jpeg', 'png'));

		return $fields;
	}

}

class Service_Controller extends Page_Controller {

}

Am I missing something?

Avatar
dhensby

Community Member, 253 Posts

18 May 2016 at 8:48pm

Edited: 18/05/2016 8:49pm

What's the actual error you're getting? Is none of this being rendered at all or is it just the banner section not being rendered or the sections not looping?

I *suspect* it's just completely not rendering - your Controller class is misnamed (you've got it as `Service_Controller` when it should be `ServicePage_Controller`)

Avatar
Turismo

Community Member, 28 Posts

18 May 2016 at 11:06pm

Ha ha, oh dear I completely neglected to update the controller class name - everything now works!

Thanks Dan.