Skip to content

Instantly share code, notes, and snippets.

@trev
Last active December 15, 2015 08:49
Show Gist options
  • Save trev/5233816 to your computer and use it in GitHub Desktop.
Save trev/5233816 to your computer and use it in GitHub Desktop.

Revisions

  1. Trevor revised this gist Apr 2, 2013. 1 changed file with 10 additions and 8 deletions.
    18 changes: 10 additions & 8 deletions SilverStripe.3.0-Recipes-Errors-Tips-And-Tricks.md
    Original file line number Diff line number Diff line change
    @@ -108,13 +108,15 @@ Many HTML boilerplates and frontend frameworks use the **vendor** naming convent

    In order to fix this problem, edit your .htaccess at the root of your SS installation like so:

    <IfModule mod_alias.c>
    RedirectMatch 403 /silverstripe-cache(/|$)
    # This is the standard SS config, but it will block all vendor folders when we don't want
    # it to block vendor folders under themes/saeq/
    # RedirectMatch 403 /vendor(/|$)
    RedirectMatch 403 ^/vendor(/|$)
    RedirectMatch 403 /composer\.(json|lock)
    </IfModule>
    <IfModule mod_alias.c>
    RedirectMatch 403 /silverstripe-cache(/|$)
    # This is the standard SS config, but it will block all vendor folders when we don't want
    # it to block vendor folders under themes/saeq/
    # RedirectMatch 403 /vendor(/|$)
    RedirectMatch 403 ^/vendor(/|$)
    RedirectMatch 403 /composer\.(json|lock)
    </IfModule>

    By adding the caret character (`^`) we're telling .htaccess to only restrict access to the vendor folder at the root of the SS folder


  2. Trevor revised this gist Apr 2, 2013. 1 changed file with 17 additions and 2 deletions.
    19 changes: 17 additions & 2 deletions SilverStripe.3.0-Recipes-Errors-Tips-And-Tricks.md
    Original file line number Diff line number Diff line change
    @@ -92,14 +92,29 @@ Similiar to Google Chrome Developer Tools, the jQuery.entwine inspector allows y

    CTRL+` // CTRL + Backtick

    ## document.write() - Dealing with the SilverStripe template engine
    ## document.write() - Dealing with the SS template engine
    ### Details
    When using a boilerplate such as [H5BP]() or [Zurb Foundation](), you might encounter an error with a line simliar to:
    When using an HTML boilerplate such as [H5BP]() or [Zurb Foundation](), you might encounter an error with a line simliar to:

    <script>document.write('<script src=$ThemeDir/javascript/vendor/' + ('__proto__' in {} ? 'zepto' : 'jquery') + '.js><\/script>');</script>

    Unfortunately, the above line wont work since it'll pass through the SS template engine first. You therefore need to escape the `</script>` tag within the `document.write()` a bit differently.

    <script>document.write('<script src=$ThemeDir/javascript/vendor/' + ('__proto__' in {} ? 'zepto' : 'jquery') + '.js>\\x3C/script>');</script>

    ## The vendor folder permissions
    ### Details
    Many HTML boilerplates and frontend frameworks use the **vendor** naming convention for a place to put 3rd party resources such as jQuery & Modernizr. Unforunately, SilverStripe has an `.htaccess` directive which forbids access to any folder names **vendor** since it's used by [Composer]() and contains sensitive configuration information about your SS setup.

    In order to fix this problem, edit your .htaccess at the root of your SS installation like so:

    <IfModule mod_alias.c>
    RedirectMatch 403 /silverstripe-cache(/|$)
    # This is the standard SS config, but it will block all vendor folders when we don't want
    # it to block vendor folders under themes/saeq/
    # RedirectMatch 403 /vendor(/|$)
    RedirectMatch 403 ^/vendor(/|$)
    RedirectMatch 403 /composer\.(json|lock)
    </IfModule>


  3. Trevor revised this gist Apr 2, 2013. 1 changed file with 13 additions and 1 deletion.
    14 changes: 13 additions & 1 deletion SilverStripe.3.0-Recipes-Errors-Tips-And-Tricks.md
    Original file line number Diff line number Diff line change
    @@ -90,4 +90,16 @@ Similiar to Google Chrome Developer Tools, the jQuery.entwine inspector allows y

    ### Hotkey

    CTRL+` // CTRL + Backtick
    CTRL+` // CTRL + Backtick

    ## document.write() - Dealing with the SilverStripe template engine
    ### Details
    When using a boilerplate such as [H5BP]() or [Zurb Foundation](), you might encounter an error with a line simliar to:

    <script>document.write('<script src=$ThemeDir/javascript/vendor/' + ('__proto__' in {} ? 'zepto' : 'jquery') + '.js><\/script>');</script>

    Unfortunately, the above line wont work since it'll pass through the SS template engine first. You therefore need to escape the `</script>` tag within the `document.write()` a bit differently.

    <script>document.write('<script src=$ThemeDir/javascript/vendor/' + ('__proto__' in {} ? 'zepto' : 'jquery') + '.js>\\x3C/script>');</script>


  4. Trevor created this gist Mar 24, 2013.
    93 changes: 93 additions & 0 deletions SilverStripe.3.0-Recipes-Errors-Tips-And-Tricks.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,93 @@
    # SilverStripe 3.0 Recipes, Errors, Tips and Tricks

    # RECIPES

    ## Get a specific page and access its children
    ### Possible usage
    Get the children from a specific page and add them to the main menu

    ### Code

    <% with Page("about-us") %>
    <% loop Children %>
    $MenuTitle.XML
    <% end_loop %>
    <% end_with %>

    ## Access information from another page
    ### Possible usage
    Displaying news items on the homepage or in a sidebar

    ### Code

    public function LatestNews($num = 5) {
    // Get the new article holder
    $holder = ArticleHolder::get()->First();
    // Get the latest 5 news article
    return ($holder) ? ArticlePage::get()->filter('ParentID', $holder->ID)->sort('Date DESC')->limit($num) : false;
    }

    ### Notes
    The above mentioned code would usually go in the controller of the page type. It'll return an array that can then be access in template with `<% loop LatestNews %>`

    ### Recommendations
    > The controller for a page is only created when page is actually visited, while the data object is available when the page is referenced in other pages, e.g. by page controls. A good rule of thumb is to put all functions specific to the page currently being viewed in the controller; only if a function needs to be used in another page should you put it in the data object.

    ### References

    * http://doc.silverstripe.org/framework/en/topics/datamodel
    * http://doc.silverstripe.org/framework/en/tutorials/2-extending-a-basic-site

    ## Overriding a constructor - Execute code on instantiation
    ### Possible usage
    Whenever you want a chunk of code to run as soon as a controller class is initialized.

    ### Code

    public function init() {
    // Code here ...
    // Call the parent method we're overriding and run that too so the chain of execution continues
    parent::init();
    }

    ### Notes
    Typically you'd overwrite the `__constuct()` method provided by default with PHP but SilverStripe uses a the custom method `init()` instead.

    # ERRORS
    ## CMS File upload error: SyntaxError: Unexpected token E
    ### Details

    * Add an `uploadField()` field to the CMS
    * Try to upload a file using the newly added field
    * You may get error: `SyntaxError: Unexpected token E`

    ### Solution
    Set write permission (potentially 777 depending on setup) to `assets/Uploads`

    # TIPS AND TRICKS
    ## CMS Page type description
    ### Details
    Add a page type description to increase user friendliness

    ### Code

    static $description = 'This is a descriptive text about this page type';

    ## Prepare array for view in template
    ### Details
    Normal PHP arrays *cannot* be displayed in SS templates. You need to wrap them with [ArrayData](http://api.silverstripe.org/3.0/class-ArrayData.html).

    ### Code

    $list = Array(1 => "Cool", 2 => "Cooler");
    return ArrayData($list);

    ## jQuery.entwine debug inspector for CMS
    ### Details
    Similiar to Google Chrome Developer Tools, the jQuery.entwine inspector allows you to see what entwine methods are bound to a specific element.

    ### Hotkey

    CTRL+` // CTRL + Backtick