Jump to:

23483 Posts in 18976 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » Basic Question (you would think)

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

Page: 1
Go to End
Author Topic: 958 Views
  • Johan
    Avatar
    Community Member
    49 Posts

    Basic Question (you would think) Link to this post

    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>

  • TotalNet
    Avatar
    Community Member
    181 Posts

    Re: Basic Question (you would think) Link to this post

    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.

  • Pigeon
    Avatar
    Community Member
    243 Posts

    Re: Basic Question (you would think) Link to this post

    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

  • TotalNet
    Avatar
    Community Member
    181 Posts

    Re: Basic Question (you would think) Link to this post

    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.

  • Johan
    Avatar
    Community Member
    49 Posts

    Re: Basic Question (you would think) Link to this post

    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.

  • TotalNet
    Avatar
    Community Member
    181 Posts

    Re: Basic Question (you would think) Link to this post

    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

  • Mo
    Avatar
    Community Member
    508 Posts

    Re: Basic Question (you would think) Link to this post

    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

    958 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.