Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Template Questions /

(solved) Using PHP Variables in Template


Reply


5 Posts   2341 Views

Avatar
Beppo98

Community Member, 19 Posts

12 January 2011 at 6:11am

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

Avatar
martimiz

Forum Moderator, 1098 Posts

12 January 2011 at 9:54am

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

Avatar
Beppo98

Community Member, 19 Posts

13 January 2011 at 8:15am

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

Avatar
apiening

Community Member, 60 Posts

13 January 2011 at 1:24pm

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

Avatar
Beppo98

Community Member, 19 Posts

14 January 2011 at 5:31am

Hello apiening,

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

Best regards, Beppo