Jump to:

23355 Posts in 17838 Topics by 2862 members

General Questions

SilverStripe Forums » General Questions » question add image template - noob

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 2 3
Go to End
Author Topic: 2039 Views
  • wachupe
    Avatar
    Community Member
    15 Posts

    question add image template - noob Link to this post

    Hi I'm new to using silverstripe and I have a question
    how do I show all images that are inside a folder in the template, and that if I change an image by a tab
    automaticamanete change to the template?

    pd: sorry for my english

  • zenmonkey
    Avatar
    Community Member
    527 Posts

    Re: question add image template - noob Link to this post

    I've done something like that to parse a CSV upload and automatically attach images to a dataobject. No real reason you couldn't do something like that right on a page object, though it may not be the most efficient way to do it.

    First you need to add a has one relation to your DataObject ( in this case I assume it would be a page)

    static $has_one = array (
          //Folder
          "ImageFolder" => "SiteTree"
       );

    The CMSField would look something like this

    $fields->addFieldToTab("Root.Main", new TreeDropdownField('ImageFolderID', 'Image Folder', "Folder"));


    Basically you're adding a new drop down field with the names of only Folders

    Then you'd need a function to get the images in that folder

    function GetMyImages(){

    $folderID=$this->ImageFolderID;
    // get images in the folder $folderIF
    $images = DataObject::get("File", "ParentID ='$folderID');

    if ($images) {
    return $images;
    } else {
    return false;
    }

    That should work, I haven't tested it though, just going off the top of my head. Like I said I was parsing a CSV file to attach specific images to a dataobject. You should be able to access that in a template with:

    <% control GetMyImages %>
    $Image
    <% end_control %>

    You may run into problems if you have non images in the folder and you may need to filter out the images in the $images dataobject

  • wachupe
    Avatar
    Community Member
    15 Posts

    Re: question add image template - noob Link to this post

    hI zenmonkey, thx for you reply

    add the tab main in the site tree , but when I select the folder shows nothing

    this is mi php

    <?php
    /**
    * Defines the HomePage page type
    */

    class HomePage extends Page {
    static $db = array(
    );
    static $has_one = array(
    "ImageFolder" => "SiteTree"
    );

       public function getCMSFields() {      
          
          $fields = parent::getCMSFields();
          
          
          $fields->addFieldToTab("Root.Main", new TreeDropdownField('ImageFolderID', 'Image Folder', "Folder"));
          return $fields;
       }
    }

    class HomePage_Controller extends Page_Controller {

          function GetMyImages(){

          $folderID=$this->ImageFolderID;
          // get images in the folder $folderIF
          $images = DataObject::get("File", "ParentID =$3");

          if ($images) {
          return $images;
          } else {
          return false;
          }
       }   
    }

  • zenmonkey
    Avatar
    Community Member
    527 Posts

    Re: question add image template - noob Link to this post

    You do have an error in your code (as does my original, i forgot the closing quote)

    $images = DataObject::get("File", "ParentID =$3");


    Should be

    $images = DataObject::get("File", "ParentID ='$folderID'");

    Beyond that I just realized GetMyImages doesn't return the DataObject as an Image and therefor doesn't generate the tag for you. You need to use $Tag instead of $Image in your template or build the tag yourself <img src="$Filename" width="$Width" height="$Height" />

    This isn't ideal, but maybe someone else has an easier way of converting the DataObject into a set of Images. The Gallery module may have some hints

  • wachupe
    Avatar
    Community Member
    15 Posts

    Re: question add image template - noob Link to this post

    when I select the folder "Uploads" shows nothing :'c
    and in the template the control only show 3 items found within the assets folder and not in uploads.

    <img src="/assets/error-404.html" alt="error-404.html" />
    <img src="/assets/error-500.html" alt="error-500.html" />
    <img src="/assets/Uploads/" alt="Uploads" />

    These are the archives showing

    pd: sry for the problems :'c

  • zenmonkey
    Avatar
    Community Member
    527 Posts

    Re: question add image template - noob Link to this post

    Are there images in the folder? Personally I'd recommend creating a separate folder to draw from. And make sure if you've uploaded via FTP to go the Files and Images tab and search for new images. You could also change

    $images = DataObject::get("File", "ParentID ='$folderID'");


    to

    $images = DataObject::get("File", "ParentID ='$folderID' AND ClassName='Image'");


    That should restrict it to only looking for Images and prevent it returning any subfolders and other files

  • wachupe
    Avatar
    Community Member
    15 Posts

    Re: question add image template - noob Link to this post

    thx, this works!!!

    have a question, when add the images for a tab, that displays a pop up, in this add the external link.

    I must add the control to select the image that takes me to that address

  • zenmonkey
    Avatar
    Community Member
    527 Posts

    Re: question add image template - noob Link to this post

    Not sure what you mean. Sorry.

    Do you want the images to link to external Sites?

    Do you have an example published somewhere to help?

    2039 Views
Page: 1 2 3
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.