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>
This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.
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.
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>
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 tutorials are a good place to start too.
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
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.
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.
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
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