Showing posts with label hello world. Show all posts
Showing posts with label hello world. Show all posts

Sunday, 24 August 2008

Introduction to Smarty

Smarty is a template engine for PHP, and one that is very easy to use at that.

It can be downloaded from http://www.smarty.net/download.php or is included in some Linux distributions.

Rather than repeat them all here, there are easy to use installation instructions on the Smarty website at http://www.smarty.net/quick_start.php

There are two ways primary ways to assign variables to Smarty, directly as a variable, e.g. a string or an integer which can be down via the following:

//assign an integer of 1 to the variable 'var1'
$smarty->assign('var1',1);

//assign a string 'test' to the variable 'var2'
$smarty->assign('var2','test');

//assign an array to the variable 'var3'
$smarty->assign('var3',array(1,2,3,4));

You can also pass an associative array to assign multiple variables at once, e.g:
$smarty->assign(array('var1' => 1,'var2' => 'test', 'var3' => array (1,2,3,4)));
This will have the same effect as the 3 lines above.

In the Smarty template, to use the variables, all you would need to do is the following:
{$var1}, this will output the value of $var1 wherever you write this.

The second way is to assign by reference, you would use this if you are assigning an object to the template, for example given a class called HelloClass that returns "Hello World" when you call its hello() function, you can assign it to smarty with:
$smarty->assign_by_ref('hello_class',new HelloClass);
and in the template you would call it with {$hello_class->hello()}

To display a template, the code is as follows:
$smarty->display('template.tpl');

There is much much more to Smarty, you can find its full manual at http://www.smarty.net/manual/en/

Wednesday, 20 August 2008

Introduction to SimpleXML with PHP

The SimpleXML php extension make it very easy to parse and create basic XML files.

Here is a basic xml file that describes a message from one person to another:
<?xml version='1.0'?>
<message>
<subject>Hello</subject>
<from>user@example.com</from>
<to>user2@example.com</to>
<body>Hello World</body>
</message>

If you create a file called test.xml with the content above then create a php file with the following, it will load in the xml file and output it as an SimpleXML object:

<?php
$xml = simplexml_load_file("test.xml");
print_r($xml);
?>
Will output:
SimpleXMLElement Object
(
[subject] => Hello
[from] => user@example.com
[to] => user2@example.com
[body] => Hello World
)
To reference a variable within the xml file you can use in the normal object way, e.g. $xml->subject

To change the value of a variable, you can simply do the following:
$xml->subject = "New Subject";

And to output the new XML file:
echo $xml->asXML();

which returns:
<?xml version="1.0"?>
<message>
<subject>New Subject</subject>
<from>user@example.com</from>
<to>user2@example.com</to>
<body>Hello World</body>
</message>
Any questions/feedback please leave a comment