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.

General Questions /

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

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

Basic Question (you would think)


Reply


7 Posts   1009 Views

Avatar
Johan

Community Member, 49 Posts

28 August 2010 at 4:30am

Edited: 28/08/2010 4:33am

In a typical .ss file we output a variable like this:

$Content

Is there a way to output like this:

IF $Content is empty THEN

PRINT nothing

ElSE

PRINT <h2>Quote</h2><p>$Content</p>

Avatar
TotalNet

Community Member, 181 Posts

28 August 2010 at 9:51am

Edited: 28/08/2010 9:54am

Hi Johan,

There are many things you can do with templates: http://doc.silverstripe.org/templates#if_blocks

essentially, if you wanted to check for the presence of $Content then:

<% if Content %>
<h2>You Have Content :)</h2>
$Content
<% else %>
<h2>You have no content :(</h2>
<% end_if %>

Note there is no $ before Content in the condition.

Also take a look at the blackcandy templates for examples of this in action. The [url=http://doc.silverstripe.org/tutorials]tutorials[/url] are a good place to start too.

Avatar
Pigeon

Community Member, 243 Posts

28 August 2010 at 10:37am

TotalNet,

That won't work because content uses the HTML editor field which will mean contact is an empty p tag if the user adds no content.

This means the condition needs to be:

<% if Content.NoHTML %>
...

Hope that works

Avatar
TotalNet

Community Member, 181 Posts

28 August 2010 at 11:39am

Hey Pigeon,

I remember there being something about empty <p> tags a while back but don't seem to get them any more in 2.4.0. Could be a TinyMCE improvement since then.

Still, Content.NoHTML would be the best way to go in case there's non-displaying markup in the db field, thanks for the tip.

Avatar
Johan

Community Member, 49 Posts

31 August 2010 at 8:29pm

Edited: 31/08/2010 8:31pm

Thank you all very much.

Shame there are no arrays for multiple content areas so that these fields do not need to be replicated (eg. Content1, Content2 or more accurately link1, link2, link3 etc...)

Good stuff I am really liking Silvertripe.

Avatar
TotalNet

Community Member, 181 Posts

1 September 2010 at 8:42am

Hi Johan,

"Shame there are no arrays for multiple content areas"

That's where Data Objects come in. There aren't any great tutorials or recipes for this and don't have time to give you an example right now but will come back later with something if no one else has stepped in by then.

Rich

Avatar
Mo

Community Member, 510 Posts

1 September 2010 at 9:39am

For multiple links, you should just be able to create a has many relationship to SiteTree, then generate a TreeMultiSelectField using SiteTree.

Something like:

class Page extends SiteTree {
...

public static $has_many = array(
'Links' => 'SiteTree'
);

public function getCmsFields() {
...
$fields->addFieldToTab('Root.Main.Links',new TreeMultiSelectField('Links','Choose links to appear on page',DataObject::get('SiteTree')));
...
}
}

Then you should be able to loop through those links with something like:

<h1>$Title</h1>
$Content

<% if Links %><ul>
<% control Links %><li><a href="$Link">$Title</a></li><% end_control %>
</ul><% end_if %>

I ,literally just plucked that out of the air, so it will probably need a bit of tweeking. The DataObject will need some more complex explination, but there are some useful links:

http://doc.silverstripe.org/datamodel
http://doc.silverstripe.org/recipes:cheat_sheet?s[]=has&s[]=many
http://doc.silverstripe.org/tutorial:5-dataobject-relationship-management?s[]=has&s[]=many

They may be of some help. Also the Silverstripe book has some very handy info in it.

Hope that helps,

Mo