Jump to:

3397 Posts in 978 Topics by 854 members

Template Questions

SilverStripe Forums » Template Questions » (solved) Using PHP Variables in Template

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 2022 Views
  • Beppo98
    Avatar
    Community Member
    19 Posts

    (solved) Using PHP Variables in Template Link to this post

    Hello,

    I am new at SilverStripe and perhaps I have a very simple question.

    I created two PHP files in mysite/code with the name ScriptForm.php and ScriptFormPage.php. The ScriptFormPage only returns in the controller class an object with the type ScriptForm. And in the ScriptForm.php I want to read data from a data table in mysql (with PHP) and than I want to display this data for example as a table.

    For this I created the templates SkriptFormPage.ss and SkriptForm.ss. In the SkriptFormPage.ss I only have this code:

    <div id="Content" class="typography">
    $Content
    $SkriptForm
    </div>

    And in the SkriptFormPage.ss I want to display the data which I have read in the ScriptForm.php.

    But currently I don't know how I can access in SkriptFormPage.ss to the data I have read in ScriptForm.php.

    Can anybode help me with my simple problem?

    Best regards,
    Beppo

  • martimiz
    Avatar
    Forum Moderator
    1038 Posts

    Re: (solved) Using PHP Variables in Template Link to this post

    I'm not sure about your ScriptForm.php file. What does it do? Is it a SilverStripe class extension? A stand-alone php script or some external class library? Do you want your script to read data from Your SilverStripe database or some external db?

    Without a bit more info, it's hard to help. One thing I know, is that SilverStripe is built to make things easy and safe as long as you stay within the framework. Once you start using stand-alone scripts outside the framework, things might get tricky...

  • Beppo98
    Avatar
    Community Member
    19 Posts

    Re: (solved) Using PHP Variables in Template Link to this post

    Hello martimiz,

    thank you very much for your help. Here is the code:

    SkriptFormPage.php

    <?php
    class SkriptFormPage extends Page {

       public static $db = array(
       );

       public static $has_one = array(
       );
       
    }
    class SkriptFormPage_Controller extends Page_Controller {

    function SkriptForm()
    {
    return new SkriptForm($this,'SkriptForm');
    }

    }

    SkriptForm.php

    <?php
    class SkriptForm extends Form
    {

    function __construct($controller, $name)
    {
    // define form structure
    $fields = new FieldSet(
    new TextField('test', 'test'),
    new TextField('name', 'Name')

    );
          
          
       //This is the code i have implemented   
          
       $sql = "SELECT COMMAND (...)";
    $result = mysql_query($sql);
    $amount= mysql_num_rows($result);
          

    $actions = new FieldSet(
    new FormAction('submit', 'Send')
    );

    $requiredFields = new RequiredFields('name', 'email');

    parent::__construct($controller, $name, $fields, $actions, $requiredFields);
    }

    function forTemplate()
    {
    // define template for rendering
    return $this->renderWith(array(
    $this->class,
    'Form'
    ));
    }

    function submit($data, $form)
    {
    // TODO: Implement
    }
    }

    ?>

    One part of the code I copied from an example on the silverstripe homepage. But what I exactly want to do is to display the - for example - variable $amount in the template SkriptFormPage.ss. The code of this template I have allready posted.

    The database I want to access is a table in the same scheme as the tables of silverstripe are. I created this tables with a sql-statement.

    Best regards

  • apiening
    Avatar
    Community Member
    55 Posts

    Re: (solved) Using PHP Variables in Template Link to this post

    moin beppo,

    you don't normally connect to the db yourself. the idea of a object orientated framework (e.g. sapphire) is that it provides a database abstraction layer (dbal, which does all the SQL for you) and an object relational mapper (orm, sits on top of the dbal and provides persistency for your objects through methods for creating, reading, updating and deleting objects). the advantage is that you can concentrate on the business logic of your app.

    if you have a class say:

    class Skript extends DataObject { // DataObject is the abstract master class for all persistent objects in sapphire
    static $db = array('Test' => 'Varchar', 'Name' => 'Varchar'); // property definition, id, created and lastupdated properties come out of the box
    }

    then in your SkriptFormPage.php you could do:

    function Skrips() {
    // automatically retrieves all records in the Skript table, turns them into php Skript objects and returns them in a DataObjectSet container
    return DataObject::get('Skript');
    }
    function SkriptForm() {
    return new Form($this, 'SkriptForm', singleton('Skript')->getCMSFields(), new FieldSet(new FormAction('addSkript', 'Submit')));
    }

    and in your SkriptFormPage.ss you could just write:

    <% if Skrips %>
    <ul>
    <% control Skripts %>
    <li>$Test, $Name</li>
    <% end_control %>
    </ul>
    <% else %>
    <p>no skripts</p>
    <% end_if %>
    $SkriptForm

    in the above example tempalte 'Skripts' calls the Skripts function in your SkriptsFormPage class and iterates over the items contained in the returned data object set. then it calls the SkriptForm function and renders the returned form with Form.ss automatically (the Form class extends ViewableData)

    good luck

  • Beppo98
    Avatar
    Community Member
    19 Posts

    Re: (solved) Using PHP Variables in Template Link to this post

    Hello apiening,

    thank you very much for your good answer. I going to do what you have described.

    Best regards, Beppo

    2022 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.