21280 Posts in 5729 Topics by 2600 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 782 Views |
-
Basic Question (you would think)

28 August 2010 at 4:30am Last edited: 28 August 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>
-
Re: Basic Question (you would think)

28 August 2010 at 9:51am Last edited: 28 August 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 tutorials are a good place to start too.
-
Re: Basic Question (you would think)

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
-
Re: Basic Question (you would think)

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.
-
Re: Basic Question (you would think)

31 August 2010 at 8:29pm Last edited: 31 August 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.
-
Re: Basic Question (you would think)

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
-
Re: Basic Question (you would think)

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[]=manyThey may be of some help. Also the Silverstripe book has some very handy info in it.
Hope that helps,
Mo
| 782 Views | ||
|
Page:
1
|
Go to Top |



